各种排序算法的C++实现,详细的代码和注释,并包含测试代码,可以运行
060 * Insertion sort algorithm.
061 * "a" ----> array of Comparable items.
062 * "left" ----> the left-most index of the subarray.
063 * "right" ----> the right-most index of the subarray. 064 */
065 template <typename Type>
066 void insertSort( vector<Type> &a, int left, int right )
067 {
068 for( int p=left+1; p<=right; p++ )
069 {
070 Type tmp = a[p];
071 int j;
072
073 for( j=p; j>left && tmp<a[j-1]; --j )
074 a[j] = a[j-1];
075 a[j] = tmp;
076 }
077 }
080 /**
081 * Internal quicksort method that makes recursive calls.
082 * Uses median-of-three partitioning and a cutoff of 20. 083 * "a" ----> array of Comparable items.
084 * "left" ----> the left-most index of the subarray.
085 * "right" ----> the right-most index of the subarray. 086 */
087 template <typename Type>
088 void quickSort( vector<Type> &a, int left, int right )
089 {
090 if( left+20 <= right )
091 {
092 Type pivot = median3( a, left, right );
094 // begin partitioning
095 int i = left, j = right-1;
096 for( ; ; )
097 {
098 while( a[++i] < pivot ) { }
099 while( pivot < a[--j] ) { }
101 if( i < j )
102 swap( a[i], a[j] );
103 else
104 break;
105 }
107 // Restore pivot
108 swap( a[i], a[right-1] );
110 // Sort small elements
111 quickSort( a, left, i-1 );
113 // Sort large elements
114 quickSort( a, i+1, right );
115 }
116 else
117 insertSort( a, left, right );
118 }
各种排序算法总结(C语言版)_计算机软件及应用_IT/计算机_专业资料。算法 排序...16 k 指示当前序列中最小者 6.1.5 选择排序算法实现:输入n 个数给a[1] ...
各种排序c算法1_计算机软件及应用_IT/计算机_专业资料。C语言的各种排序算法的比较各种排序算法实现 c语言(2007-05-18 21:08:27) 各种基本排序算法实现 c语言...
c中各种排序算法_工学_高等教育_教育专区。c中各种排序算法今日推荐 ... C_实现所有经典排序算法 5页 免费 C排序算法 6页 免费 c语言中的排序方法 ...
{ k=a; a=a[i+1]; a[i+1]=k; } } } } 六.选择排序法 /*算法原理:首先以一个元素为基准,从一个方向开始扫描, * 比如从左至右扫描,以A[0]为...
各种排序算法的实现_工学_高等教育_教育专区。各种常见排序算法的实现常见排序算法的实现摘 要:通过对几种常见排序算法的简介和举例说明,加深对排序算法的理解并...
讲解排序算法 C 语言实现 各种排序算法 另外连接: 另外连接:http://blog.csdn.net/fly_lb/category/368127.aspx 排序算法是一种基本并且常用的算法。由于实际工作...
各种排序方法的C语言实现_工学_高等教育_教育专区。各种排序方法的C语言实现,...=j); R[i]=temp; return i; } /*一次划分算法*/ /*i++后的 i 与...
那么交换排序的扩展算法,选择排序就很 容易得出来了,效率略比交换排序高,整体平均效率还是 N 的平方,C 语言实现如下: void select_sort(int *array, int size)...
多种排序算法实现C语言_计算机软件及应用_IT/计算机_专业资料。包括直接插入排序,冒泡,快排,堆排序,希尔排序,选择排序等排序算法是面试笔试中必定要涉及的内容,...
各种排序算法总结(C语言版)_计算机软件及应用_IT/计算机_专业资料。6.1 常见...直接插入排序算法实现: void InsertSort (int r[ ], int n ) { // 假设...
热门文档