数据结构基础(1) --Swap & Bubble-Sort & Select-Sort

来源:互联网 发布:域名要实名认证吗 编辑:程序博客网 时间:2024/05/16 19:20

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. }  
0 0