Insert the implementation code for sort and merge sort (c + +)

Source: Internet
Author: User

Insert sort and merge sort are the first two methods of sorting in the introduction of algorithms.

The idea of inserting a sort is that for an already sequenced array, a new element is now inserted and kept in order. So how to insert it, starting from the last element of the array and comparing it until it encounters an element that is less than or equal to its own, and then inserted behind that element (so the insertion is stable). Cyclic invariant: Sub-arrays are kept in order (using cyclic invariant methods to prove the correctness of the algorithm is similar to the mathematical induction method often used in high schools).

The idea of merging sorts is a well-known divide-and-conquer thought (divide and conquer). A large ordered array is merged from a smaller number of ordered arrays, and the merge sort adopts two-way merging. Multiple merges can use the loser number, or the linked list. The merge sort is also stable, which is determined by the details of the merger.

Insert Sort time complexity θ (n2), merge is θ (NLGN), merge requires extra θ (n) space, and insert does not need (space here does not consider saved stack-bottom pointers, local variables, etc.).

Here is the implementation:

//Insert sort (first partial order gradually expands ordered array to all order) stable/*Pseudo code Insertion-sort (A) 1 for j=2 to a.length 2 key = A[j] 3 i=j-1 4 while (I>0 and A[i]>key) 5 A[i+1]=a[i] 6 i=i-1 7 A[i+1]=key*/void Insertion_sort (int a[],IntN) {if (N <2)returnint I, J; int key; for (j = 1; J < n; ++j) { Key = a[j]; i = j-1while (i >= 0 && a[i] > key) {a[i + 1] = A[i];--i;} a[i + 1] = key;} return       
//Merge sort/*Merge_sort (a,p,r) if P==r return mid= (p+r)/2 Down rounding Merge_sort (a,p,mid) Merge_sort (a,mid+      1,r) Merge (a,p,mid,r) merge (A,p,mid,r) L1 (1,n1) =a (p,mid) L2 (1,n2) =a (mid,r) place sentinel values, i.e. Infinity I=1 at the end J=1 m=1 while (!) (      L1[i]==max&&l2[j]==max)) if (L1[i]<l2[j]) a[m]=l1[i] ++i else A[m]=l2[i] ++j ++m*///The pointer plus offset is a good choice.void Merge (int *a,int P,int Mid,IntR) {int n1 = Mid-p +1;int N2 = R-Midint *l =NewInt[n1 +1];int *r =NewINT[N2 +1];IntI, J, K;for (i =0; I < N1; i++) {L[i] = a[p +I]; }for (j =0; J < N2; J + +) {R[j] = A[mid + j +1]; } L[N1] =Int_max; R[N2] =Int_max;for (i =0, J =0, k = p; K <= R; k++) {if (L[i] <=R[j]) {A[k] =L[i]; i++else{A[k] = R[j]; j++< Span style= "color: #000000;" >; }} delete []l; delete []r;} void merge_sort (int *a, int p, int r) {if (P < R) {int mid = (p + r)/21 

Insert the implementation code for sort and merge sort (c + +)

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.