### Simple to understand Quick Sort

• The "divide and conquer" strategy of Quick Sort

• Divide and conquer strategy used by Quick Sort:

• Select an element from the input array "randomly"

 I put "randomly" in quotes because.... The common practice is to always pick the last element in the input array

• The element that is selected is called:

 The pivot element

• Partition the input array into 2 halves:

 The left half contain only values that are ≤ pivot element The right half contain only values that are > pivot element

(The pivot element is not included in the 2 halves of the partition.)

• Sort both halves of the partitioned arrays

 You can use any sorting algorithm We will use the Bubble Sort

• Concatenate the sorted halves along with the pivot element to obtain the sorted input array

• Example:

• Input array:

• Select a pivot element (pick last element in array):

• Partition the input array based on the pivot element:

• Sort both halves of the partitioned arrays:

• Concatenate the sorted halves along with the pivot element:

• The simple to understand Quick Sort algorithm

• Pseudo code:

• Quick Sort algorithm in Pseudo code:

 ``` QuickSort( double[] a ) { if ( a.length ≤ 1 ) return; // Don't need sorting Select a pivot; // It's usually the last elem in a[] Partition a[] in 2 halves: left[]: elements ≤ pivot right[]: elements > pivot; Sort left[]; Sort right[]; Concatenate: left[] pivot right[] } ```

• Code in Java:

 ``` public static void sort( double[] a ) { double[] left = null, right = null; int nleft, nright; // Length of left[] and right[ double pivot; int i, j, k; if ( a.length <= 1 ) { // Array of 1 element is sorted.... return; } /* ======================================================== Select the "pivot" ======================================================== */ pivot = a[a.length-1]; // Use last element as pivot // This is the default choice... /* ======================================================== Find out how many elements are <= and > than pivot ======================================================== */ nleft = nright = 0; for ( i = 0; i < a.length-1; i++ ) if ( a[i] <= pivot ) nleft++; else nright++; /* ================================================= Make the left and right array of the proper size ================================================= */ left = new double[nleft]; right = new double[nright]; /* ================================================= Partition array into 2 halves: all values <= pivot to left[ ] array all values > pivot to right[ ] array ================================================= */ i = 0; j = 0; for ( k = 0; k < a.length-1; k++ ) if ( a[k] <= pivot ) left[i++] = a[k]; else right[j++] = a[k]; /* ================================================= Sort each half ================================================= */ BubbleSort.sort(left); BubbleSort.sort(right); /* ================================================= Concatenate the pieces back ================================================= */ k = 0; for ( i = 0; i < left.length; i++ ) a[k++] = left[i]; a[k++] = pivot; for ( j = 0; j < right.length; j++ ) a[k++] = right[j]; } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg

• Flash of brilliance: why not use Quick Sort in place of Bubble Sort

• Idea:

• The Quick Sort algorithm can also be used to sort an array

• So, let's replace the use of Bubble Sort in the above algorithm:

 ``` /* ================================================= Sort each half ================================================= */ BubbleSort.sort(left); BubbleSort.sort(right); ```

by Quick Sort:

 ``` /* ================================================= Sort each half ================================================= */ QuickSort.sort(left); QuickSort.sort(right); ```

• The resulting algorithm is a simplified Quick Sort algorithm:

 ``` public static void sort( double[] a ) { double[] left = null, right = null; int nleft, nright; // Length of left[] and right[ double pivot; int i, j, k; if ( a.length <= 1 ) { // Array of 1 element is sorted.... return; } /* ======================================================== Select the "pivot" ======================================================== */ pivot = a[a.length-1]; // Use last element as pivot // This is the default choice... /* ======================================================== Find out how many elements are <= and > than pivot ======================================================== */ nleft = nright = 0; for ( i = 0; i < a.length-1; i++ ) if ( a[i] <= pivot ) nleft++; else nright++; /* ================================================= Make the left and right array of the proper size ================================================= */ left = new double[nleft]; right = new double[nright]; /* ================================================= Partition array into 2 halves: all values <= pivot to left[ ] array all values > pivot to right[ ] array ================================================= */ i = 0; j = 0; for ( k = 0; k < a.length-1; k++ ) if ( a[k] <= pivot ) left[i++] = a[k]; else right[j++] = a[k]; /* ================================================= Sort each half ================================================= */ sort(left); // Use Quick Sort sort(right); // Use Quick Sort /* ================================================= Concatenate the pieces back ================================================= */ k = 0; for ( i = 0; i < left.length; i++ ) a[k++] = left[i]; a[k++] = pivot; for ( j = 0; j < right.length; j++ ) a[k++] = right[j]; } ```

• How the simplified Quick Sort works:

• Input array:

• Select a pivot element (pick last element in array):

• Partition the input array based on the pivot element:

• Sort both halves of the partitioned arrays using 2 other call of Quick Sorts:

• I will only follow the left part (the right part goes one more level deeper)...

The left merge sort will invoke Quick Sort again:

Because the input array has one element, each of the Quick Sort simply return the input array back !!!

• Each one of the Quick Sorts will select its own pivot and partition the array:

• The left Quick Sort will then concatenate the results and returns the sorted left array:

• The right Quick Sort will do this also, but due to space, I will omit the drawing ....

This is the result:

• Finally, the first Merge Sort will concatenate the sorted halves along with its pivot element:

• I have rigged a verbose version of the Merge Sort that prints out what it's doing.

Here is a sample output:

 ```Before sort: [6.4, 3.5, 9.2, 1.1, 8.9, 2.5, 7.5, 4.2] Input = [6.4, 3.5, 9.2, 1.1, 8.9, 2.5, 7.5, 4.2] Pivot = 4.2 Left array = [3.5, 1.1, 2.5] Right array = [6.4, 9.2, 8.9, 7.5] Input = [3.5, 1.1, 2.5] Pivot = 2.5 Left array = [1.1] Right array = [3.5] Input = [1.1] Done. Input = [3.5] Done. Sorted Left array = [1.1] Sorted Right array = [3.5] Concatented pieces = [1.1, 2.5, 3.5] Input = [6.4, 9.2, 8.9, 7.5] Pivot = 7.5 Left array = [6.4] Right array = [9.2, 8.9] Input = [6.4] Done. Input = [9.2, 8.9] Pivot = 8.9 Left array = null Right array = [9.2] Input = [9.2] Done. Sorted Left array = null Sorted Right array = [9.2] Concatented pieces = [8.9, 9.2] Sorted Left array = [6.4] Sorted Right array = [8.9, 9.2] Concatented pieces = [6.4, 7.5, 8.9, 9.2] Sorted Left array = [1.1, 2.5, 3.5] Sorted Right array = [6.4, 7.5, 8.9, 9.2] Concatented pieces = [1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] After sort: [1.1, 2.5, 3.5, 4.2, 6.4, 7.5, 8.9, 9.2] ```

The deeper rcursive calls of Quick Sort will print their information using deeper indentations

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg