Insert and Merge sort

Source: Internet
Author: User

Introduction to the algorithm: The main concern is the performance of the program, speed is very eager!!!

The sorting algorithm is the classical algorithm

1. Insert Sort

(1), Algorithm model

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/8D/A5/wKioL1ilJmjRdloXAAAQz-YVjX0436.png-wh_500x0-wm_ 3-wmp_4-s_724856491.png "title=" Qq20170216121033.png "alt=" Wkiol1iljmjrdloxaaaqz-yvjx0436.png-wh_50 "/>

(2), code implementation

#include <stdio.h>void insertsort (Int *a, int count); Void showarray (Int *a,  int count); Void showarray (int *a, int count) {    int i ;         for (i = 0; i < count; i++) {         printf ("%d ",  a[i]);    }     printf ("\ n");  }void insertsort (Int *a, int count) {     int i;    int j;    int n;     int tmp;    for (i = 1; i < count; i++) {         tmp = a[i];         for (j = 0; a[i]>a[j] && j<i; j++) {             ;        }         if (i != j) {            for (n  = i; n > j; n--) {                 a[n] = a[n-1];        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;A[J]  = tmp;        }    }}void main ( void) {    int a[] = {2, 5, 7, 1, 11, 0, 6,  9};    int count = sizeof (a)/sizeof (int);     printf ("Output the following:  before sorting");     showarray (A, count);     insertsort (A ,  count);     printf ("Sort output as follows: ");     showarray (A, count);} 

(3), results

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8D/A5/wKioL1ilJybjQ2b1AAAP0qWNlfk788.png-wh_500x0-wm_ 3-wmp_4-s_422871366.png "title=" Qq20170216121412.png "alt=" Wkiol1iljybjq2b1aaap0qwnlfk788.png-wh_50 "/>


(4), algorithm analysis

Insert Sort worst case: all elements in the array are in reverse order;

Time complexity:O (n^2);


2. Merge sort

(1), the algorithm thought:

I, if n = 1; Done

II, recursive sequencing, divided into 2 parts, in [0, N/2] and [N/2, N]

III. Sort the 2-part merge

(2), Core code implementation

#include <stdio.h> #include <malloc.h>void mergersort (int *a1, int *a2, int &NBSP;**A3,&NBSP;INT&NBSP;COUNT1,&NBSP;INT&NBSP;COUNT2,&NBSP;INT&NBSP;*COUNT3); Void showArray (int * A3, int count); Void showarray (int *a3, int count) {    int  i;    for (i = 0; i < count; i++) {         printf ("%d ",  a3[i]);    }     printf ("\ n");} Void mergersort (int *a1, int *a2, int **a3, int count1, int  COUNT2,&NBSP;INT&NBSP;*COUNT3) {    int count;    int i  = 0;    int j = 0;    int n = 0;     count = *count3 = count1 + count2;     *a3 =  (int *) malloc (sizeof (int)  * count);     //below are < Because the length of the array is passed over;     while (I < count1 && j < count2 ) {        if (a1[i] < a2[j]) {              (*A3) [n++] = a1[i];             i++;        }else if (A1 [I] == a2[j]) {             (*A3) [n++]  = a1[i];             (*A3) [n++] =  a2[j];            i++;             j++;        } else{       //just wrote program else (A1[i] > a2[j]), and later found that the Else statement is not conditional!!!              (*A3) [n++] = a2[j];             j++;         }    }    while (I&NBSP;&LT;&NBSP;COUNT1) {          (*A3) [n++] = a1[i];         i++;    }    while (J < count2) {          (*A3) [n++] = a2[j];        j++ ;    }}/*  Merge Sort core algorithm is: the order of the 2 arrays have been sorted into the final sorting process; */void main (void) {     int a1[] = {1, 3, 5, 7};    int a2[] =  {0,&NBSP;2,&NBSP;4,&NBSP;6,&NBSP;8,&NBSP;9,&NBSP;10};&NBSP;&NBSP;&NBSP;&NBsp;int count1 = sizeof (A1)/sizeof (int);    int count2 =  sizeof (A2)/sizeof (int);    int *a3 = null;    int  Count3 = 0;    mergersort (A1, a2, &a3, count1, count2, &NBSP;&AMP;COUNT3);     showarray (A3,&NBSP;COUNT3);     free (A3);}

(3), results

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8D/A6/wKioL1ilO6_yg_-DAAAI7ipMkOg303.png-wh_500x0-wm_ 3-wmp_4-s_460705797.png "title=" Qq20170216134151.png "alt=" Wkiol1ilo6_yg_-daaai7ipmkog303.png-wh_50 "/>

(4), complete code implementation

#include <stdio.h> #include <malloc.h>void mergesort (int *a, int low, int  high); Void merge (Int *a, int low, int mid, int high);void  Merge (Int *a, int low, int mid, int high) {    int  I = low;    int j = mid+1;    int n  = 0;    int *a2;    a2 =  (int *) malloc ( sizeof (int)  *  (high-low+1))     if (a2 == null) {         return;    }    //The following are <=, because all the passing is subscript;     while (I <= mid && j <= high) {         if (A[i] < a[j]) {             a2[n++] = a[i];            i++;         }else if (A[i] == a[j]) {             a2[n++] = a[i];             i++;            j++;         }else{             a2[n++] = a[j];             j++;        }    }     while (I <= mid) {        a2[n++] = a[i];         i++;    }    while (j  <= high) {        a2[n++] = a[j];         j++;    }    for (n = 0, i = low; i  <= high; n++, i++) {  //copies the elements in A2 back to a;         a[i] = a2[n];    }    free (A2);} Void mergesort (Int *a, int low, int high) {    int mid;     if (Low < high) {        mid =   (Low + high)  / 2;        mergesort (a,  LOW,&NBSP;MID);         mergesort (A, mid+1, high);         merge (A, low, mid, high);     }} Void main (void) {    int a[] = {2, 4, 1, 7, 5, 6, 9, 10};     int count = sizeof (a)/sizeof (int);    int i;     mergesort (a, 0, count-1);     for (i = 0; i <  count; i++) {        printf ("%d ",  a[i]);     }    printf ("\ n");}

(5), results

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8D/A8/wKioL1ilVrrwQhKCAAAOMNBPomM157.png-wh_500x0-wm_ 3-wmp_4-s_3414715572.png "title=" Qq20170216153717.png "alt=" Wkiol1ilvrrwqhkcaaaomnbpomm157.png-wh_50 "/>

(6), algorithm analysis

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8D/A6/wKioL1ilQffzZR-QAAAZO7ZW08E869.png-wh_500x0-wm_ 3-wmp_4-s_2733749069.png "title=" Qq20170216140842.png "alt=" Wkiol1ilqffzzr-qaaazo7zw08e869.png-wh_50 "/>

Merge sort time complexity: Tree height log (n), a total of n elements to be sorted, so is:O (Nlogn);

Within 30 elements, insert sort performance is better, after more than 30 elements, the performance of the merge sort is more excellent;



This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1898496

Insert and Merge sort

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.