几种常见排序算法的实现(冒泡法,选择法,插入法,快速排序、堆排序)
2014-05-13 23:05
453 查看
#include "stdafx.h" //in the i th round ordering, j always points to the max element so far void bubble_sort(int *a, int len) { for(int i=0; i<len-1; i++) { for(int j=0; j<len-1-i; j++) { if(a[j]>a[j+1]) { swap<int>(a[j],a[j+1]); } } } } void bubble_sort2(int *a, int len) { for(int i=0; i<len-1; i++) { for(int j=len-1; j>i; j--) { if(a[i]>a[j]) { swap<int>(a[i],a[j]); } } } } /*in the i th round ordering, k always points to the min element so far. the additional variable k gives it a chance to make the swap till a whole round ordering is finished. so this algorithm should have less sawp operatons than bubble sort. */ void select_sort(int *a, int len) { int k; for (int i=0; i<len-1; i++) { k=i; for(int j=len-1; j>i; j--) { if(a[k]>a[j]) k = j; } if(k!=i) { swap<int>(a[k], a[i]); } } } // every round ordering, we choose the first element as benchmark. void quick_sort_recursion(int *a, int low, int high) { int bench=a[low]; int end=high; while(low<high) { while(low<high && a[high]>=bench) high--; a[low]=a[high]; while(low<high && a[low]<=bench) low++; a[high]=a[low]; } a[low]=bench;//now position low should equal to position high if(low>1) // there are at least two elements before position low { quick_sort_recursion(a, 0, low-1); } if(low+1<end) // there are at least two elements after position low { quick_sort_recursion(a, low+1, end); } } void quick_sort(int *a, int len) { quick_sort_recursion(a, 0, len-1); } //every round ordering, we want to insert a[i] to correct postion in an ordered array p[0]-p[i-1] //we compare a[i] with ordered array p[0]-p[i-1] from back, if a[i] is smaller, than move p[j] to p[j+1] //else insert a[i] to p[j+1] void insert_sort(int *a, int len) { int *p = new int[len]; int i,j; for(i=0; i<len; i++) { for(j=i-1; j>=0; j--) { if(a[i]<p[j]) { p[j+1] = p[j]; } else { break; } } p[j+1] = a[i]; } copy(p, p+len, a); delete [] p; } // 堆排序 // 对堆a[m, n]建大顶堆 void heap_adjust(int *a, int m, int n) { int rc = a[m]; for(int j=2*m; j<=n; j*=2) { if(j+1<=n && a[j+1]>a[j]) { j++;//如果右节点非空且较大,则取右节点 } if(rc>=a[j]) { break;//构建大顶堆完毕 } else { a[m] = a[j];//调整堆 m = j;//记录空位 } } a[m] = rc; } void heap_sort(int *a, int len) { for (int i=(len/2)-1; i>=0; i--)//构建大顶堆 { heap_adjust(a, i, len-1); } for (int i=len-1; i>0; i--) { swap<int>(a[0], a[i]);//交换堆顶元素与最后一个元素,得到此轮排序最大值 heap_adjust(a, 0, i-1);//对新的堆a[0, i-1]重新构建大顶堆 } }
相关文章推荐
- 常见排序算法总结与实现(冒泡、插入、选择、希尔、堆排序、归并、快排)
- 数据结构和算法——排序算法(冒泡法 选择法 插入法 快速法)实现
- 常见排序算法总结与实现(冒泡、插入、选择、希尔、堆排序、归并、快排)
- 插入法排序、选择排序、冒泡法、快速排序、堆排序的C实现
- 常见排序算法总结与实现(冒泡、插入、选择、希尔、堆排序、归并、快排)
- 快速排序的几种常见实现及其性能对比
- 几种常见排序算法实现
- 001-简单的java代码实现几种排序算法(插入,快速,冒泡,选择)
- 常见的几种排序算法-插入、选择、冒泡、快排、堆排等
- 常见的几种排序算法-插入、选择、冒泡、快排、堆排等
- 用Java实现几种常见的排序算法
- 八种常见排序算法:插入、冒泡、选择、希尔、归并、快排、堆排序、基数排序
- 五种经典排序算法的实现(冒泡、归并、快排、堆排序、希尔)
- java实现最基本的几种排序算法(冒泡,选择,插入)
- 用Java实现几种常见的排序算法
- Java 常见的几种排序算法-插入、选择、冒泡、快排、堆排等
- Java之美[从菜鸟到高手演变]之常见的几种排序算法-插入、选择、冒泡、快排、堆排等
- 几种常见的排序算法及实现(三)
- 排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
- 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)