數據結構基礎(1) --Swap & Bubble-Sort & Select-Sort

Swap的簡單實現

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //C語言方式(by-pointer):  
  2. template <typename Type>  
  3. bool swapByPointer(Type *pointer1, Type *pointer2)  
  4. {  
  5.     //確保兩個指針不會指向同一個對象  
  6.     if (pointer1 == NULL || pointer2 == NULL)  
  7.     {  
  8.         return false;  
  9.     }  
  10.   
  11.     if (pointer1 != pointer2)  
  12.     {  
  13.         Type tmp = *pointer1;  
  14.         *pointer1 = *pointer2;  
  15.         *pointer2 = tmp;  
  16.     }  
  17.   
  18.     return true;  
  19. }  

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //C++特有方式(by-reference):  
  2. template <typename Type>  
  3. void swapByReference(Type &value1, Type &value2)  
  4. {  
  5.     if (value2 != value1)  
  6.     {  
  7.         Type tmp = value1;  
  8.         value1 = value2;  
  9.         value2 = tmp;  
  10.     }  
  11. }  

小結:

雖然我們自己實現了swap,但我們還是比較推薦使用C++ STL已經實現好的std::swap()函數,其存在於命名空間std中,使用實例如下面的<冒泡排序>.

  

冒泡排序(Bubble-Sort)

算法思想:

從左到右掃描數據,找出最大的元素,將其放到數組右邊;

過程:

循環比較相鄰的兩個數,如果左邊的數比右邊的大,則交換兩個數;

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //實現:注意代碼中的三個注意點(x):  
  2. template <typename Type>  
  3. void bubbleSort(Type *begin, Type *end)  
  4. {  
  5.     if ((begin == end) || (begin == NULL) || (end == NULL))  
  6.         return ;  
  7.   
  8.     int length = end - begin;  
  9.     //注意點(1):保證一旦數組有序, 則會直接停止排序, 不會在繼續進行無用的循環  
  10.     bool isOrder = false;  
  11.   
  12.     //外層循環控制掃描次數(length-1)  
  13.     //注意點(2):N個元素其實只需N-1次掃描  
  14.     for (int i = 0; !isOrder && i < length-1; ++i)  
  15.     {  
  16.         //首先假定這次數組已經有序  
  17.         isOrder = true;  
  18.         //注意點(3):確保能夠從0掃描到最後一個未排序的元素  
  19.         for (Type *iter = begin; iter < end-i-1; ++iter)  
  20.         {  
  21.             //如果前面的左邊的元素>右邊的元素  
  22.             if (*iter > *(iter+1))  
  23.             {  
  24.                 //交換  
  25.                 std::swap(*iter, *(iter+1));  
  26.                 isOrder = false;  
  27.             }  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. template <typename Type>  
  33. void bubbleSort(Type *array, int length)  
  34. {  
  35.     return bubbleSort(array, array+length);  
  36. }  

選擇排序(Select-Sort)

思想:

從當前尚未排序的序列中選擇一個最小的元素, 將之放到已排序的序列的隊列的末尾;

要點:

1.注意三個指針(inner, outer, miner)所代表的含義;

2.同時注意是從未排序的序列中進行查找最小元素!

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //實現  
  2. template <typename Type>  
  3. void selectSort(Type *begin, Type *end)  
  4. {  
  5.     if ((begin == end) || (begin == NULL) || (end == NULL))  
  6.         return ;  
  7.   
  8.     //只要循環到最後一個元素的前一個就行了,因爲剩下的那個肯定是最大的  
  9.     for (Type *outer = begin; outer < end-1; ++outer)  
  10.     {  
  11.         //注意:是從尚未排序的序列中查找(miner = outer, inner = outer+1)  
  12.         Type *miner = outer;  
  13.         //從miner+1開始遍歷數組, 尋找一個元素值小於*miner的  
  14.         for (Type *inner = outer+1; inner < end; ++inner)  
  15.         {  
  16.             if (*inner < *miner)  
  17.                 miner = inner;  
  18.         }  
  19.   
  20.         if (miner != outer)  
  21.             std::swap(*miner, *outer);  
  22.     }  
  23. }  
  24.   
  25. //爲了能夠讓STL的標準容器如vector使用  
  26. template <typename Iterator>  
  27. void selectSort(Iterator iter1, Iterator iter2)  
  28. {  
  29.     return selectSort(&(*iter1), &(*iter2));  
  30. }  
  31.   
  32. template <typename Type>  
  33. void selectSort(Type *array, int length)  
  34. {  
  35.     return selectSort(array, array+length);  
  36. }  

小結:

雖然我們自己實現了Bubble-Sort和Select-Sort,但我們在實際軟件開發中一般是不會用到的,因爲的它的效率爲O(N^2),效率太慢^_^, 因此我們還是推薦使用C++ STL中已經實現了的std::sort(), 其內部原理使用了快速排序, 效率爲O(logN)速度非常快.

 

附-測試程序

[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. int main()  
  2. {  
  3.     srand(time(NULL));  
  4.     vector<double> dVec;  
  5.     int count = 10;  
  6.     while (count --)  
  7.     {  
  8.         dVec.push_back((rand()%1000)/100.0);  
  9.     }  
  10.   
  11.     selectSort(dVec.begin(), dVec.end());  
  12.     for (vector<double>::iterator iter = dVec.begin(); iter < dVec.end(); ++iter)  
  13.     {  
  14.         cout << *iter << endl;  
  15.     }  
  16.   
  17.     return 0;  
  18. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章