Name Best-case Average-case Worst-case Memory Stable
QuickSort n log n n log n n2 average: log n
worst: n
no

QuickSort


Your browser does not support the HTML5 canvas tag.

Java Code

public void quickSort(int[] array, int left, int right) {
     int i, j, v;
     while ( right > left ) {
          j = right;
          i = left - 1;
          v = array[ right ];

          while ( true ) {
               do {
                    i++;
               } while ( array[ i ] < v && i < j );
               do {
                    j--;
               } while ( array[ j ] > v && i < j );

               if ( i >= j )
                    break;

               swapKeys( array , i , j );

          }

          swapKeys( array , i , right );

          if (( i - 1 - left ) <= ( right - i - 1 )) {
               quickSort( array , left , i - 1 );
               left = i + 1;
          } else {
               quickSort( array , i + 1 , right );
               right = i - 1;
          }
     }
}

public void swapKeys(int[] array, int i, int j) {
     int temp;
     temp = array[ i ];
     array[ i ] = array[ j ];
     array[ j ] = temp;
} 
            

How to use

Use the textfield to type in a number and add it by either pressing ENTER or by clicking on the "Add" button.
You can also add 10 random numbers at once by clicking on the "10 Random Keys" button. Overall you can add up to 50 keys.
The "Sort" button starts to sort the keys with the selected algorithm. Alternatively you can sort 100 random keys fast for a quick impression of how the algorithm works.