C/C ++ algorithm example (4)-Sorting Algorithm

Source: Internet
Author: User

A. quick sorting:

Procedure qsort (L, R: integer );
VaR I, j, mid: integer;
Begin
I: = L; J: = r; Mid: = A [(L + r) Div 2]; {define the number of the current sequence in the center as the number}
Repeat
While a [I] <mid do Inc (I); {search for a number larger than the intermediate number in the left half}
While a [J]> mid do Dec (j); {search for a smaller number than the intermediate number in the right half}
If I <= J then begin {if you find a number pair that is inconsistent with the sorting target, exchange them}
Swap (A [I], a [J]);
INC (I); Dec (j); {continue searching}
End;
Until I> J;
If l <j then qsort (L, J); {if the boundary of two numbers is not reached, the left and right intervals are recursively searched}
If I <r then qsort (I, R );
End; {sort}

B. Insert sorting:

Idea: The current A [1] .. A [I-1] has been sorted, now want to insert a [I] so that a [1] .. A [I] Order.
Procedure insert_sort;
VaR I, j: integer;
Begin
For I: = 2 to n do begin
A [0]: = A [I];
J: = I-1;
While a [0] <A [J] Do begin
A [J + 1]: = A [J];
J: = J-1;
End;
A [J + 1]: = A [0];
End;
End; {inset_sort}

C. Select sorting:
Procedure sort;
VaR I, j, k: integer;
Begin
For I: = 1 to n-1 do
For J: = I + 1 to n do
If a [I]> A [J] then swap (A [I], a [J]);
End;

D. bubble sort
Procedure bubble_sort;
VaR I, j, k: integer;
Begin
For I: = 1 to n-1 do
For J: = n downto I + 1 do
If a [J] <A [J-1] then swap (A [J], a [J-1]); {the relationship between adjacent elements is compared each time}
End;

E. Heap sorting:
Procedure sift (I, m: integer); {adjust the subtree with I as the root to become a heap, and m as the total number of nodes}
VaR K: integer;
Begin
A [0]: = A [I]; K: = 2 * I; {the left child of node I in the Complete Binary Tree is 2 * I, the right child is 2 * I + 1}
While k <= m do begin
If (k <m) and (A [k] <A [k + 1]) Then Inc (k ); {find out the comparison between a [k] And a [k + 1}
If a [0] <A [k] Then begin a [I]: = A [k]; I: = K; K: = 2 * I; End
Else K: = m + 1;
End;
A [I]: = A [0]; {place the root in a proper position}
End;

Procedure heapsort;
VaR
J: integer;
Begin
For J: = N Div 2 downto 1 do sift (J, N );
For J: = n downto 2 do begin
Swap (A [1], a [J]);
Sift (1, J-1 );
End;
End;

F. Merge Sorting
{A is the sequence list, and TMP is the secondary array}
Procedure merge (var a: listtype; P, Q, R: integer );
{Merge sorted subsequences A [P. q] And a [q + 1. R] into ordered TMP [P. R]}
VaR I, j, T: integer;
TMP: listtype;
Begin
T: = P; I: = P; J: = q + 1; {T is the TMP pointer, And I and j are the pointers of left and right subsequences respectively}
While (T <= r) Do begin
If (I <= q) {left sequence residual} and (j> r) or (A [I] <= A [J]) {meet the requirements for retrieving the current element of the Left sequence}
Then begin
TMP [T]: = A [I]; Inc (I );
End
Else begin
TMP [T]: = A [J]; Inc (j );
End;
INC (t );
End;
For I: = P to r do a [I]: = TMP [I];
End; {merge}

Procedure merge_sort (var a: listtype; P, R: integer); {merge sort a [P. R]}
VaR Q: integer;
Begin
If P <> r then begin
Q: = (p + R-1) Div 2;
Merge_sort (A, p, q );
Merge_sort (A, q + 1, R );
Merge (A, P, Q, R );
End;
End;
{Main}
Begin
Merge_sort (A, 1, n );
End.

G. Base sorting
Thought: Sort each element by position from low to high


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.