Merge sort detailed parsing

Source: Internet
Author: User

Let's start with a sequence of two ordered arrays A and B to sort the code, two ordered array to sort by simply selecting the smallest number of two arrays to put in C is OK, then if the array has the remainder of it will be directly connected to after C. The time efficiency is also quickly reached O (n).

Python Source code

def memeryarray (a,b,c):    i = 0    j = 0    k = 0    n = Len (a)    m = len (b) While    i<n and j<m:        if a[ I]<B[J]:            c.append (A[i])            k = k + 1            i = i + 1        else:            c.append (B[j])            k = k + 1            j = j + 1 While    i<n:        c.append (A[i])        k = k + 1        i = i + 1 while    j<m:        c.append (B[j])        k = k + 1        j = j + 1    print C # test memery function Data A = [1,3,5,7,12,19]B = [2,5,6,8,9,10]c = List () Memeryarray (A,B,C)


C + + source

void Memeryarray (int a[], int n, int b[], int m, int c[]) {int I, J, k;i = j = k = 0;while (i < n && J < m) {I F (A[i] < b[j]) c[k++] = a[i++];elsec[k++] = b[j++]; }while (i < n) c[k++] = A[i++];while (J < m) c[k++] = b[j++];}


If an unordered array is to be sorted, we can think of a single element as an ordered sequence that merges only 22, as long as the log (n) is looped. We recursively recursive it to a single element, followed by a 22 sort return. The time complexity is O (log (n) *n).

Python Source code

def mergearray (a,first,mid,last):    temp = []    i = First    j = mid + 1    m = Mid    n = last    k = 0    while I<=m and J<=n:        if a[i] <= a[j]:            temp.append (A[i])            k = k + 1            i = i + 1        else:            Temp.appe  nd (A[j])            k = k + 1            j = j + 1 while    i<=m:        temp.append (A[i])        k = k + 1        i = i + 1    while I<=m:        temp.append (A[j])        k = k + 1        j = j + 1 for    I in range (0,k):        A[first + i] = temp[i]def me Rgesort (a,first,last):    if first<last:        mid = (first+last)/2        mergesort (a,first,mid)        MergeSort (a,mid+1,last)        Mergearray (a,first,mid,last) def mergesort (a):    mergesort (A,0,len (a)-1)    Print AA = [5,1,3,6,19,10,12,4,7]mergesort (a)


C + + source

void Mergearray (int a[], int first, int mid, int last, int temp[]) {int i = First, J = mid + 1;int m = Mid,   n = last;in t k = 0;while (i <= m && J <= N) {if (A[i] <= a[j]) temp[k++] = a[i++];elsetemp[k++] = a[j++];} while (i <= m) temp[k++] = A[i++];while (j <= N) temp[k++] = a[j++];for (i = 0; i < K; i++) A[first + i] = temp[i];} void mergesort (int a[], int first, int last, int. temp[]) {if (first < last) {int mid = (first + last)/2;mergesort (A, fi RST, Mid, temp);   MergeSort (A, mid + 1, last, temp); Mergearray (A, first, mid, last, temp); }}bool mergesort (int a[], int n) {int *p = new Int[n];if (p = = NULL) return False;mergesort (A, 0, n-1, p);d elete[] P;retur n true;}






Merge sort detailed parsing

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.