The characteristics and implementation of merging and sorting [C language]

Source: Internet
Author: User

The characteristics and implementation of merging and sorting [C language]

1. Algorithm characteristics

Merge sort is an efficient and stable sorting method, which is second only to fast sorting, but is more memory-intensive.

The time complexity is the best, the worst, the average condition is O (nlog (2) n), the spatial complexity is O (n).

2. Algorithm Ideas

Divide and conquer the idea of the problem decomposition, refinement, one by one, that is, by recursion to decompose the sequence of disorder, until decomposed into a sequential sequence (when the sequence length of 1 must be ordered). Then, the ordered sequence of decomposition is merged into a new ordered sequence, and finally merged into an ordered sequence.

3. Implementation code

1#include <stdio.h>2#include <stdlib.h>3 4 //Merge sort: arr [left,mid] interval ascending [mid+1,rigth] ascending merges the data of this array in ascending order5 voidMergeintArr[],intLeftintMidintrigth)6 {7     intLen = mid-left+1;8     int* p =malloc(sizeof(int)*len);9     //moving data from the [left,mid-1] interval to the memory that P points toTen     //memcpy (prr,arr+left,l1*sizeof (int)); One      for(intI=0; i<len; i++) A     { -P[i] = arr[left+i]; -     } the     //merge p[0,len-1] arr[mid,rigth] Two pieces of data into the Arr[left,rigth] array -     inti =0; -     intj = mid+1; -     intK = left;//start from left +      while(I<len && j<=rigth) -     { +         if(P[i] <Arr[j]) A         { atarr[k++] = p[i++];  -         } -         Else -         { -arr[k++] = arr[j++];  -         } in     } -      while(I <len) to     { +arr[k++] = p[i++];  -     } the } *  $ //Bunkhouse The arr left-rigth interval orderlyPanax Notoginseng void_merge_sort (intArr[],intLeftintrigth) - { the     if(left >= rigth)//There 's only one data that would have been ordered . +     { A         return; the     } +     intMid = (left+rigth)/2; -     //Left mid mid+1 rigth $     if(Mid >Left ) $     { -_merge_sort (Arr,left,mid);//make array [left,mid] orderly -     } the     if(Rigth > mid+1) -     {Wuyi_merge_sort (arr,mid+1, rigth);//make array [mid+1,rigth] orderly the     } -Merge (Arr,left,mid,rigth);//Merging Wu } -  About voidMerge_sort (intArr[],intlen) $ { -_merge_sort (arr,0, len-1); - } -  A voidTravelintArr[],intlen) + { the      for(intI=0; i<len; i++) -     { $printf"%d", Arr[i]);  the     }     theprintf"\ n"); the } the  - intMain () in { the     intArr[] = { -, the,9,233, +, -, -,9,4, the}; the     intLen =sizeof(arr)/sizeof(arr[0]); About  the Travel (Arr,len); the Merge_sort (Arr,len); the Travel (Arr,len); +  -     return 0; the}

4. Test results

The characteristics and implementation of merging and sorting [C language]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.