sorting (sorting) is the process of rearranging a set of objects in a prescribed order, which is often served for retrieval and is a very important and commonly used operation in data processing. For example, our daily study of the dictionary or book directory, these are in advance for us to order, so greatly reducing our retrieval time, improve efficiency.
Sorting can be divided into two main categories:
Internal sorting (Internal sorting): The records to be sorted are all stored in the computer's memory in the sorting process;
External sort (External sorting): The number of records to be sorted is large, the memory cannot store all the records, and the ordering process of external access is required.
The external sort has not contacted and learned at this stage, so we only study the internal sequencing of the centralized algorithm, and we hope to learn from each other and progress together.
In the internal ordering of the more common there are four kinds of algorithms, the following we have a variety of algorithms to learn the common algorithm. Mind mapping is as follows:
First, insert sort
Basic idea: In an already sequenced sequence, an unordered element is inserted into the sequence at a specified position in the order of the original sequence.
Common examples: Direct insert sort
The direct insertion sort (straight insertion sorting) is a simple sorting algorithm, and his basic idea is to insert each record sequentially into an ordered table that is already sorted, thus obtaining a new, sequential table with a record increment of 1. The process is as follows:
Initial sequence: {45 38 66 90 88 10 25}
The first step is to sort the top two numbers before inserting 38 into 45 to get a new sequence {66 90 88 10 25}
The second step is to sort the first three numbers and insert 66 into the appropriate position of the ordered sequence {45 38}, which is {90 88 10 25}
The next step is to know that the last number 25 is inserted into the appropriate position to get the final sequence in a recursive process that is based onthe above.
Algorithm Description:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >void Straightinsertsort (List r,int N) //Direct insert sort on sequential table R { int i, J; for (i=2;i<=n;i++)//n is the table length, from the second record insert { r[0] = r[i];//I record copy as sentry j = i-1; while (R[0].key<r[j].key)//compared to Sentry, until the key value is not greater than the Sentry value { r[j +1]=r[j];//the first J records assigned to the j+1 record j--; } R[j + 1] = r[0];//Inserts the first record into the sequence } }</span>
second, exchange sort
Basic idea: Compare the key value size of two records, if the size of the two record key values appear in reverse order, then exchange these two records, so that the key value of the record to the front of the sequence to move, the key value of a large record to the back of the sequence to move, and eventually get ordered sequence.
Common examples: bubble sort, quick sort
(1) Bubble sort
The bubble Sort method (Bubble sorting) First compares the key value of the first record with the key value of the second record, exchanges the two records in reverse order, and then continues to compare the key values of the second and third records. And so on, until you have completed the exchange of the key values for the n-1 record and the nth record. The first bubble is completed and the result is to move the largest record to the last one. Then the second bubble is similar to the first, with the result that the second largest record is moved to the second-to-last position. Repeat the process until the entire sorting process terminates to get the final ordered sequence.
Initial sequence: {45 38 66 90 88 10 25}
First trip Blister:
The first step is to compare the size of 45 with 38, 38<45 so 45 and 38 Exchange {66 90 88 10 25}
The second step compares the size of 45 with 66, 45<66 so there is no swap position {90 88 10 25}
The third step compares the size of 66 with 90, 66<90 without swapping { 88 10 25}
The fourth step compares the size of 90 with 88, 90>88 one swap position {10 25}
Fifth Step {25}
Sixth step {All-in-ten }
At this time the first bubbling complete, 90 is the maximum number, moved to the final position, after the blistering process with the first bubbling is exactly the same.
After the first blistering, {the
After the second blistering (next)
After the third bubble (three times)
After the four-trip bubble
After the blistering of the five- trip
After the blistering of the six weeks
The seventh trip bubbles after {Tenyears
Sort done!
Algorithm Description:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > void Bubbletsort (List r,int N) { int i, j,temp,endsort; for (i=1;i<=n-1;i++) { endsort = 0; for (j = 1; J <= N-i-1, j + +) { if (R[j].key>r[j+1].key)//In reverse order Exchange record { temp = r[j]; R[J] = r[j + 1]; R[j + 1] = temp; Endsort = 1; } } if (Endsort = = 0) break; } } </span>
(2) Quick Sort
Quick sort (quick sorting) is an improvement to the bubbling sort. Its basic idea is to take a record in N Records of the key value as the standard, usually take the first record key value as the benchmark, through a trip to sort the records to be sorted into less than equal to this key value and more than two separate parts, then the previous part of the record key value is smaller than the following record key value, The two parts are then sorted by this method until the entire ordered sequence is obtained.
Initial sequence: {45 38 66 90 88 10 25}
The first step takes the first number 45 as the standard, then looks from the end of the sequence to the standard number end, finds the first number less than the standard number, and swaps the position with the standard number. That is, starting from 25, 25<45 so the interchange position to get {
The second step starts with the number 25 of the standard number interchange from the previous step to the standard number one end, finds the first number larger than the standard number, and then swaps the position with the standard number. Starting from 25 in the previous step to the standard number 451 end is the right to find the first number greater than 45 is 66, and then the two-digit interchangeposition gets {
The third step continues from the previous step to the number 66 of the standard number interchange, find the standard number 451, find the first number less than the standard number, and then swap the position with the standard number. That is, from the previous step 66 to 451-end lookup, find the first less than the standard number 45 is 10, the interchange position obtained{66
The fourth or from the previous step with the number of standard interchange 10, to the standard number 451, find the first number greater than the standard number of 90, the interchange position obtained {x 66}
The fifth step also looks from 90 to 451, finds the first number less than the standard number 45, and then swaps the position. But here from 901 to 45 of the position did not find the number than the standard number 45 small, then completed the first order. The comparison law can be simplified to the left small right large, that is, the standard number on the left to find less than the number of standard number and the Exchange, the standard number on the right side of the number of more than the standard number and the exchange.
After the first order is completed, the sequence is {88 90 66}, at which point the entire sequence is divided into two parts before the value of 45 is less than or equal to 45, and 45 is greater than 45. The following sort procedure sorts the two parts of the sequence according to the above steps, and so on, until the final ordered sequence is reached.
Algorithm Description:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > //First trip fast sorting algorithm int quickpartition (List r,int low,int hign) { x=r[low]//assigns initial value, standard number while (Low < Hign) { while (
iii. choice of sorting
Basic idea: Select the record with the lowest key value each time in N-i+1 (i=1,2,3......,n-1) record as the first record of the ordered sequence.
Common examples: direct selection of sorting, heap sorting.
(1) Direct Select sortThe basic idea of direct selection sequencing (Selection sorting) is to select the smallest record from the n-i+1 record and exchange it with the first (1<=i<=n-1) record in the first-time selection operation by comparing the N-i secondary key values.
Initial sequence: {45 38 66 90 88 10 25}
The first step is a total of 7 numbers, so n=7, first of all, the other i=1 need to pass 6 times the key value comparison, from 7 records to select the minimum record, and the 1th record Exchange to get {ten years 25}
The second step is another i=2, when we need to pass six comparisons, select the smallest record from the remaining 6 records and exchange it with the 2nd record.
The third step i=3, find the smallest record from the remaining 5 records and exchange it with the 3rd record.
Fourth step i=4, find the smallest record from the remaining 4 records and exchange it with the 4th record {66}
Fifth Step i=5, find the smallest record from the remaining 3 records and exchange it with the 5th record.
Sixth step i=6, find the smallest record from the remaining 2 records and exchange it with the 6th record.
At this point the final ordered sequence is obtained.
Algorithm Description:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > void Selectsort (List r,int N) { int min, I, J; for (i=1;i<=n-1;i++)//each cycle selects a minimum key value { min=i;//assumes the first record key value minimum for (j=i+1;j<=n;j++) { if (r[j].key<r[min].key) min =j;//record subscript if (min!=i) swap (R[min],r[i]) of the minimum record of the key value,//the minimum key value record and the I record Exchange } } }</span>
(2) heap sortingHeap sorting is a sort algorithm designed using the data structure of the heap, which can quickly locate the elements of the specified index using the characteristics of the array. The heap is divided into the largest heap and the smallest heap, and the value of any node in the maximum heap is not less than the value of its two children (if there is a child); The minimum heap is no more than the value of its two children.
Taking the minimum heap as an example, the initial sequence is first built into a minimal heap, and then the heap is rebuilt after the top element of the heap is output. The process is as follows:
Initial sequence: {45 38 66 90 88 10 25}
Build a heap and sort
Algorithm Description:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >void Sift (List r,int k,int m) {int I, j, X; List T; i = k; j = 2 * i; x = R[k].key; t = r[k]; while (j<=m) {if ((J < m) && (R[j].key > R[j + 1].key) J + + If there is a right subtree and the key of the right subtree is small, then filter if (x < R[j].key) break;//filter is complete along the right branch. R[i] = R[j]; i = j; j = 2 * i; } R[i] = t;//fill in the appropriate position}}//heap sort algorithm void Heapsort (List R) {i NT I; for (i = N/2; I >= 1; i--) shit (R, I, n);//Start filtering from the first N/2 record build heap for (i=n;i>=2;i--) {Swap (r[1], r[i]);//swap the top record of the heap with the last record in the heap Sift (R, 1, i-1);//adjust r[1] make r[1],......, r[i-1] into a heap }}</span>
Iv. Merging and sorting
Basic idea: Combine two or more two ordered tables into a new ordered table, the merging method is the key value of comparing the first record of a subsequence, and the smallest one is the first record paper of the sorted sequence. By taking this record and continuing to compare the key values of the first record in each subsequence, you can find the second record after the order. And so on, finally get ordered sequence.
Common examples: Two-way merge sort.
The two-way merge sort is to combine two ordered tables into an ordered table, the basic idea is that there are N records in the sequence, which can be regarded as N ordered sub-sequences, and the length of each sequence is 1. First, each adjacent two records are merged to get N/2 (rounding up) a larger ordered sequence, each containing 2 records. Then the sequence 22 is combined to get (N/2)/2 (rounding up) ordered sequence, so repeated, know to get the final ordered sequence so far.
Initial sequence: [45] [38] [66] [90] [88] [10] [25]
Once merged: [38 45] [66 90] [10 88] [25]
Two times after merging: [38 45 66 90] [10 25 88]
Three times after merging: [10 25 38 45 66 88 90]
The resulting ordered sequence is: {10 25 38 45 66 88 90}
We have introduced a total of four sorting algorithms: INSERT, Swap, select, and merge sort, with emphasis on six internal sorting algorithms: Direct insert sort, bubble sort, quick sort, direct select sort, heap sort, and merge sort. These are our commonly used sorting algorithm, we can not simply evaluate the merits of the algorithm, but to choose the appropriate algorithm according to the actual situation to improve work efficiency. I just based on the contents of the book with their own understanding of the various sorting algorithms introduced, there are shortcomings and please criticize correct.
Data structure--Summary of sorting algorithm