几种排序的性能测试

几种排序的性能测试

     闲的无聊,预习数据结构的时候看到了几种排序的性能分析,就自己做了一下。
    测试结果如下:单位(ms)  RE:用时过长或爆栈


 

可见,快排在完全随机的数据下性能是最优的,在已经排序或者已经基本排序的前提下,即使是加了随机化的快排也会很慢,堆排在已经排序的情况下比随机情况下快了一倍多,归排也是,而且速度比堆排很多在各个情况下都很高效C++自带的sort函数只有在已经排序的情况下是最快的(接近O(n)其他时候的速度并不是最快的但效率依旧很高。

 快排的三者取中的优化在数据是顺序的情况下较快但在逆序的情况下很慢,随机化在顺序情况下较慢在逆序情况下较快普通快排在数据基本排序的前提下基本O(n^2)的.

lc思念落叶
2014/12/28 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 编写的排序算法及测试代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> #include <chrono> using namespace std; using namespace std::chrono; // 直接插入排序 void insertionSort(int arr[], int n) { int i, j, key; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } // 希尔排序 void shellSort(int arr[], int n) { for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int temp = arr[i]; int j; for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) { arr[j] = arr[j - gap]; } arr[j] = temp; } } } // 冒泡排序 void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); } } } } // 快速排序 int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { if (arr[j] <= pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } // 直接选择排序 void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } swap(arr[i], arr[minIndex]); } } // 堆排序 void heapify(int arr[], int n, int i) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < n && arr[l] > arr[largest]) { largest = l; } if (r < n && arr[r] > arr[largest]) { largest = r; } if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } } void heapSort(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); } for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); heapify(arr, i, 0); } } // 生成随机数 void generateRandomArray(int arr[], int n) { srand(time(NULL)); for (int i = 0; i < n; i++) { arr[i] = rand() % n; } } // 打印数组 void printArray(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } // 测试排序算法的时间性能 void testSortAlgorithm(int arr[], int n, string sortName, void(*sortFunc)(int[], int)) { auto start = high_resolution_clock::now(); sortFunc(arr, n); auto end = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(end - start); cout << sortName << ": " << duration.count() << " microseconds" << endl; } int main() { const int n = 10000; int arr[n]; generateRandomArray(arr, n); testSortAlgorithm(arr, n, "Insertion Sort", insertionSort); testSortAlgorithm(arr, n, "Shell Sort", shellSort); testSortAlgorithm(arr, n, "Bubble Sort", bubbleSort); testSortAlgorithm(arr, n, "Quick Sort", quickSort); testSortAlgorithm(arr, n, "Selection Sort", selectionSort); testSortAlgorithm(arr, n, "Heap Sort", heapSort); return 0; } ``` 上述代码实现了直接插入排序、希尔排序、冒泡排序、快速排序、直接选择排序、堆排序这六种常见的排序算法,并使用随机数生成器生成长度为 10000 的随机数组进行测试。可以根据需要修改数组长度和测试数据。运行程序后,将会输出每种排序算法的时间性能,单位为微秒。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值