You are on page 1of 2

Practical-1

Objective:
To understand Bubble sort and implement it.

Introduction:
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps
them until they are in the intended order. Just like the movement of air bubbles in the
water that rise up to the surface, each element of the array move to the end in each
iteration. Therefore, it is called a bubble sort.

Example:

Algorithm:
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Code:
#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &array[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("The sorted array is: ");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

Output:

You might also like