四个O(n^2)级别的排序性能测试

前端之家收集整理的这篇文章主要介绍了四个O(n^2)级别的排序性能测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

测试环境为DEV-C++,并且选择排序,插入排序,冒泡排序,均为优化后的,若想了解具体优化过程,请参照:https://blog.csdn.net/qq_40164152

测试用例:

  1. #ifndef OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H
  2. #define OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <string>
  7. #include <cassert>
  8. using namespace std;
  9. SortTestHelper {
  10. // 生成有n个元素的随机数组,每个元素的随机范围为[rangeL,rangeR]
  11. int *generateRandomArray(int n,int range_l,1)">int range_r) {
  12. int *arr = new [n];
  13. srand(time(NULL));
  14. for (int i = 0; i < n; i++)
  15. arr[i] = rand() % (range_r - range_l + 1) + range_l;
  16. return arr;
  17. }
  18. 生成一个近乎有序的数组
  19. 首先生成一个含有[0...n-1]的完全有序数组,之后随机交换swapTimes对数据
  20. swapTimes定义了数组的无序程度
  21. int *generateNearlyOrderedArray( swapTimes){
  22. [n];
  23. for(0 ; i < n ; i ++ )
  24. arr[i] = i;
  25. srand(time(NULL));
  26. for( 0 ; i < swapTimes ; i ++ ){
  27. int posx = rand()%n;
  28. int posy = rand()%n;
  29. swap( arr[posx],arr[posy] );
  30. }
  31. 拷贝整型数组a中的所有元素到一个新的数组,并返回新的数组
  32. int *copyIntArray(int a[],1)"> n){
  33. [n];
  34. copy(a,a+n,arr);
  35. 打印arr数组的所有内容
  36. template<typename T>
  37. void printArray(T arr[],1)"> n) {
  38. )
  39. cout << arr[i] << " ";
  40. cout << endl;
  41. ;
  42. }
  43. 判断arr数组是否有序
  44. template<typename T>
  45. bool isSorted(T arr[],1)">0; i < n - 1; i++)
  46. if (arr[i] > arr[i + 1])
  47. return false;
  48. true 测试sort排序算法排序arr数组所得到结果的正确性和算法运行时间
  49. template<typename T>
  50. void testSort(const string &sortName,1)">void (*sort)(T[],1)">int),T arr[],1)"> n) {
  51. clock_t startTime = clock();
  52. sort(arr,n);
  53. clock_t endTime = clock();
  54. cout << sortName << " : " << double(endTime - startTime) / CLOCKS_PER_SEC << s"<<endl;
  55. assert(isSorted(arr,n));
  56. ;
  57. }
  58. };
  59. #endif OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H

选择排序:
基本思想:每一趟在n-i+1(i=1,2,…,n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录。直接选择排序和直接插入排序类似,都将数据分为有序区和无序区,所不同的是直接插入排序是将无序区的第一个元素直接插入到有序区以形成一个更大的有序区,而直接选择排序是从无序区选一个最小的元素直接放到有序区的最后。

  1. #ifndef OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H
  2. #define OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H
  3. #include <algorithm>
  4. std;
  5. template<typename T>
  6. void selectionSort(T arr[],1)"> n){
  7. ){
  8. int minIndex = i;
  9. int j = i + 1 ; j < n ; j ++ )
  10. if( arr[j] < arr[minIndex] )
  11. minIndex = j;
  12. swap( arr[i],arr[minIndex] );
  13. }
  14. }
  15. OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H

插入排序:
基本思想:将一个记录插入到已排好序的有序表中,从而得到一个新的、记录数增1的有序表。
时间复杂度为O(n^2),若待排记录序列为正序,时间复杂度可提高至O(n);空间上只需要一个记录的辅助空间。

  1. #ifndef OPTIONAL_02_SHELL_SORT_INSERTIONSORT_H
  2. #define OPTIONAL_02_SHELL_SORT_INSERTIONSORT_Hvoid insertionSort(T arr[],1)">1 ; i < n ; i ++ ) {
  3. T e = arr[i];
  4. j;
  5. for (j = i; j > 0 && arr[j-1] > e; j--)
  6. arr[j] = arr[j-];
  7. arr[j] = e;
  8. }
  9. ;
  10. }
  11. OPTIONAL_02_SHELL_SORT_INSERTIONSORT_H

冒泡排序:
基本思想:首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换之,然后比较第二个记录和第三个记录的关键字。依次类推,直至第n-1个记录和第n个记录的关键字进行过比较为止。上述过程称做第一趟冒泡排序,其结果使得关键字最大的记录被安置到最后一个记录的位置上。然后进行第二趟冒泡排序,对前n-1个记录进行同样操作,其结果是使关键字次大的记录被安置到第n-1个记录的位置上。一般地,第i趟冒泡排序是从1到n-i+1依次比较相邻两个关键字,并在“逆序”时交换相邻记录,其结果是这n-i+1个记录中关键字最大的记录被交换到第n-i+1的位置上。判别冒泡排序结束的条件应该是“在一趟排序过程中没有进行过交换记录的操作”。

  1. #ifndef OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
  2. #define OPTIONAL_02_SHELL_SORT_BUBBLESORT_H#include <iostream>void bubbleSort( T arr[],1)">int newn; 使用newn进行优化
  3. do{
  4. newn = 0if( arr[i-1] > arr[i] ){
  5. swap( arr[i-],arr[i] );
  6. 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
  7. newn = i;
  8. }
  9. n = newn;
  10. }while(newn > );
  11. }
  12. OPTIONAL_02_SHELL_SORT_BUBBLESORT_H

希尔排序与测试:
基本思想:先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行一次直接插入排序。可以看出希尔排序其实只是改进了的插入排序,因此上面的插入排序也被称为直接插入排序。
特点:子序列的构成不是简单地“逐段分割”,而是将相隔某个“增量”的记录组成一个子序列。它通过比较相距一定间隔的元素来工作;各趟比较所用的距离随着算法的进行而减小,直到只比较相邻元素的最后一趟排序为止。

  1. #include <iostream>
  2. #include SortTestHelper.hSelectionSort.hInsertionSort.hBubbleSort.h"
  3. void shellSort(T arr[],1)"> 计算 increment sequence: 1,4,13,40,121,364,1093...
  4. int h = ;
  5. while( h < n/3 )
  6. h = 3 * h + while( h >= ){
  7. h-sort the array
  8. int i = h ; i < n ; i ++ arr[i],arr[i-h],arr[i-2*h],arr[i-3*h]... 使用插入排序
  9. T e = arr[i];
  10. j;
  11. for( j = i ; j >= h && e < arr[j-h] ; j -= h )
  12. arr[j] = arr[j-h];
  13. arr[j] = e;
  14. }
  15. h /= ;
  16. }
  17. }
  18. 比较SelectionSort,InsertionSortBubbleSortShellSort四种排序算法的性能效率
  19. ShellSort是这四种排序算法中性能最优的排序算法
  20. main() {
  21. int n = 20000 测试1 一般测试
  22. cout<<Test for random array,size = "<<n<<,random range [0,]endl;
  23. int *arr1 = SortTestHelper::generateRandomArray(n,int *arr2 = SortTestHelper::copyIntArray(arr1,1)">int *arr3 =int *arr4 =Selection SortInsertion SortBubble SortShell Sortdelete[] arr1;
  24. [] arr2;
  25. [] arr3;
  26. [] arr4;
  27. cout<< 测试2 测试近乎有序的数组,只有100个数字无序
  28. int swapTimes = 100;
  29. cout<<Test for nearly ordered array,swap time = "<<swapTimes<<endl;
  30. arr1 = SortTestHelper::generateNearlyOrderedArray(n,swapTimes);
  31. arr2 ==[] arr4;
  32. return ;
  33. }

测试结果:

 

猜你在找的算法相关文章