Skip to content

Commit 0c25c35

Browse files
committed
quick sort algorithm
1 parent 745480b commit 0c25c35

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Wed Aug 29 12:55:58 2018
5+
6+
@author: avinash
7+
"""
8+
9+
10+
def partition(lst, low, high):
11+
i = low - 1
12+
pivot = lst[high]
13+
for j in range(low, high):
14+
if lst[j] <= pivot:
15+
i += 1
16+
lst[i], lst[j] = lst[j], lst[i]
17+
lst[i + 1], lst[high] = lst[high], lst[i + 1]
18+
return i + 1
19+
20+
21+
def quick_sort(lst, low, high):
22+
if low < high:
23+
pi = partition(lst, low, high)
24+
quick_sort(lst, low, pi - 1)
25+
quick_sort(lst, pi + 1, high)
26+
27+
28+
lst = []
29+
size = int(input("Enter size of the list: \t"))
30+
for i in range(size):
31+
element = int(input("Enter an element: \t"))
32+
lst.append(element)
33+
34+
low = 0
35+
high = len(lst) - 1
36+
quick_sort(lst, low, high)
37+
print("The sorted list: \t", lst)

0 commit comments

Comments
 (0)