• 几种快速排序算法实现


    快速排序是一种分治的递归算法,是在实践中最快的已知算法,平均运行时间为O(N logN)--logN每次需要随机选值(枢纽值)及左右部分枢纽值 --N左右快速排序。该算法有非常精炼和高度优化得内部循环。最坏性能为(N²)--每次选取随机数都是最大值或者最小值。

    【1】普通递归实现  --《剑指offer》何海涛

     1 int Partition(int data[],int length,int start,int end)
     2 
     3 {
     4 
     5   if(data == nullptr || length <= 0 || start < 0 ||end >= length)
     6 
     7     throw new std::exception("Invalid Parameters");
     8 
     9 
    10 
    11   int index = RandomInRange(start,end);  //随机选择数组中一个位置值
    12 
    13   Swap(&data[index],&data[end]);     //将选出值放到末尾,为了从数组第0个值到倒数第二个值逐个和选出值比较大小(为啥不每次保留选出值,而是要交换到末尾去??)
    14 
    15 
    16 
    17   int small = start - 1;    
    18 
    19   for(index = start;index < end; ++index)  //遍历数组
    20 
    21   {
    22 
    23     if(data[index] < data[end])  //将比选出值小的值逐个排到从0开始的位置上
    24 
    25     {
    26 
    27       ++small;       
    28 
    29       if(small != index)    //如果从0开始的值一直比选出值小,不做操作【例:值小,small=index=0,1,2,3,4。data[5]>data[end],此时index=5,small=4,data[6]<data[end],此时index=6,small=4,交换data[6]和data[4+1=5]】
    30 
    31         Swap(&data[index],&data[small])
    32 
    33     }
    34 
    35   }
    36 
    37   ++small;
    38 
    39   Swap(&data[small],&data[end]);  //挑选出来的随机值被放入中间位置,此时左边值都比它小,右边值都比他大
    40   
    41 
    42   return small;
    43 
    44 }
    45 
    46 void QuickSort(int data[],int length,int start,int end)
    47 
    48 {
    49 
    50   if(start == end)
    51 
    52     return;
    53 
    54 
    55   int index = Partition(data,length,start,end);
    56 
    57   if(index > start)
    58 
    59     QuickSort(data,length,start,index-1);
    60   if(index < end)
    61 
    62     QuickSort(data,length,index +1,end);
    63 }

    【2】使用C++类和vector容器      --《算法之美》 左飞

     1 #ifndef QUICKSORT_H
     2 
     3 #define QUICKSORT_H
     4 
     5 #include<vector>
     6 
     7 using namespace std;
     8 
     9 class QuickSort
    10 
    11 {
    12 
    13 private:
    14 
    15   int len;
    16 
    17   vector<int> list;
    18 
    19 public:
    20 
    21   QuickSort(vector<int>_list,_len);
    22 
    23   void quick_sort(int,int);
    24 
    25   void swap(int,int);
    26 
    27   void out();
    28 
    29 };
    30 
    31 #endif
    32 
    33 
    34 
    35 #include "QuickSort.h"
    36 
    37 #include <iostream>
    38 
    39 using namespace std;
    40 
    41 
    42 
    43 QuickSort::QuickSort(vector<int> _list,int _len)
    44 
    45 {
    46 
    47   for(int i=0;i<_len;i++)
    48 
    49     list.push_back(_list[i]);
    50 
    51   this->len = _len;
    52 
    53 }
    54 
    55 void QuickSort::quick_sort(int left,int right)
    56 
    57 {
    58 
    59   int i=left;
    60 
    61   int j=right;
    62 
    63   int pivot = list[left];
    64 
    65   while(i<j)
    66 
    67   {
    68 
    69     while(i<j && list[j] >= pivot) j--;
    70 
    71     if(i<j) swap(i,j);
    72 
    73     while(i<j && list[i]<=pivot) i++;
    74 
    75     if(i<j) swap(i,j);
    76 
    77   }
    78 
    79 
    80 
    81   if(j != left) quick_sort(left,i-1);
    82 
    83   if(j != right) quick_sort(j+1,right);
    84 
    85 }
    86 
    87 
    88 
    89 void QuickSort::swap(int i,int j)
    90 
    91 {
    92 
    93   int temp=list[i];
    94 
    95   list[i] = list[j];
    96 
    97   list[j]=temp;
    98 
    99 }

    【3】使用模板和容器实现    --《C++并发编程》Anthony Williams

     1 template<typename T>
     2 
     3 std::list<T> sequential_quick_sort(std::list<T> input)
     4 
     5 {
     6 
     7   if(input.empty())
     8 
     9     return input;
    10 
    11   std::list<T> result;
    12 
    13   result.splice(result.begin(),input,input.begin());  //splice函数将数据传入容器中
    14 
    15   T const& pivot = *result.begin();
    16 
    17 
    18 
    19   auto divide_point=std::partition(input.begin(),input,input.end(),
    20 
    21         [&](T const& t){return t<pivot;});    //C++11新增,lambda函数
    22 
    23   std::list<T> lower_part;
    24 
    25   lower_part.splice(lower_part.end(),input,input.begin(),divide_point);
    26 
    27   auto new_lower(sequential_quick_sort(std::move(lower_part)));
    28 
    29   auto new_higher(seuqential_quick_sort(std::move(input)));
    30 
    31   result.splice(result.end(),new_higher);
    32 
    33   result.splice(resutlt.begina(),new_lower);
    34 
    35   return result;
    36 
    37 }

    【4】使用模板容器future并行快速排序    --《C++并发编程》Anthony Williams

     1 template<typename T>
     2 std::list<T> paraller_quick_sort(std::list<T> input)
     3 {
     4   if(input.empty())
     5 
     6     return input;
     7 
     8   std::list<T> result;
     9 
    10   result.splice(result.begin(),input,input.begin());
    11 
    12   T const& pivot = *result.begin();
    13 
    14   auto divide_point=std::partition(input.begin(),input.end(),
    15 
    16       [&] (T const& t){return t<pivot;});
    17 
    18   std::list<T> lower_part;
    19 
    20   lower_part.splice(lower_part.end(),input,input.begin(),divide_point);
    21 
    22   std::future<std::list<T>> new_lower(std::async(&paraller_quicke_sort<T>,std::move(lower_part)));
    23 
    24   auto new_higher(partition(std::move(input)));
    25 
    26 
    27 
    28   result.splice(result.end(),new_higher);
    29 
    30   result.splice(result.begin(),new_lower.get());
    31 
    32   return result;
    33 }
  • 相关阅读:
    Asp.net2.0页面执行顺序
    [转帖]常用的SQL语句
    [转帖]黑客技术经典问题FAQ
    面试的一些心得
    较全的正则表达式
    很好的创业建议
    [转帖]如何让菜单项与工具栏按钮对应
    源码下载网站
    [转帖]一段测试代码
    GOF设计模式趣解(23种设计模式) <转自百度空间>
  • 原文地址:https://www.cnblogs.com/tangtangde12580/p/8309341.html
Copyright © 2020-2023  润新知