#include <stdio.h>#include<math.h>voidMain () {intArray[] = {1,212, *,1,456,12376,167, A,7523, in,634}; MergeSort (Array,0,Ten); for(inti =0; I < One; i++) {printf ("%d\n", Array[i]); }}voidMergeSort (int* Array,intStartintend) {//start End is indexedif(Start <end) { intMiddle = ceil (start + end)/2); MergeSort (array, start, middle); MergeSort (array, Middle+1, end); Merge (array, start, middle, end); }}voidMergeint* Array,intStartintMiddle,intend) {//start middle end are indexedintLeftlength = Middle-start +1; intRightlength = End-Middle; int* left = (int*)malloc(sizeof(int) *leftlength); int* right = (int*)malloc(sizeof(int) *rightlength); inti =0; intj =0; intLeftstart =start; intRightStart = middle +1; for(; i < leftlength; i++) {Left[i]=Array[leftstart]; ++Leftstart; } for(; J < Rightlength; J + +) {Right[j]=Array[rightstart]; ++RightStart; } I=0; J=0; for(intn = start; n <= End; n++ ) { if((I<leftlength && left[i] <= right[j]) | | j>=rightlength) {Array[n]=Left[i]; I++; } Else if((J<rightlength && right[j] <= left[i]) | | i>=leftlength) {Array[n]=Right[j]; J++; } } Free(left); Free(right);}
C Novice, Forgive me
The three basic algorithms for divide and conquer are:
1. If the problem is decomposed, it is decomposed into n problems, and each small problem is a small-scale instance of the total problem
2. Solving small problems
3. Merge the solution, the final solution of the composition problem.
TODO Write the analysis process
Merging sorting based on divide-and-conquer algorithm