Introduction to MinMax Sort
The idea is simple, compare each data element in the list with the current found minimum and maximum value, and the minimum and maximum value for the list can be found after one round of data reading. Following is the algorithm for minmax sort:
Given an array of n pre-sort data elements D[i], index i start from 0 to n-1. The task is to sort the data elements in array D[ ] in ascending order such that D[i] <= D[i+1]
1st iteration of sorting data elements in D[ ], start with index i = 0 to n-1
1. if D[0] > D[n-1], then swap the values between D[0] and D[n-1]
2. For data element D[i],
if D[i] < D[0] then swap the values between D[0] and D[i] //D[i] become the current minimum
else if D[i] >D[n-1] then swap the values between D[n-1] and D[i] //D[i] become the current maximum
else if D[i] < D[i-1] then swap the values between D[i-1] and D[i] //minor position improvement for each data element
If no data value swapping occurs, D[ ] is in ascending order, sorting completed. Otherwise, start 2nd interation of sorting.
2nd iteration of sorting data elements in D[ ], Apply step 1 and 2 of iteration 1 to D[ ], start with index i = 1 to n-2 //D[0] is the minimum vlaue of D[ ] and D[n-1] is the maximum of D[ ], no need to involve in the sorting.
If no data value swapping occurs, D[ ] is in ascending order, sorting completed. Otherwise, start 3rd interation of sorting.
3rd iteration of sorting data elements in D[ ], Apply step 1 and 2 of iteration 1 to D[ ], start with index i = 2 to n-3 //D[0], D[1] is the 1st and 2nd minimum vlaue of D[ ], and D[n-1], D[n-2] is the 1st and 2nd maximum of D[ ], no need to involve in the sorting.
Sorting iteration continue until an iteration with no data value swapping occurs. Total number of iterations is less than or equal to n/2.
The above algorithm is simple and efficient, easy to implement. If the pre-sort data is already in ascending order, sorting will complete after 1st iteration and is the best case scenario O(n). If the pre-sort data is 80% to 90% sorted, it may complete in a few iterations. For worst case scenario, total number of iterations is n/2.
Remark:
For pratical sorting solution, if the pre-sort data amount is huge, use statistical method to acquire sample data and determine the range of data value. Partition the value range into consecutive value slots and apply 1 iteration to assign data elements into those slots. Parallel processing can then apply to those slots with minmax sort.