Fortune Telling Collection - Horoscope - What are the sorting methods?
What are the sorting methods?
1.? Basic idea:
Insert one data element to be sorted at a time into the appropriate position in the previously sorted sequence, so that the sequence is still in order; Until all the data elements to be sorted are inserted.
2.? Classification process:
Example:
[initial keyword]? [49]? 38? 65? 97? 76? 13? 27? Forty nine
J=2(38)? [38? 49]? 65? 97? 76? 13? 27? Forty nine
J=3(65)? [38? 49? 65]? 97? 76? 13? 27? Forty nine
J=4(97)? [38? 49? 65? 97]? 76? 13? 27? Forty nine
J=5(76)? [38? 49? 65? 76? 97]? 13? 27? Forty nine
J=6( 13)? [ 13? 38? 49? 65? 76? 97]? 27? Forty nine
J=7(27)? [ 13? 27? 38? 49? 65? 76? 97]? Forty nine
J=8(49)? [ 13? 27? 38? 49? 49? 65? 76? 97]?
Program? InsertSort(Var? r? :? FileType);
//Sort R [1...n] is arranged in ascending order. R[0] is the monitoring post//
begin
For what? Me? :=? 2? Where to? n? Do what? //Insert R[2], ..., R[n]//
begin
? R[0]? :=? r; ? j? :=? Me? -? 1;
? What time? R[0]? & lt? R[J]? Do what? //Find the insertion position of r//
begin
R[J+ 1]? :=? r[J]; ? //Move back elements greater than r//
j? :=? j? -? 1
end
? R[J? +? 1]? :=? R[0]? ; ? //insert r? //
end
End; ? //InsertSort? //copy code II. Select a sort.
1.? Basic idea:
Each trip selects the smallest (or largest) element from the data elements to be sorted, and the order is placed at the end of the sorted data sequence until all the data elements to be sorted are arranged.
2.? Classification process:
Example:
Initial keyword? [49? 38? 65? 97? 76? 13? 27? 49]
After the first sorting? 13? [38? 65? 97? 76? 49? 27? 49]
After the second sorting? 13? 27? [65? 97? 76? 49? 38? 49]
After the third sorting? 13? 27? 38? [97? 76? 49? 65? 49]
After the fourth sorting? 13? 27? 38? 49? [49? 97? 65? 76]
After the fifth sorting? 13? 27? 38? 49? 49? [97? 97? 76]
After the sixth sorting? 13? 27? 38? 49? 49? 76? [76? 97]
After the seventh sorting? 13? 27? 38? 49? 49? 76? 76? [? 97]
Final sorting result? 13? 27? 38? 49? 49? 76? 76? 97?
Program? Select sorting (Var? r? :? FileType); ? //Sort R [1...n] directly? //
begin
For what? Me? :=? 1? Where to? n? -? 1? Do what? //Don't n? -? 1 pass selection sort//
? begin
? k? :=? Me;
? For what? j? :=? Me? +? 1? Where to? n? Do what? //Select the smallest element R [k]...n]//
begin
What if? R[J]? & lt? R[K]? then what k? :=? J
End;
? What if? k? & lt& gt? Me? then what //exchange r and R[K]? //
Start? Temporary workers? :=? r; ? r? :=? r[K]; ? R[K]? :=? Temperature; ? End;
? end
End; ? //SelectSort? //copy code III. Bubble sorting
1.? Basic idea:
Compare the sizes of data elements to be sorted in pairs, and when the order of two data elements is found to be opposite, exchange them until there are no data elements in reverse order.
2.? Classification process:
Suppose that the sorted array r [1...n] stands upright, and each data element is regarded as a weighted bubble. According to the principle that light bubbles cannot be below heavy bubbles, the array R is scanned from bottom to top. When light bubbles that violate this principle are scanned, let them "float" upward, and so on until any two bubbles are light above and heavy below.
Example:
49? 13? 13? 13? 13? 13? 13? 13
38? 49? 27? 27? 27? 27? 27? 27
65? 38? 49? 38? 38? 38? 38? 38
97? 65? 38? 49? 49? 49? 49? Forty nine
76? 97? 65? 49? 49? 49? 49? Forty nine
13? 76? 97? 65? 65? 65? 65? 65
27? 27? 76? 97? 76? 76? 76? 76
49? 49? 49? 76? 97? 97? 97? 97?
Program? BubbleSort(Var? r? :? FileType)? //Bottom-up bubble sorting//
begin
For what? Me? :=? 1? Where to? N- 1? Do what? //Don't sort N- 1/
begin
? NoSwap? :=? True; ? //Set the unsorted flag//
? For what? j? :=? n? -? 1? Tonto? 1? Do what? //Scan from bottom to top//
? begin
What if? r[J+ 1]& lt; ? R[J]? then what //Exchange elements//
begin
? Temporary workers? :=? r[J+ 1]; ? R[J+ 1? :=? r[J]; ? R[J]? :=? Temperature;
? NoSwap? :=? wrong
End;
? End;
? What if? NoSwap? then what Return// If there is no exchange in this sort, the algorithm will terminate//
end
End; ? //BubbleSort// copy code IV. Quick sort (quick? Sorting)
1.? Basic idea:
Take any data element in the current disordered area R [1 ... h] as the "benchmark" for comparison (it may be written as X), and use this benchmark to divide the current disorder into two smaller disordered areas: R[ 1..I- 1] and R [I+/kloc-0]. That is, R [1.. I-1] ≤ x.key ≤ r [I+1.. h] (1≤ I ≤ h), when r [
2.? Classification process:
Example:
Initial keyword? [49? 38? 65? 97? 76? 13? 27? 49]
After the first exchange
[27? 38? 65? 97? 76? 13? 49? 49]
After the second exchange
[27? 38? 49? 97? 76? 13? 65? 49]
After the third exchange, scan to the left and the position remains unchanged.
[27? 38? 13? 97? 76? 49? 65? 49]
After the fourth exchange, I scanned to the right and the position remained the same.
[27? 38? 13? 49? 76? 97? 65? 49]
Scan to the left
[27? 38? 13? 49? 76? 97? 65? 49]
(division process)
Initial keyword
[49? 38? 65? 97? 76? 13? 27? 49]
After a classified trip,
[27? 38? 13]? 49? [76? 97? 65? 49]
After two sorting.
[ 13]? 27? [38]? 49? [49? 65]76? [97]
After three sorting trips? 13? 27? 38? 49? 49? [65]76? 97
Final ranking result? 13? 27? 38? 49? 49? 65? 76? 97
What is the state after each trip?
Program? partition(Var? r? :? File type; ? l,? h? :? Integer; ? Var? Me? :? Integer);
//Divide the disordered region R[ 1, H], and I give the position of the reference element that has been located after this division? //
begin
Me? :=? 1; ? j? :=? h; ? x? :=? r? ; //Initialization, based on X//
repeat
What time? (R[J]? & gt=? x)? And then what? (me? & lt? j)? do
? begin
j? :=? j? -? 1? //Scan from right to left and find 1 less than? Elements of x//
What if? Me? & lt? j? then what //R[J] has been found? 〈X//
? begin
? r? :=? r[J]; ? //equivalent to exchanging r and R[J]//
? Me? :=? Me? +? 1
? End;
What time? (R? & lt=? x)? And then what? (me? & lt? j)? do
? Me? :=? Me? +? 1? //Scan from left to right and find the 1 th one greater than? Elements of x///
? End;
? What if? Me? & lt? j? then what //r found it? & gt? x? //
Start? R[J]? :=? r; ? //equivalent to exchanging r and R[J]//
j? :=? j? -? 1
end
Until? Me? =? j;
r? :=? x? //Datum X has been finally positioned//
End; ? //partition? //Copy the code program? Quick sort (Var? r? : file type; ? s,T:? Integer); ? //Sort R [s...t] Hurry up//
begin
What if? s? & lt? t? then what //When r [s...t] is empty or only one element is not sorted//
begin
? Partion(R,s,? t,? I); ? //Divide R [s...t]//
? Quick sort (r, s,? I- 1); //Recursively process the left interval R[S, I- 1]//
? Quick sort (r, I+ 1, t); //Recursively process the right interval R[I+ 1..T]? //
End;
End; ? //Quick sorting//Copy code V. Heap sorting (heap? Sorting)
1.? Basic idea:
Heap sorting is a tree selection sorting. In the sorting process, R [1...n] is regarded as the sequential storage structure of a complete binary tree, and the smallest element is selected by using the internal relationship between the parent node and the child node in the complete binary tree.
2.? Definition of heap:? Sequence of n elements K 1, K2, K3, ..., Kn. A sequence is called a heap if and only if it meets the following characteristics:
Ki≤K2i? Ki? ≤K2i+ 1( 1≤? Me ≤ [N/2])
Heap is essentially a complete binary tree that satisfies the following properties: the keyword of any non-leaf node in the tree is greater than or equal to the keyword of its child nodes. For example, the sequences 10, 15, 56, 25, 30 and 70 are heaps, and their corresponding complete binary trees are shown in the above figure. The root node in this heap (called the heap top) has the smallest keyword, so we call it a small root heap. On the other hand, if the keyword of any non-leaf node in a complete binary tree is greater than or equal to the keyword of its child nodes, it is called a big root heap.
3.? Classification process:
Heap sorting is to use small root heap (or big root heap) to select records with small (or largest) keywords in the current unordered area to achieve sorting. We might as well sort by a lot. The basic operation of sorting every time is to adjust the current unsorted area to a large root heap, select the top record with the largest keyword and exchange it with the last record in the unsorted area. This is just the opposite of direct sorting, which forms an ordered area at the end of the original recording area and gradually extends to the whole recording area.
For example: heap keyword sequence 42, 13, 9 1, 23, 24, 16, 05, 88?
Program? Sift(Var? r? : file type; ? Me? m? :? Integer);
//Call the array r [r .. m in I] to make it a complete binary tree to form a heap. Its left and right subtrees (2I+ 1? & lt=M) are all heaps//
begin
x? :=? r; ? j? :=? 2 * I; ? //If J? & lt=M,? R[J] is the left son of r//
What time? j? & lt=? m? Do what? //If the currently adjusted node R has a left child node R[J]//
begin
What if? (J? & lt? m)? And then what? R[J]。 Keys? & lt? R[J+ 1]。 Keys? then
? j? :=? j? +? 1? //Use a bigger keyword//Make j point to the correct child node.
//J points to the left and right child nodes of R, which have larger keywords//
What if? X. Keys? & lt? R[J]。 Keys? then what //Child node keyword is large//
? begin
r? :=? r[J]; ? //Change R[J] to parent position//
Me? :=? j? ; ? j? :=? 2*I? //Take R[J] as the current adjustment node and continue to adjust to the next level//
? End;
? other
? Exit//After adjustment, exit the loop//
end
r? :=? x; //Put the initially adjusted node in the correct position//
End; //Sift// copy code program? HeapSort(Var? r? :? FileType); ? //Sort R [1...n] in the heap//
begin
For what? Me? :=? n? Div? Tonto? 1? Do what? //Establish initial heap//
Sift(R, me? ,? n)
For what? Me? :=? n? Tonto? 2? Do what? //Sort N- 1 time//
begin
t? :=? r[ 1]; ? R[ 1]? :=? r; ? r? :=? t; //Swap the current record at the top of the heap with the last record in the heap//
Sift(R, 1,? I- 1)? //Re-heap R [1...I- 1]//
end
End; ? ///HeapSort///Copy the code VI. Comparison and Selection of Several Sorting Algorithms
1.? Factors to be considered when selecting a classification method:
( 1)? Number of elements to sort n;
(2)? The information content of the element itself;
(3)? Structure and distribution of keywords;
(4)? The conditions of language tools, the size of auxiliary space, etc.
2.? Summary:
( 1)? If n is small (n? & lt=? 50), you can use direct insertion sorting or direct selection sorting. Because direct insertion sorting requires more record moving operations than direct selection sorting, it is better to directly select sorting when the information content of records is large.
(2)? If the initial state of the file has been basically sorted by keywords, it is more appropriate to choose direct insertion or bubble sorting.
(3)? If n is large, the sorting method with time complexity of O(nlog2n) should be adopted: quick sorting, heap sorting or merge sorting.
Among the internal sorting methods based on comparison at present, quick sorting is considered as the best method.
(4)? In the ranking method based on comparison, only two possible transitions appear after comparing the sizes of two keywords each time, so a binary tree can be used to describe the decision-making process of comparison, which can prove that when n keywords of a file are randomly distributed, any ranking algorithm with the help of "comparison" needs at least O(nlog2n) time.
This sentence is very important? It tells us the algorithm written? Is it the best? Of course, there is no need to always pursue the best.
(5)? When the record itself contains a lot of information, in order to avoid spending a lot of time moving the record, a linked list can be used as the storage structure. ?
- Related articles
- Top ten actresses in men's wear
- 1992 What is the constellation on the second day of the ninth lunar month?
- I was born on October 28th of the lunar calendar 165438. What constellation am I?
- The constellation with the worst reading _ the constellation with the worst reading
- What brand is Omega yoga clothes?
- Marriage selfish constellation male _ marriage selfish constellation male will cheat?
- Twelve constellations secret big exposure _ Twelve constellations secret daquan
- Legend of the Fairy Sword and Chivalrous Man Constellation _ Legend of the Fairy Sword and Chivalrous Man Constellation Representative
- Wanda Constellation Cabinet _ Wanda Constellation Fitness
- Earth sign _ What are the three constellations?