void BubbleSort(dataType A[], int N) // --------------------------------------------------- // Sorts the items in an array into ascending order. // Precondition: A is an array of N items. // Postcondition: The array A is sorted into ascending // order; N is unchanged. // Calls: Swap. // --------------------------------------------------- { bool Sorted = false; // false when swaps occur for (int Pass = 1; (Pass < N) && !Sorted; ++Pass) { // Invariant: A[N+1-Pass..N-1] is sorted // and > A[0..N-Pass] Sorted = true; // assume sorted for (int Index = 0; Index < N-Pass; ++Index) { // Invariant: A[0..Index-1] <= A[Index] int NextIndex = Index + 1; if (A[Index] > A[NextIndex]) { // exchange items Swap(A[Index], A[NextIndex]); Sorted = false; // signal exchange } // end if } // end for // Assertion: A[0..N-Pass-1] < A[N-Pass] } // end for } // end BubbleSort