The relationship between 8 sorts:
1, direct insertion sort
(1) Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row
In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n
It's a good order, too. Repeat the loop until all the order is sorted.
(2) Example
(3) Implement with
-insertion sorting is the same as that for direct insertion sorting.
The Code is as follows:
Sample Code (c ):
/** Author: Li Bing Date: 2014-9-6 * Email: [email protected] * @ array: the pointer to the records * @ num: the length of the records */void insertsort (INT array [], int num) {If (array = NULL | num
Summar
]GT;A[4]?not set up, jump out of the loop a[j+1] =a[i] =60I=5,TEMP=A[5]=50, j=i-1=4, Judge A[4]>50, established, a[5]=a[4] J- - ; j = 3; A[3]>50 established? Not set up, jump out of the loop. j[4]= 50; Insert the current value.The process of sorting a column of numbers using an insert sortJava code example: PackageCom.sort; Public classInsertsort {/** Direct Insert Sort * * Parameter description: A--the arr
Poker is believed that we all have played, the direct insertion sort and playing poker is very similar, grab a poker right hand, and put it into the left hand holding the order of poker inside.
As shown in figure:
Algorithm Introduction:
The direct insertion sort algorithm is the simplest algorithm and the most basi
original article, reprint please indicate from Iron Man Mac blog Http://www.cnblogs.com/gangtiexiaThe basic idea of 2-way insertion sort (2-way insertion sort) :elements smaller than fisrt, inserted in front of first;an element larger than final, inserted after final,larger than the fisrt and smaller than the final ele
original article, reprint please indicate from Iron Man Mac blog Http://www.cnblogs.com/gangtiexiaThe basic idea of binary insert sort (Binary insertion sort) is to insert a new record into an ordered table that is already sorted, the initial ordered table has only the first data of the unordered table, sequentially binary the order of each data in the unordered
Write their own code there are several large use cases have been difficult, the online code most of the problem, the idea is to first put the list of empty tables, and then the list of elements in the loop into the specified position.Here is a copy of the correct code, but a list of the leading nodes :void Insertsort (linklist l) { lnode *p,*q,*r,*u; p=l->
First, take a look at the example, insert the data into a list, and then sort the list after inserting it.
Note: This list is incremented and the memory space is allocated, except that the actual data is not populated, as follows:
Copy Code code as follows:
int Insertsort (mergetype *l, int data)
{
Int J;
if (! L->len){L->elem[++l->len
1. Basic IdeasHill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.2. Principle of ImplementationFor N ordered series, take an integer
Recently from NetEase Open class in the MIT Open Class "Introduction to the algorithm," The feeling is good, the next few articles shown in the study diary, not ready to do too much to describe the algorithm details, interested can see for themselves.The article is divided into several classical sorting algorithms, directly on the code, according to the results of the algorithm performance has a visual understanding. This article first says insert
equal to the position of the new element
After inserting a new element into the position
Repeat step 2~5
Tips: If the comparison operation is more expensive than the swap operation , a binary lookup can be used to reduce the number of comparison operations . The algorithm can be thought of as a variant of the insertion sort , called a binary lookup insert sort.Two: ProcessRaw dataAs shown, the
original article, reprint please indicate from Iron Man Mac blog http://www.cnblogs.com/gangtiexia The basic idea of a direct insert sort (straight insertion sort) is to insert a new record into an ordered table that is already sorted, that the initial ordered table has only the first data of the unordered table, and sequentially inserts each data into the unorde
sort
Print (a,10,10);
}
It is difficult for hill to sort the time-lapse analysis, the comparison number of key code and the number of record moves depend on the selection of increment factor sequence d, which can accurately estimate the comparison number of key codes and the number of movement of records in certain cases. At present, no one has given a meth
Insert sort is to insert the data into an already ordered sequence, similar to the order of poker players to organize hands: Just start the player has only one card, a card is not comparable, think is orderly, and then the player pulled out a card, assuming that the face value of the big plug into the back, he will look forward from the back to the position, When the right position is found, the card is inserted. This is how the
loop is true before an iteration, then the next iteration will still be the true proof hypothesis a[1 ... j-1] is true, the next insertion element finds the right position, so a[1 .... j] also trueTermination: When the loop terminates, the invariant is still true to prove that the termination condition is j>a.length when it is terminated.Core templatesInsertion-sort (A) Number of costsFor 2 to A.length C1
1. Direct Insertion Sort
We often come to such a sort of a problem: inserting new data into rows of data that have been sorted out. The first number and the second number are sorted, and then an ordered sequence is inserted into the third number to form a new ordered sequence. For the fourth number, the fifth number ... Until the last number, repeat step two. As
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.