鱼C论坛

 找回密码
 立即注册
查看: 1672|回复: 5

[技术交流] C++实现9大排序算法

[复制链接]
发表于 2020-5-8 14:18:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-5-14 14:58 编辑

一、冒泡排序
参考视频:https://www.bilibili.com/video/av26394341?p=6

n代表待排序的元素个数
时间复杂度:最好情况O(n)、最坏情况O(n^2)、平均情况O(n^2)       
空间复杂度:O(1)
稳定性:稳定
属于内排序

代码实现:
  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;


  4. void quick_sort(vector<int>& input){
  5.         int len = input.size();
  6.         for(int i = 0; i < len; i++){//i是辅助
  7.                 for(int j = 1; j < len - i; j++){
  8.                         if(input[j-1] > input[j]){
  9.                                 swap(input[j-1], input[j]);
  10.                         }
  11.                 }
  12.         }
  13. }

  14. int main(void){
  15.         vector<int> temp;
  16.         int number;
  17.         while(cin >> number){
  18.                 temp.push_back(number);
  19.         }
  20.         quick_sort(temp);
  21.         for(auto cha : temp) cout << cha << " ";
  22.         return 0;
  23. }
复制代码



注意事项:
1.参考链接:https://mp.weixin.qq.com/s/KWf_1Ntbo3WM52JWkP70Cg
2.稳定性和内外排序的定义参考《大话数据结构》第九章


二、选择排序
参考视频:https://www.bilibili.com/video/B ... 6255878836178982189

n代表待排序的元素个数
时间复杂度:最好情况O(n^2)、最坏情况O(n^2)、平均情况O(n^2)       
空间复杂度:O(1)
稳定性:稳定
属于内排序


  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;


  4. void select_sort(vector<int>& input){
  5.         int len = input.size();
  6.         int index;
  7.         for(int i = 0; i < len-1; i++){
  8.                 index = i;
  9.                 for(int j = i+1; j < len; j++){
  10.                         if(input[j] < input[index]) index = j;
  11.                        
  12.                 }
  13.                 swap(input[i], input[index]);
  14.         }
  15. }

  16. int main(void){
  17.         vector<int> temp;
  18.         int number;
  19.         while(cin >> number){
  20.                 temp.push_back(number);
  21.         }
  22.         select_sort(temp);
  23.         for(auto cha : temp) cout << cha << " ";
  24.         return 0;
  25. }
复制代码


三、插入排序
参考视频:https://www.bilibili.com/video/B ... 5355451024347335047

n代表待排序的元素个数
时间复杂度:最好情况O(n)、最坏情况O(n^2)、平均情况O(n^2)       
空间复杂度:O(1)
稳定性:稳定
属于内排序

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;


  4. void sort(vector<int>& input){
  5.         int len = input.size();
  6.         for(int i = 1; i <len; i++){
  7.                 for(int j = i; j > 0; j--){
  8.                         if(input[j] < input[j-1]){
  9.                                 swap(input[j], input[j-1]);
  10.                         }else{
  11.                                 break;
  12.                         }
  13.                        
  14.                 }
  15.         }
  16. }

  17. int main(void){
  18.         vector<int> temp;
  19.         int number;
  20.         while(cin >> number){
  21.                 temp.push_back(number);
  22.         }
  23.         sort(temp);
  24.         for(auto cha : temp) cout << cha << " ";
  25.         return 0;
  26. }
复制代码


四、希尔排序  

参考视频:https://www.bilibili.com/video/B ... 9271654472112320607
https://www.bilibili.com/video/B ... 9243453532555366843
n代表待排序的元素个数
时间复杂度:最好情况O(n^1.3)、最坏情况O(n^2)、平均情况O(nlogn)~O(n^2)
空间复杂度: O(1)
稳定性:不稳定
属于内排序

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;


  4. void sort(vector<int>& input){
  5.         int len = input.size();
  6.         for(int gap = len/2; gap > 0; gap /=2){
  7.                 for(int i = gap; i < len; i++){
  8.                         for(int j = i; j > 0; j--){
  9.                                 if(input[j] < input[j-gap]){
  10.                                         swap(input[j], input[j-gap]);
  11.                                 }else{
  12.                                         break;
  13.                                 }
  14.                         }
  15.                 }
  16.         }
  17.        
  18. }

  19. int main(void){
  20.     vector<int> temp;
  21.     int number;
  22.     while(cin >> number){
  23.             temp.push_back(number);
  24.     }
  25.     sort(temp);
  26.     for(auto cha : temp) cout << cha << " ";
  27.     return 0;
  28. }
复制代码


五、堆排序  
参考视频:https://www.bilibili.com/video/B ... 9245800988927155643
n代表待排序的元素个数
时间复杂度:最好情况O(nlogn)、最坏情况O(nlogn)、平均情况O(nlogn)
空间复杂度: O(1)
稳定性:不稳定
属于内排序

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. void maxHeap(vector<int>&input, int n, int len){
  5.         if(n >= len) return;
  6.         int root = n, left = 2*n+1, right = 2*n+2;
  7.         if(left < len && input[left] > input[root]) root = left;
  8.         if(right < len && input[right] > input[root]) root = right;
  9.         if(root != n){
  10.                 swap(input[n], input[root]);
  11.                 maxHeap(input, root, len);
  12.         }
  13. }

  14. void build(vector<int>&input, int n){
  15.         for(int i = (n-2)/2; i >= 0; i--) maxHeap(input, i, n);
  16. }

  17. void sortHeap(vector<int>&input, int n){
  18.         build(input, n);
  19.         for(int i = n; i > 0; i--){
  20.                 swap(input[0], input[i-1]);
  21.                 maxHeap(input, 0, i-1);
  22.         }
  23. }

  24. int main(void){
  25.         vector<int> input = {3,23,1,6,24,99,35,2,5};
  26.         sortHeap(input, input.size());
  27.         for(auto cha : input) cout << cha << " ";
  28.        
  29.         return 0;
  30. }
复制代码


六、归并排序

视频参考:https://www.bilibili.com/video/B ... 4868815010429455110
代码参考:https://www.cnblogs.com/chengxiao/p/6194356.html

n代表待排序的元素个数
时间复杂度:最好情况O(nlogn)、最坏情况O(nlogn)、平均情况O(nlogn)
空间复杂度: O(n)
稳定性:稳定
属于外排序

  1. #include<iostream>
  2. #include<vector>

  3. using namespace std;


  4. void merge(vector<int>&input, int left, int mid, int right, vector<int>&temp){
  5.         int i = left;
  6.         int j = mid + 1;
  7.         int t = 0;
  8.         while(i <= mid && j <= right){
  9.                 if(input[i] <= input[j]) temp[t++] = input[i++];
  10.                 else temp[t++] = input[j++];
  11.         }
  12.         while(i <= mid) temp[t++] = input[i++];
  13.         while(j <= right) temp[t++] = input[j++];
  14.        
  15.         //将temp中的元素全部拷贝到原数组中
  16.         t = 0;
  17.         while(left <= right) input[left++] = temp[t++];
  18. }


  19. void sort(vector<int>&input, int left, int right, vector<int>&temp){
  20.         if(left >= right) return;
  21.         if(left < right){
  22.                 int mid = (right + left)/2;
  23.                 sort(input, left, mid, temp);
  24.                 sort(input, mid+1, right, temp);
  25.                 merge(input, left, mid, right, temp);
  26.         }
  27. }


  28. int main(){
  29.         vector<int> input = {1,3,5,2,6,21,234,12};
  30.         int len = input.size();
  31.         vector<int> temp(len, 0);
  32.         sort(input, 0, len-1, temp);
  33.         for(auto cha :input) cout << cha << " ";
  34.         return 0;
  35. }
复制代码




七、快速排序

快速排序感觉看代码比看教程更清楚

n代表待排序的元素个数
时间复杂度:最好情况O(nlogn)、最坏情况O(n^2)、平均情况O(nlogn)
空间复杂度: O(nlogn)~O(n)
稳定性:不稳定
属于内排序

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;


  4. void quick_sort(int left, int right, vector<int>&input){
  5.         if(right <= left) return;
  6.         int i = left, j = right;
  7.         int base = input[i];
  8.         while(i < j){
  9.                 while(i < j && input[j] >= base) j--;//这一句和下一句的顺序不能变
  10.                 while(i < j && input[i] <= base)i++;
  11.                 if(i < j) swap(input[i], input[j]);
  12.                
  13.         }
  14.         swap(input[left], input[i]);
  15.         quick_sort(left, i-1, input);
  16.         quick_sort(i+1, right, input);
  17.        
  18. }


  19. int main(void){
  20.         vector<int> input = {1, 3, 34, 54, 3,90, 34, 6, 2};
  21.         quick_sort(0, input.size()-1, input);
  22.         for(auto cha : input) cout << cha << " ";
  23.         return 0;
  24. }
复制代码


八、计数排序(桶排序的一种)
视频参考:https://www.bilibili.com/video/B ... 333.788.videocard.1

n代表待排序的元素个数, k代表数据中可能出现的不同数的个数
时间复杂度:最好情况O(n+k)、最坏情况O(n+k)、平均情况O(n+k)
空间复杂度: O(k)
稳定性:稳定
属于外排序

  1. #include <vector>
  2. #include <iostream>
  3. #include <map>
  4. #include <algorithm>

  5. using namespace std;

  6. vector<int> solution(vector<int>&input){
  7.         vector<int> res;
  8.         map<int, int> store;
  9.         for(int i = 0; i < input.size(); i++){
  10.                 store[input[i]] ++;
  11.         }
  12.         int min = *min_element(input.begin(), input.end());
  13.         int max = *max_element(input.begin(), input.end());
  14.        
  15.         for(int i = min; i<= max; i++){
  16.                 while(store.find(i) != store.end() && store[i] != 0){
  17.                         res.push_back(i);
  18.                         store[i] --;
  19.                 }
  20.         }
  21.         return res;
  22. }
  23. int main(){
  24.         vector<int> input = {1, 20, 2, 7, 8, 14, 13, 15, 15, 2};
  25.         vector<int> res = solution(input);
  26.         for(auto cha : res) cout << cha << " ";
  27.         return 0;
  28. }
复制代码



九、基数排序(桶排序的一种)
视频参考:https://www.bilibili.com/video/B ... 4452742656427140512

n代表待排序的元素个数, k代表最高位数的长度。
时间复杂度:最好情况O(n*k)、最坏情况O(n*k)、平均情况O(n*k)
空间复杂度: O(n+k)
稳定性:稳定
属于外排序

  1. #include <vector>
  2. #include <iostream>
  3. #include <map>
  4. #include <algorithm>
  5. #include <string>

  6. using namespace std;

  7. void solution(vector<int>&input, int len){
  8.         for(int i = 0; i < len; i++){
  9.                 map<int, vector<int>> count;
  10.                 int division = pow(10, i);
  11.                 for(int j = 0; j < input.size(); j++){
  12.                         int num = input[j] / division % 10;
  13.                         count[num].push_back(input[j]);
  14.                 }
  15.                 vector<int> temp;
  16.                 for(int m = 0; m < 10; m++){
  17.                         if(count.find(m) != count.end()){
  18.                                 temp.insert(temp.end(), count[m].begin(), count[m].end());
  19.                         }
  20.                 }
  21.                 input = temp;
  22.         }
  23. }
  24. int main(){
  25.         vector<int> input = {1, 20, 2, 7, 8, 14, 13, 15, 15, 2};
  26.         int len = 2;
  27.         solution(input, len);
  28.         for(auto cha : input) cout << cha << " ";
  29.         return 0;
  30. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-8 14:22:32 | 显示全部楼层
待更新
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-9 10:47:34 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-5-8 14:22
点进来之前是点进来之后是仔细一看是

还在写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-14 14:59:26 | 显示全部楼层
完结撒花
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-2 16:58:30 | 显示全部楼层
C++中vector的insert参考链接:https://blog.csdn.net/yanbao4070/article/details/80760224
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 15:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表