Example output
### Explanation of Quick Sort:
Quick Sort is a **divide-and-conquer** algorithm that sorts an array by repeatedly partitioning it into two subarrays, sorting the subarrays, and then combining them. The key steps involved are:
1. **Pivot Selection:** A pivot element is chosen from the array (commonly the last element).
2. **Partitioning:** The array is rearranged such that all elements smaller than the pivot are placed to its left, and all larger elements are placed to its right.
3. **Recursive Sorting:** The two subarrays (left and right of the pivot) are recursively sorted.
4. **Base Case:** Arrays with one or zero elements are inherently sorted.
The time complexity of Quick Sort is:
- **Best and average case:** O(n log n)
- **Worst case:** O(n²) (can happen if the pivot is always the smallest or largest element, but this can be mitigated with good pivot selection strategies).
---
### Python Code Implementation of Quick Sort:
```python
def quick_sort(arr):
"""
Function to perform Quick Sort on an array.
Args:
arr (list): List of elements to be sorted.
Returns:
list: Sorted list of elements.
"""
# Base case: If the array is empty or has a single element, it's already sorted
if len(arr) <= 1:
return arr
# Choose a pivot (using the last element in this case)
pivot = arr[-1]
# Partition the array into three parts
# Left: Elements less than the pivot
# Middle: Elements equal to the pivot
# Right: Elements greater than the pivot
left = [x for x in arr[:-1] if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr[:-1] if x > pivot]
# Recursively sort the left and right partitions, then combine
return quick_sort(left) + middle + quick_sort(right)
# Example usage to test the quick_sort function
if __name__ == "__main__":
# Test array
arr = [3, 6, 8, 10, 1, 2, 1]
# Output the sorted array
print("Original Array:", arr)
sorted_arr = quick_sort(arr)
print("Sorted Array:", sorted_arr)
```
### Example Explanation:
- We define the `quick_sort` function that sorts an array by recursively partitioning it.
- The `pivot` is chosen as the last element in the array.
- The array is split into three parts:
- `left`: Elements smaller than the pivot.
- `middle`: Elements equal to the pivot.
- `right`: Elements greater than the pivot.
- The function recursively sorts the `left` and `right` partitions and then combines them with the `middle` section.
- If the array is empty or has one element, it's returned as is (base case).
### Testing the Code:
If you run the example with the input `[3, 6, 8, 10, 1, 2, 1]`, the output will be:
```
Original Array: [3, 6, 8, 10, 1, 2, 1]
Sorted Array: [1, 1, 2, 3, 6, 8, 10]
```
### Edge Case Handling:
- **Empty array:** Returns an empty array.
- **Single-element array:** Returns the array as is.
- **Duplicate values:** Handles them correctly by including a `middle` list with equal elements.
- **Already sorted array:** Works efficiently as the partitioning will still break it into smaller parts without unnecessary steps.