// Example for CS 2504, Spring 2006 // BubbleSort in C // // Author: William D McQuain // Modified: Feb 12, 2006 // #include #include #define SZ 10 void PrintList(int* L, int Sz); void BubbleSort(int* L, int Sz); int main() { int List[SZ] = {17, 5, 92, 87, 41, 10, 23, 55, 72, 36}; BubbleSort(List, SZ); PrintList(List, SZ); return 0; } void PrintList(int* L, int Sz) { int Pos; for (Pos = 0; Pos < Sz; Pos++) { printf("%d\n", L[Pos]); } } void BubbleSort(int* L, int Sz) { int Stop, // $s3: upper limit for pass Curr, // $s0: index of current value in comparison Next, // $s1: index of successor to current value Temp; // $s2: temp storage for swap for (Stop = Sz - 1; Stop > 0; Stop--) { for (Curr = 0; Curr < Stop; Curr++) { Next = Curr + 1; if ( L[Curr] > L[Next] ) { Temp = L[Curr]; L[Curr] = L[Next]; L[Next] = Temp; } } } }