There are many techniques to sort the values. Bubble sort is one of the sorting algorithm used to sort the values in effective way.Bubble sort is compare the values of adjacent elements repeatedly and then swapping their position if they exist in the wrong order.
Bubble sort Algorithm : How it works
1.The unsorted array of 5 elements given as input to bubble sort.It will compare the first and second element to check which one is greater and swap the elements if first element is greater than the second element.
2.Compare the second and third element to check which one is greater and swap the element if the second element is greater than third element.
3.Compare the third and fourth element to check which one is greater and swap the element if the third element is greater than fourth element.
4.Compare the fourth and fifth element to check which one is greater and swap the element if the fourth element is greater than fifth element.
5.Repeat steps 1-5 until no more swaps are required.
Example of Bubble sort
Input values in unsorted order:
printf("Enter the number of elements: ");
printf("Enter %d integers:\n",n);
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
printf("The sorted elements are: \n");