bblsortc.c |
| /* bubble sort in c */ #define UNSORTED 0 #define SORTED 1 void bubblesort(int x[], int size){ int state; /* records SORTED/UNSORTED status */ int end_index; /* number of comparisons to be done */ int i, temp; /* assume that whole array is initially unsorted */ state = UNSORTED; end_index = size; while(state == UNSORTED){ state = SORTED; /* now prove that the array is not sorted */ end_index--; for(i=0; i<end_index; i++){ /* pass loop */ /* look at the adjacent pairs of elements and swap if the second element is less than the first */ if(x[i] > x[i+1]){ temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; state = UNSORTED; } } } } |
James Little |