Algorithm-merge sort

Source: Internet
Author: User

Merge sort is an effective sorting algorithm based on merging operation, and the algorithm mainly uses a very typical application of divide-and-conquer method (Divide and Conquer). The algorithm complexity of the merge sort is O (N*logn), the extra space required is related to the length N of the array, and the simplest way to achieve merge sorting is to re-consolidate the two arrays into the third array. Usually for an array we sort the first half and then wake up the second half to sort it out in-place, but we need an extra space to store the array. Suppose that there are 8 elements in the data, first divided into four groups for comparison, then divided into two groups to compare, and finally divided into a group to compare, so that the derivation of two methods, one is the top-down merge sort, one is the bottom-up of the merge sort.

top-down merge sort

The implementation is relatively simple, but the merge is a little more troublesome:

`-(void) MergeSort: (Nsmutablearray *) arr   lowindex: (nsinteger) Low highindex: (Nsinteger) high{    if (high<= Low) {        return;    }    Nsinteger mid=low+ (high-low)/2;    [Self Mergesort:arr lowindex:low highindex:mid];//left half sort    [self Mergesort:arr lowindex:mid+1 highindex:high];// Right half sort    [self Merge:arr lowindex:low highindex:high midindex:mid];}`

The merge operation is the comparison of the array:

`-(void) Merge: (Nsmutablearray *) DataSource  lowindex: (nsinteger) Low highindex: (Nsinteger) High  Midindex: ( Nsinteger) mid{    nsinteger i=low,j=mid+1;        for (Nsinteger K=low; k<=high; k++) {        self.temparr[k]=datasource[k];    }    for (Nsinteger K=low; k<=high; k++) {        ///Left element already finished, take right half element        if (i>mid) {            datasource[k]= self.temparr[j ++];        }        The element on the right has been taken out, taking the left element        else if (j>high) {            datasource[k]= self.temparr[i++];        }        If the value of index j is large, then take the left value        else if ([Self.temparr[j] integervalue]<[self.temparr[i] integervalue]) {            Datasource[k]=self.temparr[j++];        }                else{            datasource[k]=self.temparr[i++];}}    }`

When merging, you need to assign a value to the temporary array:

`        Mysort  *sort=[[mysort Alloc]init];        Nsmutablearray *temparr=[[nsmutablearray alloc]initwithcapacity:10];        for (Nsinteger i=0; i<8; i++) {            [Temparr addobject:[nsnull null]];        }        Nsmutablearray *arr=[[nsmutablearray alloc]initwithcapacity:10];        [Arr addobject:@ "];        [Arr addobject:@ "3"];        [Arr addobject:@ "2"];        [Arr addobject:@ "];        [Arr addobject:@ "];        [Arr addobject:@ "];        [Arr addobject:@ "4"];        [Arr addobject:@ "];        Sort.temparr=temparr;        [Sort Mergesort:arr lowindex:0 highindex:arr.count-1];        for (Nsinteger i=0; I<[arr count]; i++) {            NSLog (@ "Value:%@", Arr[i]);        }        NSLog (@ "iOS technology group:%@", arr[2]);        NSLog (@ "Original address: Http://www.cnblogs.com/xiaofeixiang");`

The effect is as follows:

Bottom-up merge sort

Bottom-up merge sort in fact, the logic is the same as the previous one, but this does not need to be recursive to the end, directly from the beginning of the comparison, through the control of incremental solution merge problem, if there are 8 elements, the increment is 1 with four groups (two per group) merge, the increment is 2, the two groups (four each group) , the code is relatively simple:

`-(void) Mergesortbottom: (Nsmutablearray *) arr{    Nsinteger Count=[arr count];    for (Nsinteger increment=1; increment<count; increment=increment+increment) {//increment array for        (Nsinteger low=0; <count-increment; Low+=increment+increment) {//per Delta comparison            Nsinteger high= (low+increment+increment-1) < (count-1)? ( LOW+INCREMENT+INCREMENT-1):(count-1);            [Self Merge:arr lowindex:low highindex:high midindex:low+increment-1];}}    `

Everyone, Happy Zongzi Festival ~

Algorithm-merge sort

Related Keywords:

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.