-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
51 lines (34 loc) · 997 Bytes
/
quick_sort.py
File metadata and controls
51 lines (34 loc) · 997 Bytes
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
def load_names(filename):
names = []
with open(filename) as f:
for line in f:
names.append(line.rstrip())
return names
def load_numbers(filename):
numbers = []
with open(filename) as f:
for line in f:
numbers.append(int(line))
return numbers
def quick_sort(list):
if len(list) <= 1:
return list
less_than_pivot = []
greater_than_pivot = []
pivot = list[0]
for i in list[1:]:
if i >pivot:
greater_than_pivot.append(i)
else:
less_than_pivot.append(i)
return quick_sort(less_than_pivot) +[pivot]+ quick_sort(greater_than_pivot)
def is_sorted(list):
for i in range(len(list)-1):
if list[i] > list[i+1]:
return False
return True
unsorted_names = load_names("./names/unsorted.txt")
numbers = load_numbers('./numbers/1000000.txt')
sorted_names = quick_sort(unsorted_names)
for name in sorted_names:
print (name)