Bubble SortBubble Sort
The bubble sort is the oldest and simplest sort in use unfortunately, it is also the slowest.
- compare two numbers at a time, starting with the first two numbers
- If the first number, is larger than second number, than exchange the two numbers,
- Go right one number and compare that number to the number that follows it. The two form a new pair.
- Continue this process until no exchange has been made in an entire pass through the list.
Ex : Pass: 0
Implementation of Bubble sort
void bubble sort(int A,int n);
printf(“Enter n value \n”);
printf(“Enter the elements:\n”);
printf(“The following are the elements in sorting orfer:\n”);
void bubble sort(int A,int n)
int pass, j,temp;
Enter n value: 10
Enter the elements :
The following are the elements in the sorting order :
1 2 3 4 5 6 7 8 9 10
Traditionally, the time for a sorting algorithm is measured in terms of the number of comparisons specially, There are n-1 comparisons during the first pass, which phases the largest element in the last position, these are n-2 comparisons in second step, which places the second largest element in the next-to-last position, and so on. Thus
f(n)=(n-1) + (n-2) +(n-3) + -------------- +2+1