Insert sort and Merge sort

Source: Internet
Author: User

Insert Sort

Describe:

Suppose you have a poker: 7,2,10,4,6,9,k,4,j,q,3, now you want to sort it.

Starting from left to right, 7 is the first, does not move, starting from the second (2), 2<7 then put to 7 front: 2,7,10,4,6,9,k,4,j,q,3

Then 10 is greater than 7, greater than 2, put back to the original position does not make adjustments.

2<4<7 to the back of 2.

。。。

This until the last 3 is inserted into the back of 2. The whole sort ends.


Merge sort

Describe:

If you want to sort the sequence 7,2,10,4,3,9,k,4,j,q,3, you can divide it into two halves 7,2,10,4,6 and 9,k,4,j,q,3 to sort the two parts separately.

The same sort of 7,2,10,4,6 can be divided into two parts 7,2 and 10,4,6

The 7,2 sort can be divided into two parts: 7 and 2.

The two parts are then merged into the array in order: 2, 7.

The same array 2,7 and 6,4,10 are merged into 2,3,4,7,10.

By analogy, 2,4,6,7,10 and 3,4,9,j,q,k merge into 2,3,4,6,7,9,10,j,q,k

Code implementation:

#include "common.h" extern void Sort ();

static void Insertionsort (int* a,int N);
static void MergeSort (int* a,int p,int R);


static void Merge (int* a,int p,int q,int R);
	void Sort () {int a[]= {19,3,12,4,7,6,12,9,23,14,29,13,34,8};
	Debug (A,sizeof (a)/sizeof (int), 0);
	Insertionsort (A,sizeof (a)/sizeof (int));
	MergeSort (A,0,sizeof (a)/sizeof (int)-1);
Debug (A,sizeof (a)/sizeof (int), 0);
	} static void Insertionsort (int* a,int N) {int J;
		for (j=1;j<n;j++) {int key = A[j];
		int i = j-1;
			while (i>=0 && a[i]>key) {a[i+1] = A[i];
		i--;
	} a[i+1] = key;
	} static void MergeSort (int* a,int p,int r) {int q=0;
		if (p<r) {q = (r+p)/2;
		MergeSort (A,P,Q);
		MergeSort (A,Q+1,R);
		Merge (A,P,Q,R);
	Debug (A,R-P+1,P);
	}} static void Merge (int* a,int p,int q,int r) {int i=0,j=0;
	int len = r-p+1;
	int first1 = p, last1 = q;
	int first2 = q+1, last2 = R;
	int *t = (int*) malloc (len*sizeof (int));
	if (t== NULL | | | len = = 0) {return; while (fIrst1<=last1 && first2 <= last2) {if (A[first1] < A[first2]) {t[i] = A[first1];
			first1++;
		i++;
			else {T[i] = A[first2];
			First2 + +;
		i++;
		} while (First1 <= last1) {t[i] = A[first1];
		first1++;
	i++;
		while (First2 <= last2) {t[i] = A[first2];
		first2++;
	i++;
	For (i=p,j=0 i<=r; j++,i++) {a[i] = t[j];
} free (t);


 }


Although the worst-case runtime for the merge sort is theta (N*LGN), the worst-case runtime for the insert sort is θ (n squared), but the constant factor in the insert sort makes it run faster when n is smaller. Therefore, in the merging sort algorithm, when the child problem is enough, it is more appropriate to use the insertion sort. Worst-case elapsed time θ (NK+NLG (n/k))

static void MergeSort (int* a,int p,int R)
{
	int q=0;
	if (r-p>=7)
	{
		q = (r+p)/2;
		MergeSort (a,p,q);
		MergeSort (a,q+1,r);
		Merge (a,p,q,r);
	}
	else
	{
		insertionsort (a+q,r-q+1);
	}
	Debug (a,r-p+1,p);

}




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.