#include
#include
int *bubble(int a[],int n);
int *y;
int j,i;
void main()
{
int value[]={10,2,3,5,9,6,2,3,6,8};
int n;
clrscr();
n=sizeof(value)/sizeof(int);
y=bubble(value,n);
for(i=0;i
printf("%d\n",*y++);
}
getch();
}
int *bubble(int a[],int n)
{
int t;
for(i=0;i
for(j=0;j
if(a[j]<=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
/* ellipses (...) used to represent tabs for clarity */int array[N]; /* N must be contant literal in this example */bubblesort (int *a, int n) {... int swap = 1;... while (swap) {... ... int i;... ... int temp;... ... swap = 0;... ... for (i=0; i < n-2; i++) {... ... ... if (a[i] > a[i+1]) {... ... ... ... swap = 1;... ... ... ... temp = a[i];... ... ... ... a[i] = a[i+1];... ... ... ... a[i+1] = temp;... ... ... }... ... }... ... n--;... }}/* fill the array with some data */bubblesort (a, N);
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
to eliminate unnecessary swaps to eliminate unnecessary comparisons to stop as soon as the list is sorted to sort an array of unknown size
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
void sort (int& a, int& b, int& c) { if (a>b) std::swap (a, b); if (b>c) std::swap (b, c); else return; if (a>b) std::swap (a, b); } Note that this is based upon a bubble sort algorithm. Although usually inefficient as a general sorting algorithm, given that we know there are only three elements means we can implement it reasonably efficiently without any additional space complexity. There will always be 2 or 3 comparisons but at most there will be 3 swaps. The only improvement we could really make is to implement a type of selection sort: void sort (int& a, int& b, int& c) { if (a>b && a>c) std::swap (a, c); else if (b>c) std::swap (b, c); if (a>b) std::swap (a, b); } Here we either make 3 or 4 comparisons but only 2 swaps at most. The assumption here is that a comparison is a quicker operation than a swap thus the selection sort method is more efficient. However, unless you were to sort millions of sets of three one after the other, you are unlikely to see any measurable difference in performance.
The algorithm for bubble sort, also know as pair exchange...Set a swap flag falseLoop for the first N-1 elementsCompare each element with the following elementIf the two elements are in the correct order, continue to next loop iterationOtherwise, swap the two elements and set the swap flag trueAt loop end, if the swap flag is true, repeat starting at step 1Otherwise, sort completedBubble sort is so named because out of order elements "bubble" to the end of the array, moving one step per inner loop iteration.As stated above, the algorithm is slow because it takes a while for a significantly out of order element to reach its final point when it needs to come closer to the beginning of the array. It can be improved by introducing a "distance" parameter, initially set to one half of the array size, and using that distance in step 3 to choose the second element. After the algorithm is completed for that distance, the distance is halved, and we iterate the entire algorithm until the distance is only one. (This variation is more formally known as merge exchange, but it still retains the "bubble" characteristic.)
a=a^b; b=a^b; a=a^b;
#include using namespace std; void swap(int &a, int &b); int main() { int x=5,y=3; cout
bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }
A bubble sort may have a range from O(n-1) for a pre-sorted array, to O(n2-n) for a poorly implemented bubble sort algorithm. Given 20 elements, a best case scenario is 19 comparisons, and the worst case is 380 comparisons.