-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathquicksort.py
87 lines (76 loc) · 1.75 KB
/
quicksort.py
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
a = []
# creating an empty list to store the elements of the quicksort
def partition(a, low, high):
i = low - 1
ele = a[high]
for j in range(low, high):
if a[j] < ele:
i = i + 1
a[j], a[i] = a[i], a[j]
a[high], a[i + 1] = a[i + 1], a[high]
return i + 1
# The partition function is used to get the pivotal element and sort in such a way that all the elements to the
# left of it are less than the pivotal element and all the elements to the right of the pivotal elements are greater than it.
def quicksort(a, low, high):
if low < high:
ele = partition(a, low, high)
quicksort(a, low, ele - 1)
quicksort(a, ele + 1, high)
# The quicksort function is used to sort the elements to the left and right of the pivotal element which we get after the
# partition function.
print("Enter the value of n to be sorted:")
n = int(input())
print("Enter the {} values to be sorted:".format(n))
for k in range(n):
u = int(input())
a.append(u)
quicksort(a, 0, n - 1)
print("The elements after the quick sort are:")
for k in range(n):
print(a[k])
# This is the main part which pops up on the screen and asks the user to enter the number of elements to be sorted
# & the elements to be actually sorted.
# Example 1
# Enter the value of n to be sorted:
# 7
# Enter the 7 values to be sorted:
# 23
# 65
# 12
# 89
# 4
# 2
# 4
# The elements after the quick sort are:
# 2
# 4
# 4
# 12
# 23
# 65
# 89
# Example 2
# Enter the value of n to be sorted:
# 10
# Enter the 10 values to be sorted:
# 23
# 1
# 67
# 89
# 109
# 4
# 3
# 67
# 54
# 87
# The elements after the quick sort are:
# 1
# 3
# 4
# 23
# 54
# 67
# 67
# 87
# 89
# 109