标签:href name com 排序 new turn arc ret oid
https://www.cnblogs.com/kkun/archive/2011/11/23/2260271.html
#include<iostream> #include <stdio.h> #include <stack> using namespace std; void merge(int *a, int begin, int mid, int end, int * sorted_arr) { int i = begin; int j = mid; int k = 0; while (i < mid && j < end) { if (a[i] < a[j]) { sorted_arr[k] = a[i]; k++; i++; } else { sorted_arr[k] = a[j]; k++; j++; } } while (i < mid) { sorted_arr[k] = a[i]; k++; i++; } while (j < end) { sorted_arr[k] = a[j]; k++; j++; } for (int v =0; v < k; ++v) { a[begin +v] = sorted_arr[v]; } } void merge_sort(int *a, int begin, int end, int * sorted_arr) { if (begin +1 < end) { int mid = (begin + end) / 2; merge_sort(a, begin, mid, sorted_arr); //假设左半部分已完成排序 merge_sort(a, mid, end, sorted_arr); //假设右半部分已完成排序 merge(a, begin, mid, end, sorted_arr); // 归并核心 merge } } int main() { int a[8] = {4, 2, 6, 7, 9, 5, 1, 3}; int length = sizeof(a) / sizeof(a[0]); int* sorted_arr = new int[length]; merge_sort(a, 0, length, sorted_arr); for(int i=0; i < 8; i++) { cout << a[i] << " " << endl; } return 0; }
标签:href name com 排序 new turn arc ret oid
原文地址:https://www.cnblogs.com/pengwang52/p/12551700.html