-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_quick_sort.py
53 lines (48 loc) · 1.36 KB
/
merge_quick_sort.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
def merge_sort(my_array):
'''
sort in increasing order
'''
if len(my_array) == 1:
return my_array
else:
mid = len(my_array) / 2
left = merge_sort(my_array[:mid])
right = merge_sort(my_array[mid:])
together = []
left_ind = 0
right_ind = 0
while True:
if left[left_ind] < right[right_ind]:
together.append(left[left_ind])
left_ind += 1
if left_ind == len(left):
together.extend(right[right_ind:])
break
else:
together.append(right[right_ind])
right_ind += 1
if right_ind == len(right):
together.extend(left[left_ind:])
break
return together
def quick_sort(my_array):
'''
sort in increasing order
'''
pivot_index = len(my_array) - 1
next_cmp_index = 0
if my_array == []:
return []
if len(my_array) == 1:
return my_array
while pivot_index - next_cmp_index > 1:
if my_array[next_cmp_index] <= my_array[pivot_index]:
next_cmp_index += 1
else:
my_array[next_cmp_index],my_array[pivot_index-1],my_array[pivot_index] = my_array[pivot_index-1], my_array[pivot_index], my_array[next_cmp_index]
pivot_index -= 1
else:
if my_array[pivot_index] < my_array[next_cmp_index]:
my_array[pivot_index],my_array[next_cmp_index] = my_array[next_cmp_index],my_array[pivot_index]
pivot_index -= 1
return quick_sort(my_array[:pivot_index]) + [my_array[pivot_index]] + quick_sort(my_array[pivot_index+1:])