Min-max sort: a simple sorting method (abstract only)

  • Authors:
  • Narayan Murthy

  • Affiliations:
  • Computer Science Department, Pace University, Pleasantville, New York

  • Venue:
  • CSC '87 Proceedings of the 15th annual conference on Computer Science
  • Year:
  • 1987

Quantified Score

Hi-index 0.00

Visualization

Abstract

A simple sorting algorithm which can be considered as a double-ended selection sort is presented. The algorithm, called min-max sort is based on the optimal method for simultaneously finding the smallest and the largest elements in an array [2]. The smallest and the largest elements found are pushed respectively to the left and right of the array, and the process is repeated on the middle portion. The correctness and termination of min-max sort are easily demonstrated through the use of standard loop invariant ideas. The algorithm fits the “hard split/easy join” paradigm of Merritt [1].Let n be even. Given an array A[1..n], it is said to be in a “rainbow pattern” if for each i := 1 to n/2 we have A [i] ≤ A[n+1-i]. The following step 1 establishes the rainbow pattern in A.Step 1 : FOR i := 1 TO n DIV 2 DO IF A[i] A[n+1-i] THEN swap(A[i],A[n+1-i]).The step 1 requires n/2 number of comparisons. Observe that when an array is in a rainbow pattern it is guaranteed that the smallest element of the array is in the left half of the array and the largest in the second half.Step 2 is to find p and q such that A[p] ≤ A[i] for i = 1 to n/2, and A[q] ≥ A[j] for j = n/2+1 to n, and swap(A[1],A[p]) and swap(A[q],A[n]).The step 2 requires (n/2-1)+(n/2-1) number of comparisons. Thus we have brought the largest and the smallest of elements of A into first and the last positions respectively at a cost of 3n/2 - 2 comparisons (in contrast to 2n - 2 comparisons required in the straight forward method).Observe that the rainbow pattern in A is still preserved, except perhaps the two pairs (A[p],A[n+1-p]) and (A[q],A[n+1-q]). The rainbow pattern is reinstated, in just two comparisons, byStep 3 : IF A[p] A[n+1-p] then swap(A[p],A[n+1-p]), and IF A(n+1-q] A[q] then swap(A[q],A[n+1-q]).The general form of step 2 is Step 2′ : find p and q such that A[p] ≤ A[i] for all i = k to n/2, and A[q] ≥ A[j] for all j = n/2+1 to n+1-k. The algorithm step 1; FOR k := 1 TO n DIV 2-1 DO BEGIN step 2′; step 3; END; sorts the array A.Step 1 requires N/2 comparisons; step 2′ requires 2(N/2-k) and step 3 requires two comparisons. Thus the total number of comparisons required to sort the array A is = N/2 + @@@@ [2(N/2 - k) + 2] = (N2 + 4N - 8)/4The size of the central portion decreases steadily by 2 and the invariant assures that the smallest and largest of the central portion can be found with a number of comparisons approximately equal to the size of the portion. The algorithm is included in discussion by Zahn [3].