测试环境为DEV-C++,并且选择排序,插入排序,冒泡排序,均为优化后的,若想了解具体优化过程,请参照:https://blog.csdn.net/qq_40164152
测试用例:
- #ifndef OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H
- #define OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H
- #include <iostream>
- #include <algorithm>
- #include <ctime>
- #include <string>
- #include <cassert>
- using namespace std;
- SortTestHelper {
- // 生成有n个元素的随机数组,每个元素的随机范围为[rangeL,rangeR]
- int *generateRandomArray(int n,int range_l,1)">int range_r) {
- int *arr = new [n];
- srand(time(NULL));
- for (int i = 0; i < n; i++)
- arr[i] = rand() % (range_r - range_l + 1) + range_l;
- return arr;
- }
- 生成一个近乎有序的数组
- 首先生成一个含有[0...n-1]的完全有序数组,之后随机交换swapTimes对数据
- swapTimes定义了数组的无序程度
- int *generateNearlyOrderedArray( swapTimes){
- [n];
- for(0 ; i < n ; i ++ )
- arr[i] = i;
- srand(time(NULL));
- for( 0 ; i < swapTimes ; i ++ ){
- int posx = rand()%n;
- int posy = rand()%n;
- swap( arr[posx],arr[posy] );
- }
- 拷贝整型数组a中的所有元素到一个新的数组,并返回新的数组
- int *copyIntArray(int a[],1)"> n){
- [n];
- copy(a,a+n,arr);
- 打印arr数组的所有内容
- template<typename T>
- void printArray(T arr[],1)"> n) {
- )
- cout << arr[i] << " ";
- cout << endl;
- ;
- }
- 判断arr数组是否有序
- template<typename T>
- bool isSorted(T arr[],1)">0; i < n - 1; i++)
- if (arr[i] > arr[i + 1])
- return false;
- true 测试sort排序算法排序arr数组所得到结果的正确性和算法运行时间
- template<typename T>
- void testSort(const string &sortName,1)">void (*sort)(T[],1)">int),T arr[],1)"> n) {
- clock_t startTime = clock();
- sort(arr,n);
- clock_t endTime = clock();
- cout << sortName << " : " << double(endTime - startTime) / CLOCKS_PER_SEC << s"<<endl;
- assert(isSorted(arr,n));
- ;
- }
- };
- #endif OPTIONAL_02_SHELL_SORT_SORTTESTHELPER_H
选择排序:
基本思想:每一趟在n-i+1(i=1,2,…,n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录。直接选择排序和直接插入排序类似,都将数据分为有序区和无序区,所不同的是直接插入排序是将无序区的第一个元素直接插入到有序区以形成一个更大的有序区,而直接选择排序是从无序区选一个最小的元素直接放到有序区的最后。
- #ifndef OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H
- #define OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H
- #include <algorithm>
- std;
- template<typename T>
- void selectionSort(T arr[],1)"> n){
- ){
- int minIndex = i;
- int j = i + 1 ; j < n ; j ++ )
- if( arr[j] < arr[minIndex] )
- minIndex = j;
- swap( arr[i],arr[minIndex] );
- }
- }
- OPTIONAL_02_SHELL_SORT_SELECTIONSORT_H
插入排序:
基本思想:将一个记录插入到已排好序的有序表中,从而得到一个新的、记录数增1的有序表。
时间复杂度为O(n^2),若待排记录序列为正序,时间复杂度可提高至O(n);空间上只需要一个记录的辅助空间。
- #ifndef OPTIONAL_02_SHELL_SORT_INSERTIONSORT_H
- #define OPTIONAL_02_SHELL_SORT_INSERTIONSORT_Hvoid insertionSort(T arr[],1)">1 ; i < n ; i ++ ) {
- T e = arr[i];
- j;
- for (j = i; j > 0 && arr[j-1] > e; j--)
- arr[j] = arr[j-];
- arr[j] = e;
- }
- ;
- }
- OPTIONAL_02_SHELL_SORT_INSERTIONSORT_H
冒泡排序:
基本思想:首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换之,然后比较第二个记录和第三个记录的关键字。依次类推,直至第n-1个记录和第n个记录的关键字进行过比较为止。上述过程称做第一趟冒泡排序,其结果使得关键字最大的记录被安置到最后一个记录的位置上。然后进行第二趟冒泡排序,对前n-1个记录进行同样操作,其结果是使关键字次大的记录被安置到第n-1个记录的位置上。一般地,第i趟冒泡排序是从1到n-i+1依次比较相邻两个关键字,并在“逆序”时交换相邻记录,其结果是这n-i+1个记录中关键字最大的记录被交换到第n-i+1的位置上。判别冒泡排序结束的条件应该是“在一趟排序过程中没有进行过交换记录的操作”。
- #ifndef OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
- #define OPTIONAL_02_SHELL_SORT_BUBBLESORT_H#include <iostream>void bubbleSort( T arr[],1)">int newn; 使用newn进行优化
- do{
- newn = 0if( arr[i-1] > arr[i] ){
- swap( arr[i-],arr[i] );
- 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
- newn = i;
- }
- n = newn;
- }while(newn > );
- }
- OPTIONAL_02_SHELL_SORT_BUBBLESORT_H
希尔排序与测试:
基本思想:先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行一次直接插入排序。可以看出希尔排序其实只是改进了的插入排序,因此上面的插入排序也被称为直接插入排序。
特点:子序列的构成不是简单地“逐段分割”,而是将相隔某个“增量”的记录组成一个子序列。它通过比较相距一定间隔的元素来工作;各趟比较所用的距离随着算法的进行而减小,直到只比较相邻元素的最后一趟排序为止。
- #include <iostream>
- #include SortTestHelper.hSelectionSort.hInsertionSort.hBubbleSort.h"
- void shellSort(T arr[],1)"> 计算 increment sequence: 1,4,13,40,121,364,1093...
- int h = ;
- while( h < n/3 )
- h = 3 * h + while( h >= ){
- h-sort the array
- int i = h ; i < n ; i ++ 对 arr[i],arr[i-h],arr[i-2*h],arr[i-3*h]... 使用插入排序
- T e = arr[i];
- j;
- for( j = i ; j >= h && e < arr[j-h] ; j -= h )
- arr[j] = arr[j-h];
- arr[j] = e;
- }
- h /= ;
- }
- }
- 比较SelectionSort,InsertionSort和BubbleSort和ShellSort四种排序算法的性能效率
- ShellSort是这四种排序算法中性能最优的排序算法
- main() {
- int n = 20000 测试1 一般测试
- cout<<Test for random array,size = "<<n<<,random range [0,]endl;
- int *arr1 = SortTestHelper::generateRandomArray(n,int *arr2 = SortTestHelper::copyIntArray(arr1,1)">int *arr3 =int *arr4 =Selection SortInsertion SortBubble SortShell Sortdelete[] arr1;
- [] arr2;
- [] arr3;
- [] arr4;
- cout<< 测试2 测试近乎有序的数组,只有100个数字无序
- int swapTimes = 100;
- cout<<Test for nearly ordered array,swap time = "<<swapTimes<<endl;
- arr1 = SortTestHelper::generateNearlyOrderedArray(n,swapTimes);
- arr2 ==[] arr4;
- return ;
- }
测试结果: