This article uses the ascending sorting of integer arrays as an example to list several sort algorithms and corresponding Java implementations. Finally, this article provides performance analysis charts for these algorithms.
1. Insert sorting
Basic Idea: insert an element in each loop to a proper position in the sorted sequence so that the obtained sequence is still ordered.
Implementation:
Void sort (int A []) throws exception {
Int TMP;
Int J;
For (INT I = 1; I <= A. length; I ++ ){
TMP =;
For (j = I-1; j> = 0 & A [J]> TMP; j --){
A [J + 1] = A [J];
}
A [J + 1] = TMP;
}
}
2. Hill sorting
Basic Idea: Take an integer S1 less than N as an increment and divide all elements into S1 groups. All elements with S1 spacing are placed in the same group.
Group 1: {A [1], a [S1 + 1], a [2 * S1 + 1],…}
Group 2: {A [2], a [S1 + 2], a [2 * S1 + 2],…}
Group 3: {A [3], a [S1 + 3], a [2 * S1 + 3],…}
......
S1: {A [S1], a [2 * S1], a [3 * S1],…}
Sort directly in each group. Then, repeat the preceding grouping and sorting in the second incremental S2 (<S1, until the incremental ST = 1 (ST <St-1 <St-2 <... <S2 <S1), that is, all records are placed in the same group for direct insertion sorting.
Implementation:
Void sort (int A []) throws exception {
Int H [] = {109, 41, 19, 5, 1 };
Int TMP, J;
Int incno;
For (TMP = 0; H [TMP]> A. length; TMP ++ );
For (incno = TMP; incno <= H. Length-1; incno ++ ){
For (INT I = H [incno]; I <= A. Length-1; I ++ ){
TMP =;
For (j = I-H [incno]; j> = 0 & A [J]> TMP; j = J-H [incno]) {
A [J + H [incno] = A [J];
}
A [J + H [incno] = TMP;
}
}
}
3. Bubble Sorting
Basic Idea: Compare the data to be sorted in two pairs. If the order of the two data is the opposite, the data is exchanged until there is no reverse order.
Implementation:
Void sort (int A []) throws exception {
For (INT I = A. length; -- I> = 0 ;){
For (Int J = 0; j <I; j ++ ){
If (A [J]> A [J + 1]) {
Int T = A [J];
A [J] = A [J + 1];
A [J + 1] = T;
}
}
}
}
4. Select sorting
Basic Idea: Starting from the last element, select the maximum element position of the record from the data to be sorted, and repeat this operation to switch the element from the last element of the sequence that has not been sorted, until all data is sorted.
Implementation:
Void sort (int A []) throws exception {
For (INT I = A. length; -- I> = 0 ;){
Int largest = 0;
For (Int J = 1; j <= I; j ++ ){
If (A [Largest] <A [J]) {
Largest = J;
}
}
If (largest! = I ){
Int T = A [Largest];
A [Largest] =;
A = T;
}
}
}
5. Fast sorting
Basic Idea: In a [1 .. n] Any data element is used as the benchmark for comparison (may be recorded as X). The data partition is divided into two parts: a [1 .. i-1] And a [I + 1 .. n], and a [1 .. i-1] ≤ x ≤ A [I + 1 .. n] (1 ≤ I ≤ n), when a [1 .. i-1] And a [I + 1 .. n] when they are not empty, they are divided separately until all data elements are sorted. This "benchmark" is implemented using the Sequence Value
Implementation:
Void quicksort (int A [], int low, int HIG) throws exception
{
Int Lo = low;
Int HI = hig;
Int mid;
If (HIG> LOW)
{
Mid = A [(log + hiw)/2];
While (Lo <= Hi)
{
While (Lo <HIG) & (A [lo] <mid ))
++ Lo;
While (HI> LOW) & (A [HI]> mid ))
-- Hi;
If (Lo <= Hi)
{
Swap (A, lo, hi );
++ Lo;
-- Hi;
}
}
If (low
Quicksort (A, low, hi );
If (Lo <HIG)
Quicksort (A, lo, hiw );
}
}
Private void swap (int A [], int I, Int J)
{
Int T;
T =;
A = A [J];
A [J] = T;
}
Public void sort (int A []) throws exception
{
Quicksort (A, 0, A. Length-1 );
}
6. Heap sorting
Basic Idea: heap sorting is a kind of tree-based sorting. During the sorting process, a [n] is regarded as a sequential storage structure of a Complete Binary Tree, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.
Implementation:
Void sort (int A []) throws exception {
Int I;
For (I = A. Length/2; I> = 0; I --){
Perc_down (I, A, A. Length-1 );
}
For (I = A. Length-1; I> = 0; I --){
Delete_max (I, );
}
}
Void delete_max (int ix, int A []) throws exception {
Int ret;
Ret = A [0];
A [0] = A [ix];
Perc_down (0, A, ix-1 );
A [ix] = ret;
}
Void perc_down (int ix, int A [], int LNG) throws exception {
Int I, TMP;
TMP = A [ix];
I = IX;
While (I * 2 + 1 <= LNG ){
If (I * 2 + 1 = LNG | A [I * 2 + 1]> A [I * 2 + 2]) {
If (A [I * 2 + 1] <TMP)
Break;
A = A [I * 2 + 1];
I = I * 2 + 1;
} Else {
If (A [I * 2 + 2] <TMP)
Break;
A = A [I * 2 + 2];
I = I * 2 + 2;
}
}
A = TMP;
}
7. Merge and sort
Basic Idea: There are two ordered (ascending) sequences stored in the adjacent positions of the same array. It may be set to a [L .. m], a [M + 1 .. h], merge them into an ordered series, and store them in a [L .. h].
To reduce the number of data moves, you can use a temporary working array C to temporarily Save the intermediate sorting result in the C array. after merging, copy the C array value to.
Implementation:
Void sort (int A []) throws exception {
Mergesort (A, 0, A. Length-1 );
}
Private void Merge (int A [], int begin, int M, int end) throws exception {
Int I = begin, j = m + 1, K = 0;
Int [] TMP = new int [end-begin + 1];
While (I <= M & J <= END ){
If (A> A [J]) {
TMP [k ++] = A [J ++];
} Else {
TMP [k ++] = A [I ++];
}
}
If (I = m + 1 ){
For (; j <= end; j ++ ){
TMP [k ++] = A [J];
}
} Else {
For (; I <= m; I ++ ){
TMP [k ++] =;
}
}
For (k = 0; k <TMP. length; k ++ ){
A [begin + k] = TMP [k];
}
}
Private void mergesort (int A [], int begin, int end) throws exception {
If (end-begin> 0 ){
Int M = (end-begin)/2;
Mergesort (A, begin, begin + M );
Mergesort (A, begin + m + 1, end );
Merge (A, begin, begin + M, end );
}
}
Performance Comparison
Time Complexity
Space complexity
Stability
1
Insert sort
O (n2)
1
√
2
Hill sorting
O (n2)
1
×
3
Bubble Sorting
O (n2)
1
√
4
Select sort
O (n2)
1
×
5
Quick sorting
O (nlogn)
O (logn)
×
6
Heap sorting
O (nlogn)
1
×
7
Merge Sorting
O (nlogn)
O (N)
√
This article from: China self-learning Programming Network (www.zxbc.cn) detailed source reference: http://www.zxbc.cn/html/sf/181320448963.html