排序大汇总

来源:互联网 发布:淘宝买港版苹果手机 编辑:程序博客网 时间:2024/06/05 02:19

1.归并排序

void merge(int *result, int begin, int mid, int end, int *temp){int k = begin;int i = begin;int j = mid + 1;while (i <= mid&&j <= end){temp[k++] = result[i] < result[j] ? result[i++] : result[j++];}while (i <= mid)temp[k++] = result[i++];while (j <= end)temp[k++] = result[j++];for (int t = begin; t <= end; t++)result[t] = temp[t];}void mergeSort(int *result, int begin, int end, int *temp){if (begin < end){int mid = (end - begin) / 2 + begin;mergeSort(result, begin, mid, temp);mergeSort(result, mid + 1, end, temp);merge(result, begin, mid, end, temp);}}

2.快速排序

int partition(int *result, int begin, int end){int temp = result[begin];int low = begin;int high = end;while (low < high){while (low < high&&result[high] > temp)high--;if (low < high)result[low++] = result[high];while (low < high&&result[low] < temp)low++;if (low < high)result[high--] = result[low];}result[low] = temp;return low;}void quickSort(int *result, int begin, int end){if (begin < end){int k = partition(result, begin, end);quickSort(result, begin, k - 1);quickSort(result, k + 1, end);}}


3.插入排序

void insertSort(int *a, int length){for (int i = 1; i < length; i++){int temp = a[i];int j = i - 1;while (j >= 0 && a[j] > temp){a[j + 1] = a[j];j--;}a[j + 1] = temp;}}


4.冒泡排序

void bubbleSort(int *a, int length){for (int i = length - 1; i >= 0; i--){for (int j = 0; j < i; j++){if (a[j]>a[j + 1]){//swap(a[j], a[j + 1]);int temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}}}

5.希尔排序

void shellSort(int *a, int length){for (int i = length / 2; i >= 1; i = i / 2){for (int j = i; j < length; j++){int k = j - i;int temp = a[j];while (k >= 0 && a[k] > temp){a[k + i] = a[k];k -= i;}a[k + i] = temp;}}}

6.堆排序

void heapAdjust(int *a, int k, int n){int lhc, rhc;int index = k;while (k < n / 2){lhc = 2 * k + 1;rhc = lhc + 1;if (lhc<n&&a[lhc]>a[index]){index = lhc;}if (rhc<n&&a[rhc]>a[index]){index = rhc;}if (index == k){break;}else {//swap(a[k], a[index]);int temp = a[k];a[k] = a[index];a[index] = temp;k = index;}}}void heapSort(int *a, int n){for (int i = n / 2 - 1; i >= 0; i--){heapAdjust(a, i, n);}for (int i = n - 1; i >= 1; i--){//swap(a[i], a[0]);int temp = a[i];a[i] = a[0];a[0] = temp;heapAdjust(a, 0, i);}}

7.测试程序

int main(void){srand(time(NULL));const int size = 10000000;//int a[9] = { 64 ,45 ,76, 28 ,74 ,38, 88 ,92, 34 };clock_t start, end;int *a = new int[size];int *b = new int[size];int *c = new int[size];int *temp = new int[size];for (int i = 0; i < size; i++){c[i] = b[i] = a[i] = rand() % 2534541;//cout << a[i] << " ";}//cout << endl;start = clock();//mergeSort(c, 0, size - 1,temp);//priority_queue<int> que(c, c + size);heapSort(c, size);//bubbleSort(c, size);//sort(c, c + size);//stable_sort(c, c + size);//qsort(a, size, sizeof(int), cmp);end = clock();cout << "heapSort:" << end - start << " ms" << endl;start = clock();//mergeSort(a, 0, size - 1, temp);//insertSort(a, size);//bubbleSort(a, size);shellSort(a, size);end = clock();cout << "shellSort:" << end - start << " ms" << endl;start = clock();quickSort(b, 0, size - 1);end = clock();cout << "quickSort:" << end - start << " ms" << endl;return 0;}


总体来说,快速排序高效很多,注意,不要使用swap函数,特别是堆排序,可能是函数调用太多导致效率太低。

0 0
原创粉丝点击