Sorting Networks



February 01, 1993
URL:http://www.drdobbs.com/sorting-networks/184402663

February 1993/Sorting Networks

Frederick Hegeman is an amateur programmer and computer language hobyist. He can be reached at P.O. Box 2368, Rapid City, SD 57709, telephone (605) 343-7014.

A sorting network sorts n items by performing a predetermined set of comparisons. A software implementation for a sequential computer might take the form

swap(1,2); swap(4,5); swap(3,5);
swap(3,4); swap(1,4); swap(1,3);
swap(2,5); swap(2,4); swap(2,3);
where swap(a,b) = if(b < a) exchange a and b;
This is a nine-comparison network for sorting five items. You might try it with pennies, nickels, dimes, quarters, and cheeseburgers. The best-, worst-, and typical-case number of comparisons are the same. Only the number of exchanges varies. Nine comparisons are, in fact, the fewest necessary to sort five items by exchanges when any combination of items is possible. When n is known to be small, these simple predetermined sequences out-perform the best algorithmic sorts.

In theory, the minimum number of comparisons necessary to sort n items is the ceiling of log2(n!). As n grows larger, 1og2(n!) approaches nlog2(n). The reason why O(nlog2(n)) sorting algorithms, such as Quicksort, are so efficient as n grows larger should be obvious. Sorting networks generated by the Bose-Nelson algorithm (See Listing 1. ) are O(n1.585), which diverges from nlog2(n) rapidly. However, the Bose-Nelson network for 16 elements is 65 comparisons, which is pretty nearly nlog2(n), and sorting 1000 random 16-element arrays using a Quicksort that pivoted on the last element in the array requires 85.43 comparisons on average, which is just over n1.585. This is probably the reverse of what you might have expected. Sorting is so often discussed in terms of increasing n that it is easy to fall into the trap of expecting "efficient" algorithms to behave nicely over the whole range of their inputs. The "gotcha" in Quicksort is the extra comparisons needed to properly partition the array. They are insignificant when sorting a large array but constitute a significant part of the total when the array is small. As n becomes very small, the behavior of sorting networks comes closer to the ideal.

Bose-Nelson generates minimum comparison networks only for n <= 8. However, minimum comparison networks are available for 9 <=n<=16 as well. Those generated by the code in Listing 2 are based on illustrations in The Art of Computer Programming, Vol. 3 (Knuth 1973). Please note that both listings generate sorts according to element numbers — one greater than the corresponding array index.

Sorting small arrays is a problem all its own, difficult to understand in the usual terms. If you need to sort a small array in a time-critical section of code, you can count on only two things holding true: your algorithmic sort may perform much worse than expected, and no algorithmic sort can match the minimum comparison networks on all combinations of elements. When that code is part of something like an operating system, the networks have at least two other attractive features: many comparisons can be done in parallel, if appropriate; and they are inherently re-entrant, so that sorting may be interleaved with other tasks.

Bibliography

Knuth, Donald E. 1973. The Art of Computer Programming, Vol. 3. Reading, MA: Addison-Wesley. Pp. 220-229.

Bose, R. C. and Nelson, R. J. 1962. "A Sorting Problem". JACM, Vol. 9. Pp. 282-296.

Davis, Wilbon. September, 1992. "Time Complexity". The C Users Journal, Vol. 10, No. 9. Pp. 29-38.

February 1993/Sorting Networks/Listing 1

Listing 1 Bose-Nelson algorithm for generating sorting networks

/* Calling bose(n) generates a network
 * to sort n items. See R. C. Bose & R. J. Nelson,
 * "A Sorting Problem", JACM Vol. 9, Pp. 282-296. */
bose(n)
int n;
{
   Pstar(1, n); /* sort the sequence {X1,...,Xn} */
}

P(i, j)
int i, j;
{
   printf("swap(%d, %d);\n", i, j);
}

Pstar(i, m)
int i;  /* value of first element in sequence */
int m;  /* length of sequence */
{
   int a;

   if(m > 1)
   {
      /* Partition into 2 shorter sequences,
       * generate a sorting method for each,
       * and merge the two sub-networks. */
      a = m/2;
      Pstar(i, a);
      Pstar((i + a), (m - a));
      Pbracket(i, a, (i + a), (m - a));
   }
}

Pbracket(i, x, j, y)
int i;  /* value of first element in sequence 1 */
int x;  /* length of sequence 1 */
int j;  /* value of first element in sequence 2 */
int y;  /* length of sequence 2 */
{
   int a, b;

   if(x == 1 && y == 1)
      P(i, j); /* 1 comparison sorts 2 items */
   else if(x == 1 && y == 2)
   {
      /* 2 comparisons inserts an item into an
       * already sorted sequence of length 2. */
      P(i, (j + 1));
      P(i, j);
   }
   else if(x == 2 && y == 1)
   {
      /* As above, but inserting j */
      P(i, j);
      P((i + 1), j);
   }
   else
   {
      /* Recurse on shorter sequences, attempting
       * to make the length of one subsequence odd
       * and the length of the other even. If we
       * can do this, we eventually merge the two. */
      a = x/2;
      b = (x & 1) ? (y/2) : ((y + 1)/2);
      Pbracket(i, a, j, b);
      Pbracket((i + a), (x - a), (j + b), (y - b));
      Pbracket((i + a), (x - a), j, b);
   }
}
/* End of File */
February 1993/Sorting Networks/Listing 2

Listing 2 Minimum comparison network pairs

/* 10 items - 29 comparisons; 9 - 25 */
int net10[29][2] = {
   {2,9},{1,5},{6,10},{3,7},{4,8},{1,4},{7,10},{3,6},
   {1,2},{4,7},{9,10},{5,8},{1,3},{5,9),{2,6),{8,10},
   {2,3},{4,5},{6,7},{8,9),{2,4},{7,9},{3,5},{6,8},
   {3,4},{7,8},{4,6},{5,7},{5,6}};

/* 12 items - 39 comparisons; 11 - 35 */
int net12[39][2] = {
   {1,2},{3,4},{5,6},{7,8},{9,10},{11,12},{2,4},{6,8},
   {10,12},{1,3},{5,7},{9,11},{2,3},{6,7},{10,11},
   {2,6},{7,11},{6,10},{3,7},{2,6},{7,11},{1,5},{8,12},
   {4,8},{5,9},{1,5},{8,12},{2,5},{8,11},{4,9},{3,4},
   {9,10},{3,5},{8,10},{4,6},{7,9},{4,5},{6,7},{8,9}};

/* 16 items - 60 comparisons; 15 - 56; 14 - 51; 13 - 46 */
int net16[60][2] = {
   {1,2},{3,4},{5,6},{7,8},{9,10},{11,12},{13,14},
   {15,16},{1,3},{5,7},{9,11},{13,15},{2,4},{6,8},
   {10,12},{14,16},{1,5},{9,13},{2,6},{10,14},{3,7},
   {11,15},{4,8},{12,16},{1,9},{2,10},{3,11},{4,12},
   {5,13},{6,14},{7,15},{8,16},{6,11},{7,10},{4,13},
   {8,12},{14,15},{2,3},{5,9},{2,5},{8,14},{3,9},
   {12,15},{3,5},{6,7},{10,11},{12,14},{4,9},{8,13},
   {7,9},{4,6},{8,10},{11,13},{4,5},{6,7},{8,9},{10,11},
   {12,13},{7,8},{9,10}};

/* Extracts a network for n items from an array of
 * comparison pairs for m items when n <= m. Expects
 * the 2nd member of each pair to be the larger. For
 * example, to extract a minimum comparison network
 * for 9 items call
 * extract(9, sizeof(net10)/(2*sizeof(int)), net10); */
extract(n, m, network)
int n, m;
int network[][2];
{
   int i;

   for(i = 0; i < m; i += 1)
   {
      if(network[i][1] <= n)
         printf("swap(%d, %d);\n",
            network[i][0] , network[i][1]);
   }
}
/* End of File */

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.