Quick Sort
Advanced
Welcome to the world of sorting! Today, we're going to learn about a cool sorting algorithm called Quick Sort. It's like a super quick way to get your numbers in order!
Quick Sort is a fun algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The process is then applied recursively to the sub-arrays. Let's bring this into action!
Here's what you need to do:
- Choose a 'pivot' element from the array.
- Partition the array into two parts based on the pivot.
- Recursively apply the same process to the two parts.
Let's implement the quick sort function!
Examples
quicksort([3, 6, 8, 10, 1, 2, 1]) # returns [1, 1, 2, 3, 6, 8, 10] quicksort([5, 3, 8, 4, 2]) # returns [2, 3, 4, 5, 8] quicksort([1, 2, 3, 4, 5]) # returns [1, 2, 3, 4, 5]
Have fun sorting away with Quick Sort!
Inputs
Arg 1:
[3,6,8,10,1,2,1]Expected Output
[1, 1, 2, 3, 6, 8, 10]Actual Output