• c++几种排序算法代码


      1 #include <iostream>
      2 #include <vector>
      3 using namespace std;
      4 
      5 //交换int
      6 void swap(int& a, int& b){
      7     int temp = a;
      8     a = b;
      9     b = temp;    
     10 }
     11 
     12 //冒泡
     13 void BulletSort(vector<int>& nums){
     14     int n = nums.size();
     15     for (int i = 0; i < nums.size()-1; i++){
     16         for (int j = 0; j < nums.size() - i -1; j++){
     17             if (nums[j] > nums[j+1]){
     18                 swap(nums[j], nums[j+1]);
     19             }
     20         }
     21     }
     22 }
     23 
     24 //快排
     25 void fastSort(vector<int>&num, int first, int last){
     26     if (first < last){
     27         int i = first;
     28         int j = last;
     29         int seed = num[i];
     30         while (i<j){
     31             while (i < j && num[j] >= seed){
     32                 j--;
     33             }
     34             if (i < j){
     35                 num[i++] = num[j];
     36             }
     37             while (i <j && num[i] <= seed){
     38                 i++;
     39             }
     40             if (i < j){
     41                 num[j--] = num[i];
     42             }
     43         }
     44         num[i] = seed;
     45         fastSort(num, first, i - 1);
     46         fastSort(num, i + 1, last);
     47     }
     48 
     49 }
     50 
     51 
     52 //插入
     53 void InsertSort(vector<int>& nums){
     54     int n = nums.size();
     55     for (int i = 0; i < n; i++){
     56         for (int j = i; j > 0; j--){
     57             if (nums[j] < nums[j - 1]){
     58                 swap(nums[j], nums[j - 1]);
     59             }
     60         }
     61     }
     62 }
     63 
     64 //归并数组
     65 void mergeArray(vector<int>&nums, int first, int mid, int last, vector<int>temp){
     66     int i = first;
     67     int j = mid + 1;
     68     int m = mid;
     69     int n = last;
     70     int k = 0;
     71     while (i <= m&& j<=n){
     72         if (nums[i] < nums[j]){
     73             temp[k++] = nums[i++];
     74         }
     75         else{
     76             temp[k++] = nums[j++];
     77         }
     78     }
     79     while (i<=m){
     80         temp[k++] = nums[i++];
     81     }
     82     while (j <= n){
     83         temp[k++] = nums[j++];
     84     }
     85     for (int i = 0; i < k; i++)
     86     {
     87         nums[first+i] = temp[i];
     88     }
     89 }
     90 
     91 //归并
     92 void mergeSort(vector<int>& nums, int first, int last, vector<int>temp){
     93     if (first<last){
     94         int mid = (first + last) / 2;
     95         mergeSort(nums, first, mid, temp);
     96         mergeSort(nums, mid + 1, last, temp);
     97         mergeArray(nums,first,mid,last,temp);
     98 
     99     }
    100 }
    101 
    102 int main()
    103 {
    104     vector<int>test;
    105     vector<int>temp(4, 0);
    106     test.push_back(2);
    107     test.push_back(4);
    108     test.push_back(1);
    109     test.push_back(3);
    110     //BulletSort(test);
    111     //fastSort(test, 0, 3);
    112     //InsertSort(test);
    113     //mergeSort(test, 0,3,temp);
    114     return 0;
    115 
    116 }

      几种算法的比较如下图所示
      
    
    
  • 相关阅读:
    楷书四大家
    什么叫同学?
    css悬浮在页面顶端
    jq给页面添加覆盖层遮罩的实例
    jQuery实现遮罩层
    jQuery实现的文字逐行向上间歇滚动效果示例
    jquery实现文字由下到上循环滚动的实例代码
    Js/Jquery获取网页屏幕可见区域高度
    本机无法访问虚拟机配置的域名
    thinkphp5+GatewayWorker+Workerman
  • 原文地址:https://www.cnblogs.com/LaplaceAkuir/p/6284900.html
Copyright © 2020-2023  润新知