/* * Source name : bubble_sort.c * Executable name : bubble_sort * Version : 1.0 * Created date : 2/28/12 * Last update : 2/28/12 * Author : Jason Fritts * Description : Simple bubble sort example * * Build using this command: * gcc -01 -m32 bubble_sort.c -o bubble_sort */ #include #define N 7 void one_pass (int array[], int n) { int j; int t; for (j = 0; j < n; j++) { if (array[j] > array[j+1]) { t = array[j]; array[j] = array[j+1]; array[j+1] = t; } } } int main () { int i; int nums[] = {25, -13, 57, 18, 4, 63, 12}; /* Make N-1 passes over the elements, moving the next maximum to the end each time */ for (i = 0; i < N-1; i++) one_pass (nums, (N-1)-i); /* Print out n elements of number pattern */ printf ("\nDisplaying %d elements of pattern:\n ", N); for (i = 0; i < N; i++) printf ("%d ", nums[i]); printf ("\n"); }