排序算法总结(C++)

来源:互联网 发布:发票在线制作软件 编辑:程序博客网 时间:2024/06/08 08:31

一、插入排序

(1)最差时间复杂度 Θ(n²)
(2)稳定性 稳定
(3)特点和适用情况
(4)编程实现

 template <class Elem>void inssort(Elem A[], int n) {  for (int i=1; i<n; i++)    for (int j=i; (j>0) &&         (A[j]< A[j-1])); j--)      swap(A, j, j-1);}
// Modified version of Insertion Sorttemplate <class Elem>void inssort2(Elem A[], int n, int from, int incr) {  for (int i= from + incr; i<n; i+=incr)        for (int j=i; (j> from &&                A[j]< A[j-incr]); j-=incr)        swap(A, j, j-incr);}

二、冒泡排序

(1)最差时间复杂度 Θ(n²)
(2)稳定性 稳定
(3)特点和适用情况
(4)编程实现

 template <class Elem>void bubsort(Elem A[], int n) {  for (int i=0; i<n-1; i++)    for (int j=n-1; j>i; j--)      if (A[j]< A[j-1])        swap(A, j, j-1);}

三、选择排序

(1)最差时间复杂度 Θ(n²)
(2)稳定性 不稳定
(3)特点和适用情况
(4)编程实现

template <class Elem>void selsort(Elem A[], int n) {  for (int i=0; i<n-1; i++) {  // select the minial values in [i,n-1]    int lowindex = i; // Remember its index    for (int j=n-1; j>i; j--) // Find least      if (A[j]< A[lowindex])        lowindex = j; // Put it in place    swap(A, i, lowindex);  }}

插入排序、冒泡排序、选择排序时间复杂度总结

四、希尔排序

(1)最差时间复杂度 Θ(n^1.5)
(2)稳定性 不稳定
(3)特点和适用情况
(4)编程实现

template <class Elem, class Comp>void shellsort(Elem A[], int n) { // Shellsort  for (int i=n/2; i>=1; i/=2)  // For each incr    for (int j=0; j<i; j++)   // Sort sublists      inssort2<Elem,Comp>(A, n, j, i);   }

五、归并排序

(1)最差时间复杂度 Θ(n log n)
(2)稳定性 稳定
(3)特点和适用情况
(4)编程实现

List mergesort(List inlist) {  if (inlist.length() <= 1)return inlist;  List l1 = half of the items from inlist;  List l2 = other half of items from inlist;  return merge(mergesort(l1),                mergesort(l2));}

六、快速排序

(1)最差时间复杂度 Θ(n²)
(2)稳定性 不稳定
(3)特点和适用情况
(4)编程实现

template <class Elem, class Comp>void qsort(Elem A[], int i, int j) {  if (j <= i) return;     // List too small  int pivotindex = findpivot(A, i, j);  swap(A, pivotindex, j);  // Put pivot at end  // k will be first position on right side  int k =      partition<Elem,Comp>(A, i, j, A[j]);  swap(A, k, j);         // Put pivot in place  qsort<Elem,Comp>(A, i, k-1);  qsort<Elem,Comp>(A, k+1, j);}

七、堆排序

(1)最差时间复杂度 Θ(n log n)
(2)稳定性 不稳定
(3)特点和适用情况
(4)编程实现

template<class Elem> class maxheap{private:  Elem* Heap;   // Pointer to the heap array  int size;     // Maximum size of the heap  int n;        // Number of elems now in heap  public:  maxheap(Elem* h, int num, int max)  {Heap = h; n = num; size = max; buildHeap();}    

八、分配排序

(1)最差时间复杂度 Θ(n + MaxKeyValue)
(2)稳定性 稳定
(3)特点和适用情况
(4)编程实现

for (i=0; i<n; i++)     B[A[i]] = A[i];    

九、基数排序

(1)最差时间复杂度
Θ((n + r)k) (k is the digit count of a keyword, r is the radix )
(2)稳定性 稳定
(3)特点和适用情况
(4)编程实现

template<typename E,typename getKey>   void radix(E A[],E B[]),int n,int k,int r,int cnt[]){//cnt[i] stores number of records in bin[i]int j;for(int i=0,rtoi=1;i<k;i++,rtoi*=r){  for(j=0;j<r;j++) cnt[j]=0;  for (j=0;j<n;j++) cnt[(getKey::key(A[j]/rtoi)%r]++;  for (j=1;j<r;j++) cnt[j]=cnt[j-1]+cnt[j];  for (j=n-1;j>=0;j--)  B[--cnt[(getKey::key(A[j]/rtoi)%r]]=A[j];  for (j=0;j<n;j++) A[j]=B[j];  } }
0 0
原创粉丝点击