• 1、快速排序


    1、debug下手写快排效率为sort的10倍

    2、release下sort略快于手写快排(相比debug均有极大提高)

    3、stable_sort 在debug下比sort快 release下比sort慢 原因不明。
     
     1 #pragma once
     2 #include "stdafx.h"
     3 #include <vector>
     4 template<typename Comparable>
     5 void insertSort(std::vector<Comparable>& v, int begin, int end)
     6 {
     7     for(int i=begin+1; i != end + 1; i++)
     8     {
     9         int j;
    10         Comparable tmp = v[i];
    11         for(j=i; j > 0 && tmp < v[j-1]; j--)
    12         {
    13             //将较大数后移一位
    14             v[j] = v[j-1];
    15         }
    16         //与循环体内最后一次j不同
    17         v[j] = tmp;
    18     }
    19 }
    20 
    21 //三数排序,并将中值放在末尾
    22 //用于选取枢纽值
    23 template<typename Comparable>
    24 const Comparable& median3(std::vector<Comparable>& v, int left, int right)
    25 {
    26     int center = (left + right)/2;
    27     if(v[center] < v[left])
    28         swap(v[center], v[left]);
    29     if(v[right] < v[left])
    30         swap(v[right], v[left]);
    31     if(v[right]  < v[center])
    32         swap(v[right], v[center]);
    33     swap(v[center], v[right-1]);
    34     return v[right-1];
    35 }
    36 template<typename Comparable>
    37 void QuickSort(std::vector<Comparable>& v, int left, int right)
    38 {
    39     if(left + 10 <= right)
    40     {
    41         Comparable pivot = median3(v, left, right);
    42         int i = left,j = right - 1;
    43         while(true)
    44         {
    45             while(v[++i] < pivot)
    46                 ;
    47             while(v[--j] > pivot)
    48                 ;
    49             if(i < j)
    50             {
    51                 swap(v[i], v[j]);
    52             }
    53             else
    54             {
    55                 break;
    56             }
    57         }
    58         swap(v[i],v[right-1]);
    59         QuickSort(v, left, i-1);
    60         QuickSort(v, i+1, right);
    61     }
    62     else
    63     {
    64         insertSort(v, left, right);
    65     }
    66 }
    快排实现

     

     1 // QuickSort.cpp : Defines the entry point for the console application.
     2 //
     3 #include "stdafx.h"
     4 #include "QuickSort.h"
     5 #include <vector> 
     6 #include <iostream>
     7 #include <time.h>
     8 #include <algorithm>
     9 using namespace std;
    10 #define MAX_RAND 829346
    11 int _tmain(int argc, _TCHAR* argv[])
    12 {
    13     vector<long> v;
    14     clock_t c_s,c_e; 
    15     for(int i =0 ; i < 2000000; i++)
    16     {
    17         v.push_back(long((double)rand()/RAND_MAX * MAX_RAND));
    18     }
    19     vector<long> v1(v),v2(v),v3(v);
    20     c_s =clock();
    21     QuickSort(v1,0,v1.size()-1);
    22     c_e =clock();
    23     cout<<"QuickSort:"<<c_e-c_s<<endl;
    24     c_s =clock();
    25     sort(v3.begin(),v3.end());
    26     c_e =clock();
    27     cout<<"sort:"<<c_e-c_s<<endl;
    28     return 0;
    29 }
    运行时间测试

     

     
    运行结果如下:
    Debug下:
    QuickSort:6562
    Sort:67766
     
    Release下:
    QuickSort:218
    Sort: 157
     
    代码下载 

    http://pan.baidu.com/s/1jHqqxnS

    当下即永恒
  • 相关阅读:
    #最小生成树,Trie#CF888G Xor-MST
    #Tarjan#洛谷 5676 [GZOI2017]小z玩游戏
    #区间dp#CF1114D Flood Fill
    #构造,二分#[AGC006B] [AGC006D] Median Pyramid
    #0/1分数规划#AT1807 食塩水
    #笛卡尔树#洛谷 3793 由乃救爷爷
    #同余最短路#洛谷 2371 [国家集训队]墨墨的等式
    awk命令使用
    k8s快速删除所有退出的pod
    ratticdb密码管理工具安装使用
  • 原文地址:https://www.cnblogs.com/HellcNQB/p/5069815.html
Copyright © 2020-2023  润新知