A one-day study of data structure merge sort

Source: Internet
Author: User

Merge several ordered sequences 22 until all pending records are in an ordered sequence.

1, two-way merge sort

For any given length of n-pending sequence, where N records are each an ordered sequence (a single record must be ordered), and then merge the adjacent two sequences, forming a new ordered sequence, one at a time until it is merged into an ordered sequence.

  • The sequence of two sequential sequences is merged, and the sequential sequence of the array arr is merged into the array arr1. Begin is the first record subscript for the first ordered sequence, middle is the last record subscript for the first ordered sequence, Middle+1 is the first record subscript for the second ordered sequence, and end is the last record subscript for the second ordered sequence.
  • void_mergesort (intArr[],intArr1[],intBeginintMiddle,intend) {    inti = Begin,j=middle+1, k=begin;  while(I<=middle && j<=end) {        if(arr[i]<=Arr[j]) arr1[k+ +] = arr[i++]; Elsearr1[k+ +] = arr[j++]; }       while(i<=middle) arr1[k+ +] = arr[i++];  while(j<=end) Arr1[k+ +] = arr[j++];}
  • The sequence to be sorted is divided into sequences, in which the sequence to be sorted is a sequence from the subscript begin to end of an array in arr, which is stored in the array arr1 by the merged ordered sequence. So the sequence that begins at the end of the arr1 by memcpy is copied into the array arr.
  • voidMergeSort (intArr[],intArr1[],intBeginintend) {    intMiddle; inti; if(Begin = =end) Arr1[begin]=Arr[begin]; Else{Middle= (begin + END)/2;        MergeSort (Arr,arr1,begin,middle); MergeSort (Arr,arr1,middle+1, end);        _mergesort (Arr,arr1,begin,middle,end); memcpy (&arr[begin],&arr1[begin], (end-begin+1)*sizeof(int)); }}
  • Test
  • intMain () {inti; intarr[Ten] = { $,7,8, the, -, A,767, +,823, +}; int*ARR1 = (int*)malloc(sizeof(arr)); MergeSort (ARR,ARR1,0,9);  for(i=0;i<Ten; i++) printf ("%d", Arr[i]);  Free(ARR1); return 0;}

2. Summary

Time complexity: O (nlog2n), is a stable sort.

Space complexity O (n), which requires the same storage space as the original sequence during storage.

A one-day study of data structure merge sort

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.