排序1
来源:互联网 发布:开源自动收录网站源码 编辑:程序博客网 时间:2024/05/22 02:51
1.冒泡排序
倒序,正序优化,正序
template <class Elem, class Comp> void bubsort(Elem A[], int n) { // Bubble Sort for (int i=0; i<n-1; i++) // Bubble up i'th record for (int j=n-1; j>i; j--) if (Comp::lt(A[j], A[j-1])) swap(A, j, j-1); } // int lowindex; // for (int i = 0; i<n - 1; i++) { // Select i'th record // lowindex = 0; // for (int j = 1; j<n - i ; j++) { // Find the least value // if (Comp::gt(A[j], A[lowindex])) { // lowindex = j; // Put it in place // } // } // swap(A, n-i-1, lowindex);//不能用didSwap // } //} template <class Elem, class Comp> void bubsort2(Elem A[], int n) { // Bubble Sort for (int i = 0; i<n - 1; i++) { for (int j = 0; j<n - i - 1; j++) { if (A[j] > A[j + 1]) swap(A, j, j + 1); } } }
2.选择排序(冒泡排序改善)
template <class Elem, class Comp> void selsort(Elem A[], int n) { // Selection Sort bool didSwap;int lowindex; for (int i = 0; i<n - 1; i++) { // Select i'th record didSwap = false; lowindex = i; // Remember its index for (int j = n - 1; j>i; j--) // Find the least value if (Comp::lt(A[j], A[lowindex])) { lowindex = j; // Put it in place didSwap = true; } if (didSwap == false)continue; swap(A, i, lowindex); } } //template <class Elem, class Comp> //void selsort3(Elem A[], int n) { // Selection Sort // bool didSwap;int lowindex; // for (int i = 0; i<n - 1; i++) { // select i'th record // didSwap = false; // lowindex = i; // remember its index // for (int j = i + 1; j<n; j++) { // find the least value // if (Comp::lt(A[j], A[lowindex])) { // lowindex = j; // put it in place // didSwap = true; // } // } // if (didSwap == false)continue; // swap(A, i, lowindex); // } //} static void bubble_sort3(int unsorted[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (unsorted[i] > unsorted[j]) //只能用i { int temp = unsorted[i]; unsorted[i] = unsorted[j]; unsorted[j] = temp; } } } }
3.插入排序
template <class elem, class comp> void inssort(elem a[], int n) { // insertion sort for (int i=0; i<n-1; i++) // insert i'th record for (int j=i+1; (j>0) && (comp::lt(a[j], a[j-1])); j--) swap(a, j,j-1); } template <class Elem, class Comp> void inssort(Elem A[], int n) { // Inserttion Sort for (int i = 1; i < n; i++) //从第2个数据开始插入 { int j = 0; while (j < i && A[j] <= A[i]) //寻找插入的位置 j++; if (j < i) //i位置之前,有比pDataArray[i]大的数,则进行挪动和插入 { int k = i; int temp = A[i]; while (k > j) //挪动位置 { A[k] = A[k - 1]; k--; } A[k] = temp; //插入 } } }
参考来自
4.希尔排序
a)将序列分成子序列,分别对子序列排序
b)再用插入排序完成最后排序工作
// Modified version of Insertion Sort template <class Elem, class Comp> 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 && Comp::lt(A[j],A[j-incr]); j-=incr) swap(A, j, j-incr); } 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); } // Modified version of Insertion Sort for varying increments //template <class Elem, class Comp> //void inssort2(Elem A[], int n, int incr) { // for (int i=incr; i<n; i+=incr) // for (int j=i; // (j>=incr) && (Comp::lt(A[j], A[j-incr])); j-=incr) // swap(A, j, j-incr); //} // //template <class Elem, class Comp> //void shellsort(Elem A[], int n) { // Shellsort // for (int i=n/2; i>2; i/=2) // For each increment // for (int j=0; j<i; j++) // Sort each sublist // inssort2<Elem,Comp>(&A[j], n-j, i); // inssort2<Elem,Comp>(A, n, 1); //}
a)
innsort(A,16,0,4)
innsort(A,16,1,4)
innsort(A,16,2,4)
innsort(A,16,3,4)
b)
innsort(A,16,0,4)执行过程
0 0
- 排序1 冒泡排序
- 排序 : 1 交换排序
- 排序 : 1 交换排序
- 排序(1) -- 冒泡排序
- 排序 - 归并排序 [1]
- 排序1-冒泡排序
- 排序1-选择排序
- 09-排序1 排序
- 排序(1) 初级排序
- 排序1-冒泡排序
- 排序1:冒泡排序
- 排序-1-冒泡排序
- 排序 1 --选择排序
- 排序 - 插入排序 [1 - 直接插入排序]
- 排序(1)--插入排序和交换排序
- 排序1
- 排序1
- 排序1
- 颦颦停
- JZOJ4684.卡牌游戏
- bzoj 2049 SDOI 2008 Cave 洞穴勘测 [Link-Cut Tree]
- 51nod 1586 约数和
- 平安夜的思念
- 排序1
- Noip提高组模拟【2016.8.17】
- 借记/贷记交易处理流程
- Android开发之APP直播简介
- 解决Eclipse闪退问题的几种方案总结
- 【NOIP2016模拟赛四】小结
- 大数据_Kafka_搭建Kafka伪集群(本地集群)
- 将Datatable一分为二
- #android#activity