forked from qwert-27/a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick sort.txt
More file actions
98 lines (75 loc) · 2.85 KB
/
Quick sort.txt
File metadata and controls
98 lines (75 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Quick sort
BEGIN
# ---------- PARTITION FUNCTION ----------
FUNCTION partition(arr, low, high)
pivot ← arr[high] # choose last element as pivot
i ← low - 1 # pointer for smaller element
FOR j FROM low TO high - 1 DO
IF arr[j] <= pivot THEN
i ← i + 1
SWAP arr[i] ↔ arr[j]
ENDIF
ENDFOR
SWAP arr[i + 1] ↔ arr[high]
RETURN (i + 1) # partition index
END FUNCTION
# ---------- QUICK SORT FUNCTION ----------
FUNCTION quick_sort(arr, low, high)
IF low < high THEN
pi ← partition(arr, low, high) # get partition index
CALL quick_sort(arr, low, pi - 1) # sort left subarray
CALL quick_sort(arr, pi + 1, high) # sort right subarray
ENDIF
END FUNCTION
#code
import random
import time
import matplotlib.pyplot as plt
# Partition Function
def partition(arr, low, high):
pivot = arr[high] # choose last element as pivot
i = low - 1 # pointer for smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # swap
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1 # return partition index
# Quick Sort Function (Recursive)
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)
# Function to Measure Time Taken
def measure_time(arr):
start = time.time()
quick_sort(arr, 0, len(arr) - 1)
end = time.time()
return end - start
# Different Input Sizes
sizes = [100, 500, 1000, 2000, 5000, 8000, 10000]
best_times = []
avg_times = []
worst_times = []
for n in sizes:
# Best Case → already sorted array (balanced partitions if pivot is middle)
arr_best = list(range(n))
best_times.append(measure_time(arr_best.copy()))
# Average Case → random elements
arr_avg = [random.randint(0, 100000) for _ in range(n)]
avg_times.append(measure_time(arr_avg.copy()))
# Worst Case → reverse sorted (pivot always largest)
arr_worst = list(range(n, 0, -1))
worst_times.append(measure_time(arr_worst.copy()))
# Plotting Time Complexity
plt.figure(figsize=(8,5))
plt.plot(sizes, best_times, marker='o', color='green', label='Best Case (O(n log n))')
plt.plot(sizes, avg_times, marker='s', color='blue', label='Average Case (O(n log n))')
plt.plot(sizes, worst_times, marker='^', color='red', label='Worst Case (O(n²))')
plt.title('Quick Sort Time Complexity (with Partition Function)')
plt.xlabel('Input Size (n)')
plt.ylabel('Time Taken (seconds)')
plt.grid(True)
plt.legend()
plt.show()