📄 c09p462.txt
字号:
void bubbleSort(DataType theArray[], int n)// ---------------------------------------------------// Sorts the items in an array into ascending order.// Precondition: theArray is an array of n items.// Postcondition: theArray 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: theArray[n+1-pass..n-1] is sorted // and > theArray[0..n-pass] sorted = true; // assume sorted for (int index = 0; index < n-pass; ++index) { // Invariant: theArray[0..index-1] <= // theArray[index] int nextIndex = index + 1; if (theArray[index] > theArray[nextIndex]) { // exchange items swap(theArray[index], theArray[nextIndex]); sorted = false; // signal exchange } // end if } // end for // Assertion: theArray[0..n-pass-1] < // theArray[n-pass] } // end for} // end bubbleSort
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -