Introduction to MinMax Sort

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.













转载于:https://my.oschina.net/u/2399919/blog/543648

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值