Sorting algorithm Summary and C-code __ sorting algorithm

Source: Internet
Author: User
Tags create index

Recently participated in the written examination, sensory sorting algorithm needs to be properly sorted out, feeling part of the sorting algorithm is not very clear; Through this time of collation and summary of the sorting algorithm for a review bar.

The main reference to the "Liar data Structure":
1. The basic idea of bubble sort: 22 Compare the keywords of adjacent records, if the reverse order is exchanged, until there is no reverse sequence of records.

#define Length

void swap (int *a,int *b)
{
	int temp;
	Temp=*a;
	*a=*b;
	*b=temp;
}
/* Using bubble sort, is the data from small to large array
/void BubbleSort0 (int a[],int len)  
{
	int i,j;
	for (i=0;i<len;i++)
	{
		for (j=i+1;j<len;j++)
		{
			if (a[i]>a[j])
				swap (&a[i), &A[J]);}}
Improvement of bubble algorithm 1, mainly to compare the adjacent two numbers

void BubbleSort1 (int a[],int len)
{
	int i,j;
	for (i=0;i<len;i++)
	{
		for (j=len-2;j>=i;j--)
		{
			if (a[j]>a[j+1])    //The former is compared to the latter, Here is the difference with the previous algorithm
				swap (&a[j],&a[j+1]);}}
Further optimization of the bubbling algorithm, using a flag bit to record the exchange of data, and assigning the flag flag to true if the data is exchanged

void BubbleSort2 (int a[],int len)
{
	int i,j;
	int flag=1;
	for (I=0;i<len &&flag;i++)
	{
		flag=0;
		for (j=len-2;j>=i;j--)
		{
			if (a[j]>a[j+1])
			{
				swap (&a[j],&a[j+1]);
				Flag=1}}}}
Bubble sort Complexity Analysis: When the best case (sequence is already ordered), according to the last improved code, only n-1 comparisons, no exchange between the data, time complexity of O (n), when the worst case (sequence is in reverse order), at this time need to compare N (n-1)/ 2, and the same order of magnitude of record movement is required, so the total time complexity is O (n^2)
2. Simple selection Sort

Simple selection Sort: It is through the comparison between the N-i key words, from the N-i+1 records to select the smallest keyword records, and the first record exchange:

void Selectsort (int a[],int len)
{
	int i,j,min;
	for (i=0;i<len;i++)
	{
		min=i;
		for (j=i+1;j<len;j++)
		{
			if (A[min]>a[j])
				min=j
		}
		if (i!=min)
			swap (&a[min],&a[i]);
	}

Simple choice of order complexity analysis: It is the number of comparisons must be: N (n-1)/2. Therefore, no matter in the sequence of the circumstances, it will not have excellent performance, visible to the data is not sensitive to the order of. Although it compares more times, and for the exchange of times, when the best, the exchange for 0 times, the worst time of exchange for n-1 times, so the data exchange is very small, based on time complexity is the sum of the comparison and exchange times, so the total time complexity is still O (n^2). Although the time complexity of the bubble sort algorithm is the same, the performance of the simple selection sort is slightly better than the bubble sort.
3. Direct Insertion Sort

The basic operation of direct insertion sort is to insert a record into an ordered table that has been sorted so as to obtain a new ordered table with a record number of 1.

void Insertsort (int a[],int len)
{
	int i,j,temp;
	for (i=1;i<len;i++)
	{
		if (a[i]<a[i-1])
		{
			temp=a[i];
			for (j=i-1;a[j]>temp;j--)
				a[j+1]=a[j];
			A[j+1]=temp
		}}
	}
Direct Insert Sort complexity analysis: Remove a reverse order at most after each comparison, so it is the same efficiency as bubbling sorting. But it's still a high speed, because in a bubble sort, it's a value exchange, and a value move under the Insert sort, so the direct insertion sort will be better than the bubble sort. The direct insertion method is also a kind of algorithm which is very sensitive to the order of data. In an orderly situation, only after n-1 comparison, because each time it is a[j]>a[j-1] there is no move record, the time complexity is O (n), in the worst case, will need (n+2) (n-1)/2 times comparison, and will move the maximum number of moves (n+4) (n-1)/2. If the sorting record is random, and the same principle of probability, the average comparison and the number of moves are about you (N^2)/4, so the time complexity of the direct insertion sort is O (n^2).

4. Hill sort

The basic idea of hill sort: first, the whole backlog of records is divided into several sub sequences for direct insertion, and the records in the whole sequence are basically ordered, then the whole records are sorted once.

/* Mainly utilizes the idea of insert sort
/void shellsort (int a[],int len)
{
	int i,j,temp,increment;
	Increment=len;
	Do
	{
		increment=increment/3+1;
		for (i=increment;i<len;i++)
		{
			if (A[i]<a[i-increment])
			{
				temp=a[i];
				for (j=i-increment;j>=0&&a[j]>temp;j-=increment) 
				{
					a[j+increment]=a[j];
				}
				A[j+increment]=temp
			}}
		}
	while (increment>1);
}


Hill ranking Complexity analysis: Hill sort of "incremental" selection is very critical, a large number of studies indicate that when the increment sequence is 2^ (t-k+1)-1 (0=<k=<t=<log2 (n+1)), you can get good results, its time complexity of O (n^1.5). The incremental choice will affect the efficiency of hill sorting. But no matter how the increment is chosen, finally make sure that the increment is 1 to make a direct insert sort. However, it is relative to the direct insertion sort, and as a result of each comparison in the child table, it is possible to remove multiple reverse orders from the entire table, thus improving the overall sorting performance. Hill sort is an algorithm based on insert ordering, so it is sensitive to data order.

5. Heap sorting

The idea of heap sequencing: first, the backlog is built into a large top heap (large top heap: The value of each node is greater than or equal to the value of its left and right child nodes). At this point the maximum value of the entire sequence is the root node of the heap top. Remove it (in fact, it is exchanged with the end element of the heap array, at which point the end element is the maximum), and then reconstruct the remaining n-1 subsequence into a heap, thus getting the secondary large value in n elements. So repeated execution, we get an ordered sequence.

void Heapadjust (int a[],int s,int m)
{
	int temp,j=0;
	Temp=a[s];
	for (j=2*s+1;j<m;j=j*2+1)   
	{
		if (j<m && a[j]<a[j+1])
			++j;
		if (j<m && temp>=a[j]) break
			;
		A[S]=A[J];
		s=j;
	}
	a[s]=temp;
}

void heapsort (int a[],int len)
{
	int i;
	For (i= (len)/2-1;i>=0;i--)
		heapadjust (A,i,len);
	for (i=len-1;i>=1;i--)
	{
		swap (&a[0],&a[i]);
		Heapadjust (a,0,i-1);
	}


Complexity analysis of heap ordering: in the process of building a heap, because a complete binary tree is constructed from a non terminal node of the lowest-level final edge, comparing it to its children and, if necessary, a maximum of two comparison and interchange operations for each non-terminal node, the time complexity of the entire build heap is O (n).

In the formal sort, the first time the heap is recorded to rebuild the heap requires an O (Logi) (the distance from one node of the complete binary tree to the root node is [logi]+1), and the time complexity of the rebuild heap is O (Nlogn), and the n-1 is required to take the top record of the heap.

So overall, the time complexity of heap sorting is O (Nlogn). Because heap sorting is not sensitive to the ordering state of the original record, the best and worst average time complexity is O (NLOGN). so it is generally not suitable for small scale sequences, but for larger sequences, it will show superior performance.

6. Merge sort
Merge sort idea: Assuming that the initial sequence contains n records, it can be thought of as N ordered subsequence, the length of each subsequence is 1, but 22 is merged to obtain an ordered sequence of 2 or 1 of [N/2] ([x] 's smallest integer not less than x); and then 22 merge,....., so repeated, Until an ordered sequence of length n is obtained.

* * Merge ordered SR into TR
/void Merge (int sr[],int tr[],int i,int m,int N)
{
	int j,k,l;
	For (j=m+1,k=i;i<=m && j<=n;k++)/  * Compare size to 1...M and M+1...N medium-length arrays and save small elements
	/{
		if (SR[I]<SR [j])
			tr[k]=sr[i++];
		else 
			tr[k]=sr[j++];
	}
	if (i<=m)/  * Adds a portion of the excess in SR[1...M] to TR
	/{for
		(l=0;l<=m-i;l++)
			tr[k+l]=sr[i+l];
	if (j<=n)/    * Adds a portion of the excess in SR[M+1...N] to TR
	/{for
		(l=0;l<=n-j;l++)
			tr[k+l]=sr[j+l];
	}
/* Sort SR merge as tr1*/
void msort (int sr[],int tr1[],int s,int t)
{
	int m=0;
	int tr2[length+1]={0};
	if (s==t)
		tr1[s]=sr[s];
	else
	{
		m= (s+t)/2;
		Msort (sr,tr2,s,m);
		Msort (sr,tr2,m+1,t);
		Merge (tr2,tr1,s,m,t);
	}
}

Merge order Complexity analysis: A trip to merge needs to be sr[0]~sr[n-1] in the adjacent length of H ordered sequence of 22 merge. And put the results in tr[0]~tr[n-1], this requires a scan of the records in the sequence to be sorted so that the O (n) time is consumed and, by the depth of the complete binary tree, the entire merge sort requires [log (n)] times, so the total time complexity is O (NLOGN), And this is the best, worst, average time performance in the merge sort algorithm. And merging sort is a kind of stable sorting algorithm. Not sensitive to the order of the data. If the data node data volume is large, that will not be suitable. But can be changed to create index operations, the effect will be very good.

Non-recursive implementations of merge ordering are not explained here.

7. Quick Sort

The idea of a quick sort: to divide the backlog into two separate parts by a sort of sequence, in which some of the records are less critical than the other, then the two parts can be sorted separately to achieve the ordered order of the whole order.

int partiton (int a[],int low,int high) {int pivotkey;
		pivotkey=a[low];//Pivot Selection You can select the median while (Low
Fast sort Complexity Analysis: it is also an improvement of bubble sorting, which eliminates multiple reverse orders through one exchange, reducing the number of scans and data exchanges that are consumed in reverse order. In the optimal case, its order time complexity is O (nlog2n). That is, each time the sequence is divided, it can be evenly divided into two substrings. But at worst, its time complexity will be O (n^2). That is, each time the substring is divided, one string is empty and the other is m-1 (if the program uses the central data of each fetch sequence as the dividing point, it will be the best in positive sequence and inverse time). In some books this explains "quick sort," which, in theory, would be the fastest sort algorithm if it could divide the sequence evenly each time, and therefore called it a fast sort. Although it is difficult to evenly divide the sequence, it is still the fastest in the internal sorting algorithm based on keyword comparisons, in terms of average performance.

sorting by average time into classes :
(1) Order of squares (O (n2))
All kinds of simple sorting, such as direct insertion, simple selection and bubble sort;
(2) Order of linear Logarithmic order (O (NLOG2N))
Such as quick sort, heap sort and merge sort;
(3) O (n1+§)) sort
§ is a constant between 0 and 1. Hill sort is one;

selection of sorting methods

Because different sorting methods adapt to different application environments and requirements, it is important to select the appropriate sorting method.
(1) If n is small, direct insertion or Direct selection can be used.
When the record scale is small, the direct insertion sort is better, it will be less than the choice of comparison times;
However, when the record size is large, because the direct selection of the number of mobile records is less than the direct insertion, it is appropriate to select a direct selection of sorting.
Both of these are stable sorting algorithms.
(2) If the initial state of the document in a basic order (correction), you should choose the direct insertion, bubble or random fast sorting appropriate (here is the random reference value of the random, the reason for the rapid sorting analysis); Here the quick sort algorithm will be unstable.
(3) If n is large, then the time complexity of O (nlog2n) Sorting method should be used: quick sort, heap sort or merge sort order.
Fast sorting is considered as the best method at present based on the comparison, when the keyword to be sorted is randomly distributed, the average time of fast ordering is shortest;
Heap sorting does not appear to be the worst-case scenario that can occur with a quick sort. But it needs to build a heap of process. Both of these sorts are unstable.
Merge sort is a stable sort algorithm, but it has a certain amount of data movement, so we may be in combination with the insertion sequence, first get a certain length of the series, and then merge, in the efficiency will be improved.


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.