bubblesorta.cpp

来自「C++上机课的习题答案」· C++ 代码 · 共 40 行

CPP
40
字号
// bubblesorta.cpp 
//用冒泡排序法对数组进行升序排序,并追踪比较的次数和发生数值交换的次数。
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   const int SIZE = 10;
   int a[ SIZE ];
   int hold;
   int numberOfComparisons = 0;//记录比较的次数 
   int numberOfExchange = 0;//记录交换的次数

   cout<<"Please input 10 integers:\n"; 
   /* Write for loop to read 10 integers: */
   
   cout << "\n";

   /* Write bubble sort implementation(实现) here 
      and appropriately increment numberOfComparisons and numberOfExchange */      

   cout << "Data items in ascending order:\n";//升序

   for ( int j = 0; j < SIZE; ++j )
      cout << a[ j ]<<" ";

   cout << "\nNumber of comparisons = " << numberOfComparisons;
   cout << "\nNumber of exchange = " << numberOfExchange
        << endl;

   system("PAUSE");
   return 0;

} // end main


⌨️ 快捷键说明

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