1. Select sort
The basic idea of sorting is:
Processing the sorted record sequence for n-1 times, and processing for 1st times means processing for L [1 .. n] the smallest one exchanges location with L [1]. For 2nd times .. the smallest person in N] exchanges location with L [2 ,......, the I-th processing is to convert L [I .. the smallest person in N] exchanges location with L [I. In this way, after I times, the positions of the previous I records are arranged in ascending order.
Example 1: input sequence data is output in non-descending order.
ProgramAs follows:
Program xzpx;
Const n = 7;
VaR A: array [1 .. n] of integer;
I, J, K, T: integer;
Begin
Write ('enter Date :');
For I: = 1 to n do read (A [I]);
Writeln;
For I: = 1 to n-1 do
Begin
K: = I;
For J: = I + 1 to n do
If a [J] <A [k] Then K: = J;
If K <> I then
Begin T: = A [I]; A [I]: = A [k]; A [k]: = T; end;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
2. Insert sorting
The basic idea of insertion sorting: After I-1 processing, L [1 .. I-1] has arranged the order. Only insert L [I] into L [1 .. i-1] of the appropriate position P, the original p elements one by one to the right move a position, so l [1 .. i] is a sorted sequence.
Example 2: input sequence data is output in non-descending order.
Procedure 1:
Program crpx;
Const n = 7;
VaR A: array [1 .. n] of integer;
I, J, K, T: integer;
Begin
Write ('enter Date :');
For I: = 1 to n do read (A [I]);
Writeln;
For I: = 2 to n do
Begin
K: = A [I]; J: = I-1;
While (k <A [J]) and (j> 0) Do
Begin a [J + 1]: = A [J]; J: = J-1 end;
A [J + 1]: = K;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
3. Bubble Sorting
The basic idea of Bubble sorting, also known as exchange sorting, is to compare the key words of the sorted records. If two
If the record is in reverse order, it is exchanged until there is no reverse order record.
For example, the input sequence data is output in non-descending order.
Procedure 1:
Program mppx;
Const n = 7;
VaR A: array [1 .. n] of integer;
I, J, K, T: integer;
Begin
Write ('enter Date :');
For I: = 1 to n do read (A [I]);
For I: = 1 to n-1 do
For J: = n downto I + 1 do
If a [J-1] <A [J] Then
Begin T: = A [J-1]; A [J-1]: = A [J]; A [J]: = t end;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Procedure 2:
Program mppx;
Const n = 7;
VaR A: array [1 .. n] of integer;
I, J, K, T: integer;
Bool: Boolean;
Begin
Write ('enter Date :');
For I: = 1 to n do read (A [I]);
I: = 1; bool: = true;
While (I <n) and bool do
Begin
Bool: = false;
For J: = n downto I + 1 do
If a [J-1] <A [J] Then
Begin T: = A [J-1]; A [J-1]: = A [J]; A [J]: = T; bool: = true end;
I: = I + 1;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Procedure 3:
Program mppx;
Const n = 7;
VaR A: array [1 .. n] of integer;
I, J, K, T: integer;
Begin
Write ('enter Date :');
For I: = 1 to n do read (A [I]);
Writeln;
K: = N;
While K> 0 do
Begin
J: = K-1; K: = 0;
For I: = 1 to J do
If a [I]> A [I + 1] Then
Begin T: = A [I]; A [I]: = A [I + 1]; A [I + 1]: = T; k: = I; end;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Return
4.2 fast sorting
The idea of fast sorting is: first select an element from the data sequence, and put all the elements in the sequence smaller than this element to its right or left, use the same method on the left and right sides until the length of each sequence to be processed is 1 and the processing ends.
For example, enter a group of data in ascending order.
Procedure 1:
Program kspv;
Const n = 7;
Type
Arr = array [1 .. n] of integer;
VaR
A: arr;
I: integer;
Procedure quicksort (var B: arr; S, T: integer );
VaR I, j, X, T1: integer;
Begin
I: = s; J: = T; X: = B [I];
Repeat
While (B [J]> = x) and (j> I) Do J: = J-1;
If j> I then begin T1: = B [I]; B [I]: = B [J]; B [J]: = T1; end;
While (B [I] <= x) and (I <j) Do I: = I + 1;
If I <j then begin T1: = B [J]; B [J]: = B [I]; B [I]: = T1; End
Until I = J;
B [I]: = X;
I: = I + 1; J: = J-1;
If S <j then quicksort (B, S, J );
If I <t then quicksort (B, I, T );
End;
Begin
Write ('input data :');
For I: = 1 to n do read (A [I]);
Writeln;
Quicksort (A, 1, n );
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Procedure 2:
Program kspv;
Const n = 7;
Type
Arr = array [1 .. n] of integer;
VaR
A: arr;
I: integer;
Procedure quicksort (var B: arr; S, T: integer );
VaR I, j, X: integer;
Begin
I: = s; J: = T; X: = B [I];
Repeat
While (B [J]> = x) and (j> I) Do J: = J-1;
If j> I then begin B [I]: = B [J]; I: = I + 1; end;
While (B [I] <= x) and (I <j) Do I: = I + 1;
If I <j then begin B [J]: = B [I]; J: = J-1; End
Until I = J;
B [I]: = X;
I: = I + 1; J: = J-1;
If S <j then quicksort (B, S, J );
If I <t then quicksort (B, I, T );
End;
Begin
Write ('input data :');
For I: = 1 to n do read (A [I]);
Writeln;
Quicksort (A, 1, n );
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Return
4.3 Hill sorting
Basic Idea: divide the entire unordered sequence into several small sub-sequences for insertion sorting or Bubble Sorting respectively.
Sequence segmentation method: the elements separated by an incremental H form a subsequence. In the sorting process, this increment is gradually reduced. Finally, when H is reduced to 1, an insert or bubble sort is performed to complete the sorting. Incremental sequence is generally used: d1 = N Div 2, DI = di-1 Div 2; I = 2, 3, 4... where N is the length of the sequence to be sorted.
Procedure 1: (The subsequence is insert sorting)
Program xepx;
Const n = 7;
Type
Arr = array [1 .. n] of integer;
VaR
A: arr;
I, j, T, D: integer;
Bool: Boolean;
Begin
Write ('input data :');
For I: = 1 to n do read (A [I]);
Writeln;
D: = N;
While D> 1 do
Begin
D: = D Div 2;
For J: = d + 1 to n do
Begin
T: = A [J]; I: = J-D;
While (I> 0) and (A [I]> T) Do
Begin a [I + D]: = A [I]; I: = I-d; end;
A [I + D]: = T;
End;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Procedure 2: (sub-sequence is bubble sort)
Program xepx;
Const n = 7;
Type
Arr = array [1 .. n] of integer;
VaR
A: arr;
I, temp, D: integer;
Bool: Boolean;
Begin
Write ('input data :');
For I: = 1 to n do read (A [I]);
Writeln;
D: = N
While D> 1 do
Begin
D: = D Div 2;
Repeat
Bool: = true;
For I: = 1 to n-d do
If a [I]> A [I + D] Then
Begin temp: = A [I]; A [I]: = A [I + D]; A [I + D]: = temp; bool: = false end;
Until bool;
End;
Write ('output data :');
For I: = 1 to n do write (A [I]: 6 );
Writeln;
End.
Return
4.4 heap sorting and binary tree sorting
1. Heap sorting
Heap: a set of data elements (R1, R2, R3,... RN). They are nodes of an ordered binary tree and contain
RI <= r2i and RI <= r2i + 1 (or> =)
Heap nature: the element at the root node of the heap is the smallest element in the heap, And the elements on each path of the heap are ordered.
The idea of heap sorting is:
1) create an initial heap (change the node [n/2], [n/2]-1,... 3, 2, and 1)
2) When not sorted
Output the heap top element, delete the heap top element, and re-create the remaining element.
The procedure is as follows:
Program duipx;
Const n = 8;
Type arr = array [1 .. n] of integer;
VaR A: arr; I: integer;
Procedure sift (var a: arr; L, M: integer );
VaR I, j, T: integer;
Begin
I: = L; J: = 2 * I; T: = A [I];
While j <= m do
Begin
If (j <m) and (A [J]> A [J + 1]) then J: = J + 1;
If T> A [J] Then
Begin a [I]: = A [J]; I: = J; J: = 2 * I; End
Else exit;
A [I]: = T;
End;
End;
Begin
For I: = 1 to n do read (A [I]);
For I: = (N Div 2) downto 1 do
Sift (A, I, n );
For I: = n downto 2 do
Begin
Write (A [1]: 4 );
A [1]: = A [I];
Sift (A, 1, I-1 );
End;
Writeln (A [1]: 4 );
End.
2. Binary Tree sorting
Sort Binary Tree: each data that participates in the Arrangement corresponds to a node of the binary tree, and if any node has a left (right) subtree, the left (right) the data of each node of the subtree must be smaller (larger) than that of the node. The result is obtained by traversing the binary tree in the middle order. The procedure is as follows:
Program pxtree;
Const
A: array [1 .. 8] of integer = (10, 18, 3, 8, 12, 2, 7, 3 );
Type Point = ^ nod;
Nod = record
W: integer;
Right, left: point;
End;
VaR root, first: point; K: Boolean; I: integer;
Procedure hyt (D: integer; var P: Point );
Begin
If P = nil then
Begin
New (P );
With P ^ do begin W: = D; Right: = nil; left: = nil end;
If K then begin root: = P; K: = false end;
End
Else with P ^ do if D> = W then hyt (D, right) else hyt (D, left );
End;
Procedure hyt1 (P: Point );
Begin
With P ^ do
Begin
If left <> nil then hyt1 (left );
Write (W: 4 );
If right <> nil then hyt1 (right );
End
End;
Begin
First: = nil; K: = true;
For I: = 1 to 8 do hyt (A [I], first );
Hyt1 (Root); writeln;
End.
Return
4.5 Merge Sorting
Merging is to combine multiple ordered series into an ordered series. Merge the two ordered sequences into an ordered sequence called the two-way merge (merge). The merge sequence is a sub sequence with the N length of 1, and the merge of the two ends is finally an ordered sequence.
1. Two-way merge
Example 1: Combine the sorted table L1 = (, 7), L2 = (, 10) into an ordered table L.
Program GB;
Const M = 3; n = 7;
Type arrl1 = array [1 .. m] of integer;
Arrl2 = array [1 .. n] of integer;
Arrl = array [1 .. m + n] of integer;
VaR A: arrl1;
B: arrl2;
C: arrl;
I, J, K, R: integer;
Begin
Write ('enter L1 (sorted ):');
For I: = 1 to M do read (A [I]);
Write ('enter L2 (sorted ):');
For I: = 1 to n do read (B [I]);
I: = 1; J: = 1; K: = 0;
While (I <= m) and (j <= N) Do
Begin
K: = k + 1;
If a [I] <= B [J] Then begin C [k]: = A [I]; I: = I + 1; End
Else begin C [k]: = B [J]; J: = J + 1; end;
End;
If I <= m then for R: = I to M do C [M + R]: = A [R];
If j <= n then for R: = J to n do C [n + R]: = B [R];
Writeln ('output data :');
For I: = 1 to m + n do write (C [I]: 5 );
Writeln;
End.
2. Merge and sort
Program gbpx;
Const maxn = 7;
Type arr = array [1 .. maxn] of integer;
VaR A, B, C: arr;
I: integer;
Procedure merge (r: arr; L, M, N: integer; var R2: ARR );
VaR I, J, K, P: integer;
Begin
I: = L; J: = m + 1; K: L-1;
While (I <= m) and (j <= N) Do
Begin
K: = k + 1;
If R [I] <= R [J] Then begin R2 [k]: = R [I]; I: = I + 1 end
Else begin R2 [k]: = R [J]; J: = J + 1 end
End;
If I <= m then
For P: = I to M do begin K: = k + 1; r2 [k]: = R [p] end;
If j <= n then
For P: = J to n do begin K: = k + 1; r2 [k]: = R [p] end;
End;
Procedure mergesort (var r, R1: arr; S, T: integer );
VaR K: integer; C: arr;
Begin
If S = t then R1 [s]: = R [s] else
Begin
K: = (S + T) Div 2;
Mergesort (R, C, S, k );
Mergesort (R, C, k + 1, t );
Merge (C, S, K, T, R1)
End;
End;
Begin
Write ('enter data :');
For I: = 1 to maxn do
Read (A [I]);
Mergesort (A, B, 1, maxn );
For I: = 1 to maxn do
Write (B [I]: 9 );
Writeln;
End.
Return
4.6 Linear sorting
As discussed aboveAlgorithmBoth are comparison-based sorting algorithms. In the Sorting Algorithm, there are algorithms based on numbers: Counting, bucket, and base sorting.
1. Count sorting
The basic idea is to determine the number of elements in the sequence smaller than X for each element x in the sequence.
For example, N integer sequences with each value in [1, m] are sorted.
Program jspx;
Const M = 6; n = 8;
VaR I, j: integer;
A, B: array [1 .. n] of integer;
C: array [1 .. m] of integer;
Begin
Writeln ('enter data :');
For I: = 1 to n do read (A [I]);
For I: = 1 to M do C [I]: = 0;
For I: = 1 to n do C [A [I]: = C [A [I] + 1;
For I: = 2 to M do C [I]: = C [I] + C [I-1];
For I: = n downto 1 do
Begin
B [C [A [I]: = A [I];
C [A [I]: = C [A [I]-1;
End;
Writeln ('sorted data :');
For I: = 1 to n do
Write (B [I]: 6 );
End.
2. Sort buckets
The thought of Bucket sorting is that if the keyword of the record to be sorted is within an obviously limited range (integer), a limited number of ordered buckets can be designed, and each bucket is loaded with a value, output the values of each bucket sequentially to obtain an ordered sequence.
For example, enter n integers ranging from 0 to 100 and output them in ascending order.
Program tpx;
Const n = 7;
VaR B: array [0 .. 100] of integer;
K: 0. 100;
I: integer;
Begin
Write ('enter date :( 0-100 )');
For I: = 0 to 100 do B [I]: = 0;
For I: = 1 to n do
Begin
Read (k );
B [k]: = B [k] + 1;
End;
Writeln ('output data :');
For I: = 0 to 100 do
While B [I]> 0 do begin write (I: 6); B [I]: = B [I]-1 end;
Writeln;
End.
3. Base sorting
The basic idea is to sort n elements by K, K-1,... the number on the first digit in the bucket.
Program jspx;
Const n = 8;
Type link = ^ node;
Node = record
Data: integer;
Next: Link;
End;
VaR I, j, L, M, K: integer;
A: array [1. N] of integer;
S: string;
Q, head: array [0 .. 9] of link;
P, P1: Link;
Begin
Writeln ('enter data :');
For I: = 1 to n do read (A [I]);
For I: = 5 downto 1 do
Begin
For J: = 0 to 9 do
Begin
New (head [J]);
Head [J] ^. Next: = nil;
Q [J]: = head [J]
End;
For J: = 1 to n do
Begin
STR (A [J], S );
For K: = 1 to 5-length (s) do
S: = '0' + S;
M: = ord (s [I])-48;
New (P );
P ^. Data: = A [J];
P ^. Next: = nil;
Q [m] ^. Next: = P;
Q [m]: = P;
End;
L: = 0;
For J: = 0 to 9 do
Begin
P: = head [J];
While P ^. Next <> nil do
Begin
L: = L + 1; P1: = P; P: = P ^. Next; dispose (P1); A [l]: = P ^. Data;
End;
End;
End;
Writeln ('sorted data :');
For I: = 1 to n do
Write (A [I]: 6 );
End.
4.7 comparison of various sorting algorithms
1. Stability Comparison
Insertion sorting, Bubble sorting, binary tree sorting, two-way merge sorting, and other linear sorting are stable.
It is unstable to select sorting, Hill sorting, fast sorting, and heap sorting.
2. Comparison of time complexity
Insert sorting, Bubble sorting, and select the time complexity of sorting as O (n2)
The time complexity of other non-linear sorting is O (nlog2n)
The time complexity of Linear sorting is O (n );
3. Comparison of auxiliary space
The auxiliary space for Linear sorting and binary Merge Sorting is O (n), and the auxiliary space for other sorting is O (1 );
4. Other comparisons
The insertion and Bubble Sorting speed is slow, but this sorting speed can reach a high speed when the sequencing sequence is partial or overall.
In this case, fast sorting slows down.
When N is relatively small, sequence is not required for stability. sequence is required for stability. insertion or Bubble Sorting is required.
If the keywords of the record to be sorted are within a obviously limited range, and the space can be sorted in buckets.
When N is large, keyword elements are random, so quick sorting is not required for stability.
When N is large, the keyword element may appear in order. When stability is required, the space is allowed.
Sort by merge.
When N is large, the keyword element may appear in order, and there is no requirement for stability. sort by heap.