C + + Merge sort summary __c++

Source: Internet
Author: User
#include <iostream> using namespace std;
Merge sort is not recursive version.
    void Sort (int a[], int n,int high) {int k; for (int i = 0; i < i = 2 * n) {int x = i;//001 int y = i + n;//2 2 3 int z = i +
        2 * n;//4 if (z>high) {z = high;
            for (int j = y; J <= Z; j +) {int temp = a[j]; for (k = j; k > 0; k--) {if (a[k-1]>temp) {a[k]
                = A[k-1];
                } else {break;
            } A[k] = temp;
        When merging is required, I choose to use Insert sorting without opening up new space.
    }} void grial (int a[],int high) {int n = high/2+1;//terminate position.
    1 2 3 for (int i = 1; I <= n; I *= 2) {Sort (A,i,high); int main () {int a[] = {5,4,3,7,1,3,2,0,5,6,7,9,654,5,423,3,4, 5,6,8,523,423,4,5,6,7,8,45,6,53, 423,4,100};
    Grial (A,sizeof (a)/sizeof (int)-1);
    for (int i = 0; i < sizeof (a)/sizeof (int); i++) {cout << a[i] << "";
    } cout << Endl;
return 0; }
#include <iostream> using namespace std;               
Recursive version of the merge sort.                                                
    void Sort (int a[], int low, int mid, int high) {
    int i;
    Int J;
    int temp;
        for (i = mid+1 i <= high; i++) {temp = A[i];
            for (j = i; j > low;j--) {if (temp < a[j-1]) {a[j] = a[j-1];
            } else {break;
        }//Now that we are close to sorting, we can choose to use the Insert sort or bubble sort to increase efficiency//And not open up additional space.
    A[J] = temp;  } void Sert (int a[],int low,int high) {if               

    (Low >= high) return;                 
    int mid = (low + high)/2;                    
    Sert (A,low, mid);
    Sert (A, mid + 1, high);
Sort (A,low,mid,high); int main () {int a[] = {6,2,1,3,4,-1,2-3,4,5,6,7,532,4,432,-23, -32,1,-3,21,-321,-3,5,6,8,432,5,66,991};
    Sert (A, 0,sizeof (a)/sizeof (int)-1);
    for (int i = 0; i < sizeof (a)/sizeof (int); i++) {cout << a[i] << "";
    } cout << Endl;
return 0; }

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.