C++ bubber sort

来源:互联网 发布:移动通信 算法 招聘 编辑:程序博客网 时间:2024/06/06 01:49

bubber sort:, 也就是常说的冒泡法排序。 是所有排序算法中比较简单地一个了。

bubber sort  的伪代码如下:

procedure bubbleSort( A:list of sortable items)
   n = length(A)
   repeat    
     swapped = false
     for i = 1to  n-1 inclusivedo
       /* if this pair is out of order */
       if A[i-1]> A[i]then
         /* swap them and remember something changed */
         swap( A[i-1], A[i])
         swapped = true
       end if
     end for
   until not swapped
end procedure

 

举个例子, the array of numbers "5 1 4 2 8",使用bubber sort  从小到大的对这个数组排序。 则有如下:

Now, the array is already sorted, but our algorithm does not know if it is completed.

The algorithm needs one whole pass without any swap to know it is sorted.(注意啊 , 算法并没有结束)

 

bubber sort的算法分析:

冒泡排序的最坏的时间复杂度是O(n^2)。

 

 程序如下:

//Bubber sort for array based list#include <iostream>#include <ctime>#include <cstdlib> // 产生随机数#include <iomanip>using namespace std;template <class elemType>void print(elemType list[], int length);// do swaptemplate <class elemType>void swap(elemType[], int, int);//bubber sort 总程序template <class elemType>void bubberSort(elemType[], int);int main() {   int intList[100];   int num;   for (int i = 0; i < 100; i++){      num = (rand() + time(0)) %1000;      intList[i] = num;   }   cout << "intList before sorting: " << endl;   print(intList, 100);   cout << endl << endl;   bubberSort(intList, 100);   cout << "intList after bubber sort: " << endl;   print(intList, 100);   cout << endl;   system("Pause");   return 0;}template <class elemType>void print(elemType list[], int length) {   int count = 0;   for(int i = 0; i < length; i++) {      cout << setw(5) << list[i];      count++;      if(count % 10 == 0)         cout << endl;   }}template <class elemType>void swap(elemType list[], int first, int second) {   elemType temp;   temp = list[first];   list[first] = list[second];   list[second] = temp;}template <class elemType>void bubberSort(elemType list[], int length) {   bool swaped = true;   while(swaped) {      swaped = false;      for (int i = 1 ; i < length; i++) {         if(list[i-1] > list[i]) {            swap(list, i - 1, i);            swaped = true;         }      }   }}



运行结果为:

 

上述的程序是我根据伪代码编写的。

视频tutorial的代码如下:

//Bubber sort for array based list#include <iostream>#include <ctime>#include <cstdlib> // 产生随机数#include <iomanip>using namespace std;template <class elemType>void print(elemType list[], int length);//bubber sorttemplate <class elemType>void bubberSort(elemType[], int);int main() {   int intList[100];   int num;   for (int i = 0; i < 100; i++){      num = (rand() + time(0)) %1000;      intList[i] = num;   }   cout << "intList before sorting: " << endl;   print(intList, 100);   cout << endl << endl;   bubberSort(intList, 100);   cout << "intList after bubber sort: " << endl;   print(intList, 100);   cout << endl;   system("Pause");   return 0;}template <class elemType>void print(elemType list[], int length) {   int count = 0;   for(int i = 0; i < length; i++) {      cout << setw(5) << list[i];      count++;      if(count % 10 == 0)         cout << endl;   }}template <class elemType>void bubberSort(elemType list[], int length) {  for (int i = 1; i < length; i++) { // i is 第几次的 iteration     for (int index = 0; index < length - 1; index++) {        if(list[index] > list[index + 1]) {           elemType temp = list[index];           list[index] = list[index + 1];           list[index + 1] = temp;        }     }  }}


运行结果如下:

不难看出, 最坏的情况的时间复杂度是: O(n^2)

0 0
原创粉丝点击