1. Introduction
Today, we will discuss one of the effective sorting algorithms using Python, known as Quick Sort. Quick Sort has an average time complexity of O(n log n) and is, in practice, a very fast and efficient sorting algorithm. In this process, we will examine the working principle of Quick Sort and learn how to implement it in Python.
2. What is Quick Sort?
Quick Sort is a type of divide-and-conquer algorithm that divides an array into two sub-arrays and recursively sorts each sub-array to sort the entire array. The key to this process is to choose a reference value called ‘pivot’ and partition the array based on that value.
2.1 Steps of Quick Sort
- Select the pivot value from the array.
- Partition the array into two sub-arrays based on the pivot. The first sub-array consists of values less than the pivot, while the second sub-array consists of values greater than the pivot.
- Recursively repeat the above steps to sort the sub-arrays.
- Once all sub-arrays are sorted, the entire array will be sorted.
2.2 Pivot Selection
There are several methods for selecting the pivot, and generally, one of the first element, last element, or middle element is chosen. Additionally, there is a method to select the pivot randomly, which helps maintain an O(n log n) performance even in the worst-case scenario.
3. Problem Solving
3.1 Problem Description
Write a function that takes an input integer array and returns a sorted array. You must use the Quick Sort algorithm.
3.2 Input and Output Format
- Input: Integer array
arr = [3, 6, 8, 10, 1, 2, 1]
- Output: Sorted array
[1, 1, 2, 3, 6, 8, 10]
3.3 Solution Process
Let’s examine the process of implementing the Quick Sort algorithm in Python step by step.
3.3.1 Pivot Selection and Array Partitioning
First, we will write a function to select the pivot and partition the array based on the pivot. Below is the code that performs this task.
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
3.3.2 Implementing the Quick Sort Function
Now we will implement a function that recursively sorts the partitioned array.
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)
3.3.3 Complete Code
Using the functions we have written above, the complete Quick Sort code is as follows.
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)
arr = [3, 6, 8, 10, 1, 2, 1]
quick_sort(arr, 0, len(arr) - 1)
print("Sorted array:", arr)
4. Advantages and Disadvantages of Quick Sort
4.1 Advantages
- Generally has a time complexity of O(n log n), making it fast.
- The recursive nature allows for concise code.
- Supports in-place sorting, leading to minimal additional memory usage.
4.2 Disadvantages
- In the worst case (e.g., already sorted array), it can have a time complexity of O(n^2).
- Excessive recursive calls may lead to stack overflow.
5. Conclusion
In this lecture, we explored the implementation process of the Quick Sort algorithm using Python. While Quick Sort is an effective sorting method in many situations, there are points of caution to be aware of when using it. Understanding the properties of the algorithm and applying it correctly in appropriate situations is important.
I hope this has been helpful in understanding Quick Sort. In the next lecture, we will delve into other sorting algorithms. Thank you!