DAA2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment-2

• Bubble Sort :

CODE:

#include <stdio.h>

void bubbleSort(int arr[], int n) {


if (n == 1)
return;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
bubbleSort(arr, n - 1);
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: \n");


printArray(arr, n);
return 0;
}

Pseudo Code:

Step 1 – function bubbleSort(arr, n)


Step 2 – if n == 1
return
Step 3 – for i from 0 to n-2
if arr[i] > arr[i+1]
Swap arr[i] and arr[i+1]
Step 4 - bubbleSort(arr, n-1)
Output:

You might also like