Common sorting algorithms -- Merge Sorting

Source: Internet
Author: User

Merging and sorting adopts the divide and conquer idea. arrays are constantly decomposed into sub-arrays until the sub-array has only one element. Each decomposition corresponds to a Merge function, the merge function can combine the two child arrays currently decomposed. There are two methods to achieve Merge Sorting. The first method is to implement recursion. The Code is as follows:

#include 
 
  static void merge( int* array, int* tmp, size_t start, size_t end){    size_t i = start, j = (start+end)/2, pos = start;    while(i != (start+end)/2 && j != end){        if(array[i] < array[j]){            tmp[pos++] = array[i++];        }        else{            tmp[pos++] = array[j++];        }    }    for(;i != (start+end)/2; ++i){        tmp[pos++] = array[i];    }    for(;j != end; ++j){        tmp[pos++] = array[j];    }    for(i = start; i != end; ++i){        array[i] = tmp[i];    }}static void merge_sort_service (int * array, int* tmp, size_t start, size_t end){    if(end-start > 2){        merge_sort_service(array, tmp, start, (start+end)/2);        merge_sort_service(array, tmp, (start+end)/2, end);    }    merge(array, tmp, start, end);}void merge_sort( int* array, size_t len){    int* pArray = new int[len];    merge_sort_service(array, pArray, 0, len);    delete [] pArray;}int main(){    int array[10] = {3, 9, 5, 1, 8, 7, 2, 4, 6, 0};    merge_sort(array, 10);    for(int i = 0; i != 10; ++i){        std::cout << array[i] << " ";    }    std::cout << std:: endl;    return 0;}
 

The second method is implemented based on the non-recursive method of the loop. The overall performance is higher than the Merge Sorting implemented by the recursive method. The Code is as follows:
#include 
 
  void merge( int* array, int* tmp, size_t length, size_t step){    size_t i, j, pos, i_stop, j_stop;    size_t k = 0;    size_t stop = length-step;    while(k < stop){        pos = k;        i_stop = k+step-1;        j_stop = (k+2*step)<(length)?(k+2*step-1):(length-1);        for(i = k, j = k+step; i <= i_stop && j <= j_stop; ){            if(array[i] < array[j]){                tmp[pos++] = array[i++];            }            else{                tmp[pos++] = array[j++];            }        }        while(i <= i_stop){            tmp[pos++] = array[i++];        }        while(j <= j_stop){            tmp[pos++] = array[j++];        }        k += 2*step;    }    for(i = 0; i != length; ++i){        array[i] = tmp[i];    }}void merge_sort( int* array, size_t length ){    int* tmp = new int[length ];    size_t k = 1;    while(k < length){        merge(array, tmp, 10, k);        k *= 2;    }    delete [] tmp;}int main(){    int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};    merge_sort(array, 10);    for(int i = 0; i != 10; ++i){        std::cout << array[i] << " ";    }    std::cout << std:: endl;    return 0;}
 

Because the latter method uses loops rather than recursion, it reduces the overhead of function calls and stacks and is more efficient than the first method, but it is more difficult to compile than the first method.


Link: http://blog.csdn.net/girlkoo/article/details/17606331

Author: girlkoo

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.