排序算法总结(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
- 排序算法总结(C++)
- 基本排序算法总结(C/C++)
- c排序算法总结
- c排序算法总结
- c排序算法总结
- c排序算法总结
- 排序算法总结(C++)
- 各种排序算法总结(C语言)
- 排序算法(C实现)------- 总结
- 各种排序算法总结(Python,C++)
- 简单排序算法总结(C语言版)
- c语言排序算法总结
- c/c++排序算法总结
- C语言排序算法总结
- 常见排序算法总结(C++)
- c语言排序算法总结
- c语言排序算法总结
- c语言排序算法总结
- 关于在g++编译C++过程中调用移动构造函数
- nodejs的第二个小程序
- 图像是怎么转换为JPEG图像的
- 传统产业、传统企业为什么要进行互联网转型?
- HDU3450-Counting Sequences
- 排序算法总结(C++)
- poi导出excel例子
- ListView列表
- 打造CommonAdapter
- Java面向对象-Integer与int的区别
- List的contains()和remove()方法探讨
- Spring Boot 启动加载数据 CommandLineRunner
- Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'
- OC关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)