-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.py
More file actions
72 lines (61 loc) · 1.74 KB
/
Copy pathmethods.py
File metadata and controls
72 lines (61 loc) · 1.74 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
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
for i in arr:
if i < pivot:
less.append(i)
elif i > pivot:
more.append(i)
else:
pivotList.append(i)
less = quickSort(less)
more = quickSort(more)
return less + pivotList + more
def bubbleSort(array):
index = len(array) - 1
while index >= 0:
for j in range(index):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
index -= 1
return array
def merge(left, right):
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
# change the direction of this comparison to change
# the direction of the sort
if left[left_idx] <= right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1
if left:
result.extend(left[left_idx:])
if right:
result.extend(right[right_idx:])
return result
def mergeSort(m):
if len(m) <= 1:
return m
middle = len(m) // 2
left = m[:middle]
right = m[middle:]
left = mergeSort(left)
right = mergeSort(right)
return list(merge(left, right))
def insertionSort(array):
for slot in range(1, len(array)):
value = array[slot]
test_slot = slot - 1
while test_slot > -1 and array[test_slot] > value:
array[test_slot + 1] = array[test_slot]
test_slot = test_slot - 1
array[test_slot + 1] = value
return array