Hello, curious learners! Today, we will embark on a journey through the Quick Sort world. Picture yourself organizing various items - like toys or books - by size or color. That's what Quick Sort does with spectacular speed. Are you ready to explore further? Fantastic! Let's get started.
Quick Sort is a clever little algorithm invented by a British computer scientist named Tony Hoare in 1959. It uses a strategy called 'divide-and-conquer' to put elements in order. Quick Sort takes an array, selects a particular "pivot" element, and then places everything smaller than the pivot on one side and everything larger on the other.
Quick Sort has a three-step process:
- Pick a random "pivot" element from the array.
- Move all elements smaller than the pivot to one side and bigger ones to the other. This operation effectively divides the array into two parts, guaranteeing that all the elements will be kept within their part until the end of the sorting process.
- Repeat steps 1 and 2 for each part until there are no more unsorted elements.
For example, if we have nine marbles numbered [3, 9, 4, 7, 5, 1, 6, 2, 8]
and our chosen marble, or pivot, is 7
, then after one round of sorting, we'll get [3, 4, 5, 1, 6, 2, 7, 9, 8]
. It seems that this is a minor change, but now the pivot element is in its correct position, and we can think of the first half of the array [3, 4, 5, 1, 6]
and [9, 8]
separately as they won't ever intersect again.
Let's translate these steps into a concrete Java
program. We'll tackle it part by part. Our first step is to partition an array around a pivot. In the Java
world, we need to write a method, let's call it partition()
, to handle this:
Java1int partition(int[] arr, int start, int end) { 2 int pivot = arr[end]; // choosing the last element as pivot 3 int i = (start - 1); // marking the index of smaller element 4 5 for (int j = start; j < end; j++) { 6 // checking if the current element is smaller than the pivot 7 if (arr[j] <= pivot) { 8 i++; 9 10 // swap arr[i] and arr[j] 11 int temp = arr[i]; 12 arr[i] = arr[j]; 13 arr[j] = temp; 14 } 15 } 16}
In this portion of the code, we selected the last element as the pivot and placed smaller elements on the left.
- The function starts by initializing
i
to one index before thestart
. Thisi
basically keeps track of the latest position where an element has been swapped because it was less than or equal to the pivot. Ifarr[j]
is less than or equal to the pivot,i
is incremented and thenarr[j]
is swapped witharr[i]
. Essentially, smaller elements get pushed towards the front of the array (or the given part of the array).
The start
and end
parameters control which part of the given array is under the partition operation. Using these parameters, we can apply partition to some part of the array, which will be helpful later.
After partitioning, we still need to place the pivot properly in the already partitioned list. We'll add this in the next part of our partition()
method:
Java1 // Swap arr[i+1] and arr[end] (or pivot) 2 int temp = arr[i+1]; 3 arr[i+1] = arr[end]; 4 arr[end] = temp; 5 6 return (i+1); // return the partition point
Now our partition()
method is complete! It partitions the array around the pivot and ensures it is in its correct position.
Next up is the quickSort()
method. It will use our partition()
method to sort the left and right portions of the array recursively. Let's code that step-by-step. First, it should call the partitioning process:
Java1void quickSort(int[] arr, int start, int end) { 2 if (start < end) { 3 int pivot_index = partition(arr, start, end); 4 } 5 // Ready to split! 6}
This function has yet to do much, but it's a strong start. We've managed to partition our list around a pivot point.
We must teach our quickSort()
method to keep sorting smaller and larger partitions. We do this by simply calling itself recursively for these partitions:
Java1void quickSort(int[] arr, int start, int end) { 2 if (start < end) { 3 int pivot_index = partition(arr, start, end); 4 quickSort(arr, start, pivot_index - 1); // sort left part 5 quickSort(arr, pivot_index + 1, end); // sort right part 6 } 7}
And that's it! Our Quick Sort implementation is complete. It initially partitions the array and then continues sorting each partition until everything is sorted.
The efficiency or "time complexity" of Quick Sort varies. When sorting items, usually the more unique items, the quicker it is. In the "best" and "average" situations, Quick Sort works like a charm with a time complexity of . However, in the "worst" situation, where many items are the same (like a pile of identical blocks), it may take more time, resulting in a time complexity of .
Great job! We've untangled the concept of Quick Sort, broken it down piece by piece, and implemented it in Java. Now comes the fun part: we will reinforce what you've learned with practical exercises. Ready to dive in?