⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bubblesort.cpp

📁 Data Abstraction & Problem Solving with C++源码
💻 CPP
字号:
// Must define DataType before compilationtypedef type-of-array-item DataType;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 bubbleSortvoid swap(DataType& x, DataType& y)// ---------------------------------------------------// Swaps two items.// Precondition: x and y are the items to be swapped.// Postcondition: Contents of actual locations that x // and y represent are swapped.// ---------------------------------------------------{   DataType temp = x;   x = y;   y = temp;}  // end swap

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -