标签:
参考如下链接:
http://blog.csdn.net/hguisu/article/details/7776068
#include <iostream> #include <memory.h> using namespace std; void print(int a[], int n){ for(int j= 0; j<n; j++){ cout<<a[j] <<" "; } cout<<endl; } //直接插入排序 void InsertSort(int a[], int n) { int i, j, tmp; for (i = 1; i < n; ++i) { if (a[i] < a[i - 1]) { tmp = a[i]; for (j = i - 1; j >= 0 && tmp < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = tmp; } } return; } void InsertSort1(int a[], int n) { int i, j, tmp; for (i = 1; i < n; ++i){ tmp = a[i]; for (j = i; j > 0 && tmp < a[j - 1]; --j){ a[j] = a[j - 1]; } a[j] = tmp; } return; } //希尔排序 void ShellSort(int a[], int n) { int i, j, tmp, increment; for (increment = n / 2; increment > 0; increment /= 2) { for (i = increment; i < n; ++i) { if (a[i] < a[i - increment]) { tmp = a[i]; for (j = i; j >= increment; j -= increment) { if (tmp < a[j - increment]) { a[j] = a[j - increment]; } else { break; } } a[j] = tmp; } } } return; } //选择排序 void SelectSort(int a[], int n) { int min = 0; int tmp = 0; for (int i = 0; i < n; ++i){ min = i; for (int j = i + 1; j < n; ++j){ if (a[j] < a[min]){ min = j; } } if (min != i){ tmp = a[min]; a[min] = a[i]; a[i] = tmp; } } return; } //二元选择排序 void TwoSelectSort(int a[], int n) { int i = 0; int j = 0; int min = 0; int max = 0; int tmp = 0; for (i = 0; i <= n / 2; ++i){ min = i; max = i; for (j = i + 1; j < n - i; ++j){ if (a[j] < a[min]){ min = j; continue; } if (a[j] > a[max]){ max = j; } } if (min != i){ tmp = a[min]; a[min] = a[i]; a[i] = tmp; } if (max != i){ tmp = a[max]; a[max] = a[n - i - 1]; a[n - i - 1] = tmp; } } return; } //堆排序 void PercDown(int a[], int i, int n) { int tmp = 0; int child = 2 * i; for (tmp = a[i]; 2 * i + 1 < n; i = child){ child = 2 * i; if (child != n - 1 && a[child + 1] > a[child]){ ++child; } if (tmp < a[child]){ a[i] = a[child]; } else { break; } } a[i] = tmp; return; } void HeapSort(int a[], int n) { int tmp = 0; for (int i = n / 2; i >= 0; --i){ PercDown(a, i, n); } for (int i = n - 1; i > 0; --i){ tmp = a[0]; a[0] = a[i]; a[i] = tmp; PercDown(a, 0, i); } return; } //归并排序 void Merge(int a[], int t[], int i, int m, int n) { int pos = i; int j = m + 1; int length = n - i + 1; int start = i; while (i <= m && j <= n){ if (a[i] <= a[j]){ t[pos++] = a[i++]; } else { t[pos++] = a[j++]; } } while(i <= m){ t[pos++] = a[i++]; } while(j <= n){ t[pos++] = a[j++]; } memcpy(&a[start], &t[start], length * sizeof(int)); return; } void MSort(int a[], int t[], int i, int n) { if (i < n){ int m = (i + n) / 2; MSort(a, t, i, m); MSort(a, t, m + 1, n); Merge(a, t, i, m, n); } return; } void MergeSort(int a[], int n) { int *t = new int[n]; if (NULL != t){ MSort(a, t, 0, n - 1); print(t, n); delete []t; } else { cout<<"malloc error!"<<endl; } return; } //快速排序 void Swap(int *a, int *b) { int tmp = 0; tmp = *a; *a = *b; *b = tmp; return; } int Median3(int a[], int left, int right) { int center = (left + right) / 2; if (a[left] > a[center]){ Swap(&a[left], &a[center]); } if (a[left] > a[right]){ Swap(&a[left], &a[right]); } if (a[center] > a[right]){ Swap(&a[center], &a[right]); } Swap(&a[center], &a[right - 1]); return a[right - 1]; } void Qsort(int a[], int left, int right) { int i, j; int pivot; if (left + 3 <= right){ pivot = Median3(a, left, right); i = left; j = right - 1; for (;;){ while(a[++i] < pivot){} while(a[--j] > pivot){} if (i < j){ Swap(&a[i], &a[j]); } else { break; } } Swap(&a[i], &a[right - 1]); Qsort(a, left, i -1); Qsort(a, i + 1, right); } else { InsertSort(a + left, right - left + 1); } return; } int main() { int a[9] = {34, 18, 12, 4, 1, 99, 178, 9, 37}; //InsertSort1(a, 9); //ShellSort(a, 9); //TwoSelectSort(a, 9); //HeapSort(a, 9); //MergeSort(a, 9); Qsort(a, 0, 8); print(a, 9); return 0; }
标签:
原文地址:http://www.cnblogs.com/libin2015/p/5207501.html