Sorting Algorithm:
1. rank sort)
Calculate the ranking first:
Template <class T>
Void rank (t a [], int N, int R [])
{// Calculate the ranking of n elements in a [0: n-1]
For (INT I = 0; I <n; I ++)
R [I] = 0; // Initialization
For (INT I = 0; I <n; I ++)
For (Int J = 0; j <n; j ++)
If (A [J] <= A [I])
R [I] ++;
Else
R [J] ++;
}
You can estimate the time complexity of a program based on the comparison operations between elements of A. These comparison operations are completed by the if statement. The total number of comparisons is (n-1) n/2.
Sort by ranking:
Template <class T>
Void rearrange (t a [], int N, int R [])
{// Sort the elements in array A in order, and use the additional array u
T * u = new T [n + 1];
// Move to the correct position in u
For (INT I = 0; I <n; I ++)
U [R [I] = A [I];
For (INT I = 0; I <n; I ++)
A [I] = U [I];
Delete [] U;
}
The number of elements moved during the execution of the rearrange function is 2n. To complete the sorting, perform (n-1) n/2 Comparison operations and 2n moving operations.
Another function to rearrange elements is as follows. This function does not use an additional array U.
Assume that the rank function has been used to calculate the ranking of each element in an array.
Template <class T>
Void rearrange (t a [], int N, int R [])
{// Rearranging array elements in situ
For (INT I = 0; I <n; I ++)
// Obtain the elements that should be placed at a [I ].
While (R [I]! = I)
{
Int T = R [I];
Swap (A [I], a [T]);
Swap (R [I], R [T]);
}
}
Template <class T>
Inline void swap (T & A, T & B)
{
T temp = A; A = B; B = temp;
}
The minimum number of exchanges executed by this program is 0 (the initial array is already sorted in order), and the maximum number of exchanges is 2 (n-1 ). When this function is used to replace the preceding function, the execution time required increases in the worst case because more elements need to be moved (three times for each exchange ), however, the memory required by the program is reduced.
In summary, the two counting sorting methods have their own advantages and disadvantages.
2. selection sort)
Template <class T>
Void selectionsort (t a [], int N)
{// Sorts n elements in array a [0: n-1]
For (INT size = N; size> 1; size --)
{
Int J = max (A, size );
Swap (A [J], a [size-1]);
}
}
Template <class T>
Int max (t a [], int N)
{// Find the maximum element in a [0; n-1]
Int Pos = 0;
For (INT I = 1; I <n; I ++)
If (A [POS] <A [I]
Pos = I;
Return Pos;
}
Each call to max (A, size) requires a size-1 comparison, so the total number of comparisons is 1 + 2 + 3 +... + n-1 = (n-1) n/2. The number of elements to be moved is 3 (n-1 ). The number of comparisons required for sorting is the same as the number of comparisons required for ranking, but the number of elements required to be moved is 50% more.
One disadvantage of the sorting function is that the program continues to run even if the elements are arranged in order. To terminate unnecessary loops, you can check whether the arrays are sorted in order during the search for the largest element. The following program provides a selection sorting function based on this idea. In this function, the loop for finding the largest element is directly merged with the selectionsort function, instead of using it as an independent function.
Template <class T>
Void selectionsort (t a [], int N)
{// Sorting of timely termination options
Bool sorted = false;
For (INT size = N ;! Sorted & (size> 1); size --)
{
Int Pos = 0;
Sorted = true;
// Find the largest element
For (INT I = 1; I <size; I ++)
If (A [POS] <= A [I])
Pos = I; // if it has been sorted in order, there will be no chance of else, sorted will always be true, and the redmost will terminate the external for loop.
Else
Sorted = false; // non-ordered, indicating that an external for loop is required
Swap (A [POS], a [size-1]);
}
}
The best case is that array A is an ordered array. At this time, the external for loop is executed only once, and the number of comparisons between elements in a is n-1. In the worst case, the external for loop continues until size = 1, and the number of comparisons executed is (n-1) n/2. The number of exchanges in the best and worst cases is exactly the same as the previous optimization program, but in the worst case, the optimized program may be slightly slower, this is because it requires additional work for variable maintenance.
To sum up, the optimized sorting function is the best choice.
3. bubble sort)
Template <class T>
Void bubble (t a [], int N)
{// Move the maximum element in array a [0: n-1] To the right through bubbling
For (INT I = 0; I <n-1; I ++)
If (A [I]> A [I + 1])
Swap (A [I], a [I + 1]);
}
Template <class T>
Void bubblesort (t a [], int N)
{// Bubble sort the n elements in array a [0: n-1]
For (INT I = N; I> 1; I --)
Bubble (A, I );
}
The number of element comparisons is (n-1) n/2, which is the same as selectionsort.
As with sorting, You can redesign a timely terminated Bubble Sorting function. If element swaps are not performed during a bubble process, the table is sorted in order and there is no need to continue the bubble process. The following program provides a Bubble Sorting function for timely termination.
Template <class T>
Bool bubble (t a [], int N)
{// Bubble the largest element in a [0: n-1] To the right end
Bool swapped = false; // exchange has not occurred
For (INT I = 0; I <n-1; I ++)
If (A [I]> A [I + 1])
{
Swap (A [I], a [I + 1]);
Swapped = true;
}
Return swapped;
}
Template <class T>
Void bubblesort (t a [], int N)
{// Timely termination of the bubble Program
For (INT I = N; I> 1 & bubble (A, I); I --);
}
In the worst case, the number of comparisons executed is the same as that of the original function. In the best case, the number of comparisons is n-1.
4. insertion sort)
Template <class T>
Void insert (t a [], int N, const T & X)
{// Insert element x into the ordered array a [0: n-1]
Int I;
For (I = n-1; I> = 0 & x <A [I]; I --)
A [I + 1] = A [I];
A [I + 1] = X;
}
Template <class T>
Void insertionsort (t a [], int N)
{// Sort a [0: n-1]
For (INT I = 1; I <n; I ++)
{
T = A [I];
Insert (a, I, T );
}
}
In the best case, the number of comparisons is n-1, and in the worst case, the number of comparisons is (n-1) n/2.
To sum up, the rank function for ranking calculation is used using the "appending array elements" and "rearranging array elements in situ", with the number of comparisons being (n-1) n/2.
However, the number of moving elements is different. You need to perform a 2n moving operation for "appending array elements", while the minimum number of switching times for "rearranging array elements in situ" is 0, the maximum number of exchanges is 2 (n-1). Note that each exchange needs to be moved three times.
The total number of comparisons for "select sort" is (n-1) n/2, but the number of elements moved is 3 (n-1), which is 50% more than the number of moves required for sorting. "Sort by timely termination" the best case is that the number of comparisons is n-1. In the worst case, the number of comparisons is (n-1) n/2.
The total number of comparisons for "Bubble Sorting" is (n-1) n/2, and the number of Moving Elements depends on the specific data, "timely terminated Bubble Sorting" is compared with n-1 in the best case, and the worst case is (n-1) n/2.
In the best case of "insert sort", the number of comparisons is n-1, and in the worst case, the number of comparisons is (n-1) n/2.