Repository navigation
Expand file tree
/
Copy pathquick_sort.py
More file actions
78 lines (58 loc) · 2.28 KB
/
Copy pathquick_sort.py
File metadata and controls
78 lines (58 loc) · 2.28 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
"""
Quicksort Visualization
This Python script demonstrates the Quicksort algorithm using matplotlib to visualize the sorting process.
Usage:
- The script generates a random list of integers for demonstration purposes.
- The algorithm sorts the list in ascending order, providing a step-by-step visualization.
Dependencies:
- matplotlib: A data visualization library for creating static, animated, and interactive visualizations in Python.
Author: Karthik
Date: 03-02-2024
"""
import matplotlib.pyplot as plt
import numpy as np
def quicksort_visualization(lst, low, high):
"""
Sorts a list using the Quicksort algorithm and visualizes each step.
Parameters:
- lst (list): The input list to be sorted.
- low (int): The starting index of the list or sub-list.
- high (int): The ending index of the list or sub-list.
Returns:
None
"""
def partition(lst, low, high):
"""
Partitions the list and returns the pivot index.
Parameters:
- lst (list): The input list to be partitioned.
- low (int): The starting index of the list or sub-list.
- high (int): The ending index of the list or sub-list.
Returns:
- int: The pivot index.
"""
pivot = lst[high]
i = low - 1
for j in range(low, high):
if lst[j] <= pivot:
i += 1
lst[i], lst[j] = lst[j], lst[i]
lst[i + 1], lst[high] = lst[high], lst[i + 1]
return i + 1
if low < high:
# Find the pivot element such that elements smaller than pivot are on the left, and greater on the right
pivot_index = partition(lst, low, high)
# Visualize the current state of the list using a bar chart
x = np.arange(0, len(lst), 1)
plt.bar(x, lst, color='lightblue')
plt.bar(pivot_index, lst[pivot_index], color='red') # Highlight the pivot element
plt.pause(0.5)
plt.clf()
# Recursively sort the sub-lists on either side of the pivot
quicksort_visualization(lst, low, pivot_index - 1)
quicksort_visualization(lst, pivot_index + 1, high)
# Example usage
amount = 15
lst_to_sort = np.random.randint(0, 100, amount)
# Sorting the entire list
quicksort_visualization(lst_to_sort, 0, len(lst_to_sort) - 1)