Optimized Bubble Sort Algorithm
In case of Bubble Sort, after each iteration largest element will be shifted to right.
bubble_sort(int a[], n) {
int swapped, i, j;
for(i = 0; i < n; i++) {
swapped = 0;
for(j = 0; j < n - i - 1; j++) {
if(a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
swapped = 1;
}
}
if(swapped == 0)
break;
}
}