Merge sort c language

 1 void mergeAdd(int arr[], int left, int mid, int right, int *temp){
2   int i = left; 3   int j = mid + 1; 4   int k = left;//临时下标 5   while (i <= mid&&j <= right){ 6     if (arr[i] < arr[j]){ 7       temp[k++] = arr[i++]; 8     }   9     else{ 10     temp[k++] = arr[j++]; 11     } 12   } 13   while (i <= mid){ 14     temp[k++] = arr[i++]; 15   } 16   while (j <= right){ 17     temp[k++] = arr[j++]; 18   } 22   memcpy(arr + left, temp + left, sizeof(int)*(right - left+1)); 23 } 24 void mergeSort(int arr[],int left,int right,int *temp){ 25   int= MID 0 ; 26 is   IF (left < right) {// merge sort using the first divided and then thinking, the efficiency of O (nlgn), the space is O (n), needs to consume twice as many space 27     MID = left + (right - left) / 2 ; 28     mergesort (ARR, left, MID, TEMP); 29     mergesort (ARR, MID + . 1 , right, TEMP); 30     mergeAdd (ARR, left, MID, right, TEMP) ; 31   }
32}

 

Guess you like

Origin www.cnblogs.com/haoxing990/p/11507191.html