-
Notifications
You must be signed in to change notification settings - Fork 7
/
algorithms.py
129 lines (98 loc) · 3.47 KB
/
algorithms.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def bubble_sort(array):
n = len(array)
for i in range(n):
"""Check if its already sorted
already_sorted = True"""
for j in range(n - i - 1):
if array[j] > array[j + 1]:
"""If the item you're looking at is greater than its
adjacent value, then swap them"""
array[j], array[j + 1] = array[j + 1], array[j]
# Since you had to swap two elements
# set the `already_sorted` flag to `False` so the
# algorithm doesn't finish prematurely
"""already_sorted = False"""
# If there were no swaps during the last iteration,
# the array is already sorted, and you can terminate
"""if already_sorted:
break"""
return array
def insertion_sort(array):
# Loop from the second element of the array until
# the last element
for i in range(1, len(array)):
# This is the element we want to position in its
# correct place
key_item = array[i]
# Initialize the variable that will be used to
# find the correct position of the element referenced
# by `key_item`
j = i - 1
# Run through the list of items (the left
# portion of the array) and find the correct position
# of the element referenced by `key_item`. Do this only
# if `key_item` is smaller than its adjacent values.
while j >= 0 and array[j] > key_item:
# Shift the value one position to the left
# and reposition j to point to the next element
# (from right to left)
array[j + 1] = array[j]
j -= 1
# When you finish shifting the elements, you can position
# `key_item` in its correct location
array[j + 1] = key_item
return array
def merge_sort(A, start, end):
"""Merge sort."""
if end <= start:
return
mid = start + ((end - start + 1) // 2) - 1
merge_sort(A, start, mid)
merge_sort(A, mid + 1, end)
merge(A, start, mid, end)
return A
def merge(A, start, mid, end):
"""Helper function for merge sort."""
merged = []
left_index = start
right_index = mid + 1
while left_index <= mid and right_index <= end:
if A[left_index] < A[right_index]:
merged.append(A[left_index])
left_index += 1
else:
merged.append(A[right_index])
right_index += 1
while left_index <= mid:
merged.append(A[left_index])
left_index += 1
while right_index <= end:
merged.append(A[right_index])
right_index += 1
for i, sorted_val in enumerate(merged):
A[start + i] = sorted_val
def quick_sort(array, start, end):
"""In-place Quick Sort."""
if start >= end:
return
pivot = array[end]
pivot_index = start
for i in range(start, end):
if array[i] < pivot:
array[i], array[pivot_index] = array[pivot_index], array[i]
pivot_index += 1
array[end], array[pivot_index] = array[pivot_index], array[end]
quick_sort(array, start, pivot_index - 1)
quick_sort(array, pivot_index + 1, end)
return array
def count_sort(array):
"""Count Sort."""
count = [0] * (max(array) + 1)
for i in range(0, len(array)):
count[array[i]] += 1
j = 0
for i in range(0, len(count)):
for k in range(0, count[i]):
array[j] = i
j += 1
return array