Herong's Tutorial Notes on Sorting
Dr. Herong Yang, Version 5.03

Quicksort

Part:   1  2 

(Continued from previous part...)

Performance

Now let's see how it performs. I tried this implementation with my SortTest.java under JDK 1.3.1. Here are the results:

Array size: 10000
Average sorting time: 18 milliseconds
Number of tests: 1000
Performance: 1.8 O(N) nonaseconds
Performance: 0.13546349804879154 O(N*Log2(N)) nonaseconds
Performance: 1.8E-4 O(N*N) nonaseconds

Array size: 20000
Average sorting time: 48 milliseconds
Number of tests: 1000
Performance: 2.4 O(N) nonaseconds
Performance: 0.16797650570256523 O(N*Log2(N)) nonaseconds
Performance: 1.2E-4 O(N*N) nonaseconds

Array size: 30000
Average sorting time: 82 milliseconds
Number of tests: 1000
Performance: 2.7333333333333334 O(N) nonaseconds
Performance: 0.18378222850546175 O(N*Log2(N)) nonaseconds
Performance: 9.11111111111111E-5 O(N*N) nonaseconds

The performance of this implementation is definitely better than the insertion sort and the selection sort. The numbers tell us that it's between O(N*Log2(N) and O(N*N).

But this implementation is still not good as the JDK implementation java.util.Arras.sort(), which gives about constant performance of 0.157 O(N*Log2(N)).

Improvements

Yes, there are ways to improve this implementation.

First, we can improve the division process by replacing the one-way loop with a two-way loop to reduce the number of swapping operations.

Another area that can be improved is when the sections are being divided smaller enough, like number of elements less than 3, or 4. Instead of dividing them further, we can sort them directly.

Here is my improved version of Quicksort:

/**
 * HyArrays.java
 * This class contains sorting methods similar to java.util.Arrays.
 * All sorting methods should have a signiture of 
 * %Sort(Object[] a, int fromIndex, int toIndex)
 * where "fromIndex" is inclusive, and "toIndex" is exclusive.
 * Copyright (c) 1999 by Dr. Herong Yang
 */
public class HyArrays {
   public static void quickSortImproved(Object[] a, int fromIndex, 
      int toIndex) {
      Object d;
      if (toIndex-fromIndex<=1) {
         return;
      } else if (toIndex-fromIndex==2) {
         if (((Comparable)a[fromIndex]).compareTo(a[toIndex-1])>0) {
            d = a[toIndex-1];
            a[toIndex-1] = a[fromIndex];
            a[fromIndex] = d;
         }
      } else {
      	 int iLeft = fromIndex + 1;
      	 int iRight = toIndex - 1;
      	 Comparable p = (Comparable) a[fromIndex];
      	 while (iLeft<iRight) {
      	    while (iLeft<toIndex-1 && p.compareTo(a[iLeft])>=0) {
      	       iLeft++;
      	    }
      	    while (iRight>fromIndex+1 && p.compareTo(a[iRight])<=0) {
      	       iRight--;
      	    }
      	    if (iLeft>=iRight) break;
            d = a[iRight];
            a[iRight] = a[iLeft];
            a[iLeft] = d;      	       
      	 }
         if (iLeft>fromIndex+1) {
            d = a[iRight];
            a[iRight] = a[fromIndex];
            a[fromIndex] = d;            	
         }
	 if (iLeft==iRight && iRight==toIndex) {
            quickSortImproved(a, fromIndex, iRight);
	 } else if (iLeft==iRight && iRight==fromIndex) {
            quickSortImproved(a, iLeft, toIndex);
	 } else {
            quickSortImproved(a, fromIndex, iRight);
            quickSortImproved(a, iLeft, toIndex);
         }
      }
   }
}

Here is the result of this version:

Array size: 10000
Average sorting time: 16 milliseconds
Number of tests: 1000
Performance: 1.6 O(N) nonaseconds
Performance: 0.12041199826559247 O(N*Log2(N)) nonaseconds
Performance: 1.6E-4 O(N*N) nonaseconds

Array size: 20000
Average sorting time: 40 milliseconds
Number of tests: 1000
Performance: 2.0 O(N) nonaseconds
Performance: 0.13998042141880435 O(N*Log2(N)) nonaseconds
Performance: 1.0E-4 O(N*N) nonaseconds

Array size: 30000
Average sorting time: 70 milliseconds
Number of tests: 1000
Performance: 2.3333333333333335 O(N) nonaseconds
Performance: 0.15688726823636978 O(N*Log2(N)) nonaseconds
Performance: 7.777777777777778E-5 O(N*N) nonaseconds

The results is interesting. The improvement is significant. The performance is much better than my initial version. It actually performs better than the JDK implementation when N = 10000 and 20000.

Part:   1  2 

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on Sorting - Quicksort