Introduction
Sorting algorithms are fundamental in computer science, used to arrange elements in a specific order. Understanding different sorting algorithms helps in choosing the right one for different scenarios based on time and space complexity.
Quick Sort
Quick sort is a divide-and-conquer algorithm that selects a pivot and partitions the array around it.
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
# In-place quick sort
def quick_sort_inplace(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort_inplace(arr, low, pi - 1)
quick_sort_inplace(arr, pi + 1, high)
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
# Test
numbers = [64, 34, 25, 12, 22, 11, 90]
print("Quick Sort:", quick_sort(numbers))
arr = [64, 34, 25, 12, 22, 11, 90]
quick_sort_inplace(arr, 0, len(arr) - 1)
print("In-place Quick Sort:", arr)
Merge Sort
Merge sort divides the array into halves, sorts them, and merges them back together.
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
# Test
numbers = [64, 34, 25, 12, 22, 11, 90]
print("Merge Sort:", merge_sort(numbers))
Heap Sort
Heap sort uses a binary heap data structure to sort elements.
def heap_sort(arr):
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)
# Test
numbers = [64, 34, 25, 12, 22, 11, 90]
heap_sort(numbers)
print("Heap Sort:", numbers)
Comparison
import time
import random
def measure_sort_time(sort_func, arr):
arr_copy = arr.copy()
start = time.time()
sort_func(arr_copy)
return time.time() - start
sizes = [100, 1000, 5000]
for size in sizes:
arr = [random.randint(0, 10000) for _ in range(size)]
print(f"\nArray size: {size}")
print(f" Quick Sort: {measure_sort_time(quick_sort, arr):.4f}s")
print(f" Merge Sort: {measure_sort_time(merge_sort, arr):.4f}s")
arr_copy = arr.copy()
print(f" Heap Sort: {measure_sort_time(heap_sort, arr_copy):.4f}s")
Practice Problems
- Implement quick sort with random pivot selection.
- Write an in-place merge sort implementation.
- Optimize heap sort to handle duplicates efficiently.
- Compare the performance of all three sorting algorithms.
- Implement a hybrid sorting algorithm (TimSort-style).