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

📄 bubblesorta.cpp

📁 C++上机课的习题答案
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -