日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

7个重要内排序算法的实现以及实验比较

發布時間:2025/3/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 7个重要内排序算法的实现以及实验比较 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本代碼片段對7個內排序算法作出實現與比較,復制粘貼代碼即可編譯運行

其中,最需要一定掌握的是快速排序,請一定要記得快速排序的代碼shi'xi?

/************************ 內排序算法的實驗比較 使用C++語言分別實現插入排序、冒泡排序、選擇排序、歸并排序、快速排序、堆排序、基數排序算法,要求: 1)使用隨機數生成方法,分別產生規模為100、1000、10000的三組自然數序列,作為各排序算法的輸入; 2)算法的實現要求是穩定的(stable); 3)分別以三組自然數序列作為輸入,執行各排序算法。要求每組序列各執行三次,記錄各算法每次執行時間(毫秒),取平均值作為結果記入實驗報告的表1中; 4)根據表1中的結果數據,通過折線圖(橫坐標表示輸入規模,縱坐標表示平均執行時間)來對各算法的時間效率進行比較分析,回答如下問題: –各組輸入中那個排序算法時間開銷最小? 隨著輸入規模的增長,哪個排序算法的增長趨勢最緩慢? *************************/#include "stdafx.h" #include<iostream> #include<vector> #include<stdio.h> #include<stdlib.h> #include<ctime> #include<iomanip> #define random(x) (rand()%x) using namespace std;//1.插入排序 //平均時間復雜度:O(N^2) //最壞情況復雜度:O(N^2) //最好情況復雜度:O(N) //空間復雜度:O(1) //最多需要n(n?1)/2次比較 //最少需要n?1次比較 //穩定排序 void insertsort(vector<int>& a) {int n = a.size();for (int i = 1; i < n; i++){int insert_num = a[i], j;for (j = i - 1; j >= 0; j--){if (a[j] > insert_num)a[j + 1] = a[j];elsebreak;}a[j + 1] = insert_num;} }//2.冒泡排序 //平均時間復雜度:O(N^2) //最壞情況復雜度:O(N^2) //空間復雜度:O(1) //穩定排序 void bubblesort(vector<int>& a) {int n = a.size();for (int i = 0; i < n; i++){for (int j = 0; j < n - 1 - i; j++){if (a[j] > a[j + 1])swap(a[j], a[j + 1]);}} }//3.選擇排序 //平均時間復雜度 O(n^2) //最壞時間復雜度 O(n^2) //最好時間復雜度 O(n^2) //空間復雜度 O(1) //我這個寫法 是穩定排序 void select_sort(vector<int>& vt) {for (int i = 0; i < vt.size() - 1; i++){int swap_pos = i;for (int j = i + 1; j < vt.size(); j++){if (vt[swap_pos] > vt[j]){swap_pos = j;}}if (swap_pos != i){swap(vt[swap_pos], vt[i]);}} }//4.歸并排序 //平均時間復雜度:O(NlogN) //穩定排序 vector<int> mergeHelper(vector<int> &a, int left, int right) {if (left == right) return vector<int>(1, a[left]);int mid = (right - left) / 2 + left;vector<int> l = mergeHelper(a, left, mid);vector<int> r = mergeHelper(a, mid + 1, right);//mergevector<int> ret;int ll = 0, rr = 0;while (ll < l.size() && rr < r.size()){if (l[ll] <= r[rr]) ret.push_back(l[ll++]);else ret.push_back(r[rr++]);}while (ll < l.size()) ret.push_back(l[ll++]);while (rr < r.size()) ret.push_back(r[rr++]);return ret; }void mergesort(vector<int>& a) {a = mergeHelper(a, 0, a.size() - 1); }//5.快速排序 //平均時間復雜度:O(NlogN) //最壞情況復雜度:O(N^2) //不穩定排序 //快排的最差時間復雜度為O(n2) //通常出現在選擇的軸值(pivot)不能將數組劃分為兩個長度相等的子數組的時候 //一個較好的辦法是“三數取中”,查看當前數組的第一個、中間一個和最后一個位置的數組,取其中位數,以此來降低軸值選擇得不好的可能性。 int findmiddle(int a, int b, int c) {if (a >= b && a <= c)return a;else if (b >= a && b <= c)return b;elsereturn c; } void quicksortHelper(vector<int>& a, int start, int end) {if (start >= end) return;int l = start, r = end;int pivot = findmiddle(a[start], a[end], a[(end - start) / 2 + start]);while (l <= r){while (l <= r && a[r] > pivot) r--;while (l <= r && a[l] < pivot) l++;if (l <= r) swap(a[l++], a[r--]);}quicksortHelper(a, start, r);quicksortHelper(a, l, end); } void quicksort(vector<int>& a) {quicksortHelper(a, 0, a.size() - 1); }//6.堆排序 //建堆的平均時間是:O(N) //建堆的最壞情況是:O(NlogN) //刪除元素的時間是:O(logN) //整個排序平均時間復雜度:O(N+NlogN)=O(NlogN) //最壞情況復雜度:O(NlogN) //不穩定排序 //建立一個大頂堆O(n),要求就是 把最大的元素 移動到堆頂 也就是a[0] void make_heap(vector<int>& a, int size) //size的當前堆的大小,也就是數組的前size個數 {for (int i = size - 1; i > 0; i--){if (i % 2 && a[i] > a[(i - 1) / 2])//奇數swap(a[i], a[(i - 1) / 2]);else if (i % 2 == 0 && a[i] > a[(i - 2) / 2])//偶數swap(a[i], a[(i - 2) / 2]);} } void heapsort(vector<int>& a) {int n = a.size();while (n){make_heap(a, n); //每次把新的最大元素移到堆頂,也就是a[0]n--;swap(a[0], a[n]); //然后把當前最大移動到后面來作為排好序的元素} }//7.基數排序 //時間復雜度最壞情況、平均情況、最差情況均為O(d(n+r)) //其中,r代表關鍵碼的基數,d代表長度,n代表關鍵碼的個數 int maxbit(vector<int>& data) { int n = data.size();int d = 1; //保存最大的位數int p = 10;for (int i = 0; i < n; ++i){while (data[i] >= p){p *= 10;++d;}}return d; }void radixsort(vector <int>& data) //基數排序 {int n = data.size();int d = maxbit(data);int tmp[10000] = {};int count[10]; //計數器int i, j, k;int radix = 1;for (i = 1; i <= d; i++) //進行d次排序{for (j = 0; j < 10; j++)count[j] = 0; //每次分配前清空計數器for (j = 0; j < n; j++){k = (data[j] / radix) % 10; //統計每個桶中的記錄數count[k]++;}for (j = 1; j < 10; j++)count[j] = count[j - 1] + count[j]; //將tmp中的位置依次分配給每個桶for (j = n - 1; j >= 0; j--) //將所有桶中記錄依次收集到tmp中{k = (data[j] / radix) % 10;tmp[count[k] - 1] = data[j];count[k]--;}for (j = 0; j < n; j++) //將臨時數組的內容復制到data中data[j] = tmp[j];radix = radix * 10;} }int main() {vector<int> A;srand((int)(time(0)));for (int i = 0;i < 100;i++)A.push_back(random(50000));cout << "100個數據為:" << endl;vector<int> A2;vector<int> A3;vector<int> A4;vector<int> A5;vector<int> A6;vector<int> A7;for (int i = 0;i < 100;i++){A2.push_back(A[i]);A3.push_back(A[i]);A4.push_back(A[i]);A5.push_back(A[i]);A6.push_back(A[i]);A7.push_back(A[i]);}clock_t start = clock();insertsort(A);clock_t end = clock();cout << "1.插入排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC *1000<< "ms\n";start = clock();bubblesort(A2);end = clock();cout << "2.冒泡排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC*1000 << "ms\n";start = clock();select_sort(A3);end = clock();cout << "3.選擇排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC*1000 << "ms\n";start = clock();mergesort(A4);end = clock();cout << "4.歸并排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC *1000<< "ms\n";start = clock();quicksort(A5);end = clock();cout << "5.快速排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();heapsort(A6);end = clock();cout << "6.堆的排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC*1000 << "ms\n";start = clock();radixsort(A7);end = clock();cout << "7.基數排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC *1000<< "ms\n";cout << endl << endl;cout << "1000個數據為:" << endl;vector<int> AA;srand((int)(time(0)));for (int i = 0;i < 1000;i++)AA.push_back(random(50000));cout << endl;vector<int> AA2;vector<int> AA3;vector<int> AA4;vector<int> AA5;vector<int> AA6;vector<int> AA7;for (int i = 0;i < 1000;i++){AA2.push_back(AA[i]);AA3.push_back(AA[i]);AA4.push_back(AA[i]);AA5.push_back(AA[i]);AA6.push_back(AA[i]);AA7.push_back(AA[i]);}start = clock();insertsort(AA);end = clock();cout << "1.插入排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();bubblesort(AA2);end = clock();cout << "2.冒泡排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();select_sort(AA3);end = clock();cout << "3.選擇排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();mergesort(AA4);end = clock();cout << "4.歸并排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();quicksort(AA5);end = clock();cout << "5.快速排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();heapsort(AA6);end = clock();cout << "6.堆的排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();radixsort(AA7);end = clock();cout << "7.基數排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";cout << endl << endl;vector<int> AAA;srand((int)(time(0)));for (int i = 0;i < 10000;i++)AAA.push_back(random(50000));vector<int> AAA2;vector<int> AAA3;vector<int> AAA4;vector<int> AAA5;vector<int> AAA6;vector<int> AAA7;for (int i = 0;i < 10000;i++){AAA2.push_back(AAA[i]);AAA3.push_back(AAA[i]);AAA4.push_back(AAA[i]);AAA5.push_back(AAA[i]);AAA6.push_back(AAA[i]);AAA7.push_back(AAA[i]);}cout << "10000個數據為:" << endl;start = clock();insertsort(AAA);end = clock();cout << "1.插入排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();bubblesort(AAA2);end = clock();cout << "2.冒泡排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();select_sort(AAA3);end = clock();cout << "3.選擇排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();mergesort(AAA4);end = clock();cout << "4.歸并排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();quicksort(AAA5);end = clock();cout << "5.快速排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();heapsort(AAA6);end = clock();cout << "6.堆的排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";start = clock();radixsort(AAA7);end = clock();cout << "7.基數排序時間為:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms\n";cout << "10000個數據為:" << endl;}

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

總結

以上是生活随笔為你收集整理的7个重要内排序算法的实现以及实验比较的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。