INSERTION-SORT(A) for j=2 to A.length key=A[j] // insert A[j] into the sorted sequence A[1..j-1]. i=j-1 while i>0 and A[i] > key A[i+1]=A[i] i=i-1 A[i+1]=key
void insertion_sort(int array[], int first, int last) { int i,j; int key; for (j = first+1; j<=last;i++) { key = array[j]; i=j-1; while((i>=first) && (array[i] > key)) { array[i+1] = array[i]; i--; } array[i+1] = key; } }
#include <iterator> template<typename biIter> void insertion_sort(biIter begin, biIter end) { typedef typename std::iterator_traits<biIter>::value_type value_type; biIter bond = begin; std::advance(bond, 1); for( ; bond!=end; std::advance(bond, 1)) { value_type key = *bond; biIter ins = bond; biIter pre = ins; std::advance(pre, -1); while(ins!=begin && *pre>key) { *ins = *pre; std::advance(ins, -1); std::advance(pre, -1); } *ins = key; } }
知识:
std::advance
template <class InputIterator, class Distance>void advance (InputIterator& it, Distance n);Advance iteratorAdvances the iterator it by n element positions.
INSERTION-SORT(A)
for j=2 to A.length
key=A[j]
// insert A[j] into the sorted sequence A[1..j-1].
i=j-1
while i>0 and A[i] < key
A[i+1]=A[i]
i=i-1
A[i+1]=key
SELECTION-SORT(A) n=A.length for j=1 to n-1 smallest = j for i=j+1 to n if A[j] < A[smallest] smallest=i exchange A[j] with A[smallest]
void selection_sort(int *a, int len) { register int i, j, min, t; for(i = 0; i < len - 1; i ++) { min = i; //查找最小值 for(j = i + 1; j < len; j ++) if(a[min] > a[j]) min = j; //交换 if(min != i) { t = a[min]; a[min] = a[i]; a[i] = t; } } }
分解:分解待排序的n个元素的序列成各具n/2个元素的两个子序列。
解决:使用归并排序递归地排序两个子序列。
合并:合并两个已排序的子序列以产生已排序的答案。
归并排序算法的关键操作是“合并”步骤中两个已排序虚拟的合并。我们通过调用一个辅助过程MERGE(A,p,q,r)来完成合并。
伪代码:
MERGE(A,p,q,r) n1=q-p+1 n2=r-q let L[1..n1+1] and R[1..n2+1] be new arrays for i=1 to n1 L[i] = A[p+i-1] for j=1 to n2 R[j]=A[q+j] L[n1+1]=INF R[n2+1]=INF i=1 j=1 for k=p to r if L[i]<=R[j] A[k]=L[i] i=i+1 else A[k]=R[j] j=j+1
MERGE-SORT(A,p,r) if p<r q=[(p+r)/2] MERGE-SORT(A,p,q) MERGE-SORT(A,q+1,r) MERGE(A,p,q,r)
void Merge(int a[], int b[], int low, int mid, int high) { int k = low; int begin1 = low; int end1 = mid; int begin2 = mid + 1; int end2 = high; while(k <= high ) { if(begin1 > end1) b[k++] = a[begin2++]; else if(begin2 > end2) b[k++] = a[begin1++]; else { if(a[begin1] <= a[begin2]) b[k++] = a[begin1++]; else b[k++] = a[begin2++]; } } } void MergePass(int a[], int b[], int seg, int size) { int seg_start_ind = 0; while(seg_start_ind <= size - 2 * seg) { Merge(a, b, seg_start_ind, seg_start_ind + seg - 1, seg_start_ind + seg * 2 - 1); seg_start_ind += 2 * seg; } //#如果一段是正好可歸併的數量而另一段則少於正好可歸併的數量#% if(seg_start_ind + seg < size) Merge(a, b, seg_start_ind, seg_start_ind + seg - 1, size - 1); else for(int j = seg_start_ind; j < size; j++) //#如果只剩下一段或者更少的數量#% b[j] = a[j]; } void MergeSort(int a[], int size) { int* temp = new int[size]; int seg = 1; while(seg < size) { MergePass(a, temp, seg, size); seg += seg; MergePass(temp, a, seg, size); seg += seg; } delete [] temp; } int main() { int a[] = {3, 5, 3, 6, 4, 7, 5, 7, 4}; MergeSort(a, sizeof(a) / sizeof(*a)); //#輸出#% for(int i = 0; i < sizeof(a) / sizeof(*a); i++) cout << a[i] << ' '; cout << endl; return 0; }