-
Notifications
You must be signed in to change notification settings - Fork 232
/
algorithms.py
98 lines (83 loc) · 2.77 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
#=================================================================================================
# SEARCHING ALGORITHMS
#=================================================================================================
def binary_search(items, target):
mid = len(items) // 2
if len(items) == 1:
return mid if items[mid] == target else False
elif items[mid] == target:
return mid
else:
if items[mid] < target:
callback_response = binary_search(items[mid:], target)
return mid + callback_response if callback_response is not False else False
else:
return binary_search(items[:mid], target)
def linear_search(items,target):
for i in range(len(items)):
if items[i] == target:
return i
return None
#=================================================================================================
# SORTING ALGORITHMS
#=================================================================================================
def bubble_sort(items):
for i in range(len(items)):
for j in range(len(items)-1-i):
if items[j] > items[j+1]:
items[j], items[j+1] = items[j+1], items[j]
return items
def insertion_sort(lst):
i = 1
while i < len(lst):
x=lst[i]
j=i-1
while j>=0 and lst[j]>x:
lst[j+1]=lst[j]
j=j-1
lst[j+1]=x
i=i+1
return lst
def merge(A, B):
""" The merge function used in merge sort """
new_list = []
while len(A) > 0 and len(B) > 0:
if A[0] < B[0]:
new_list.append(A[0])
A.pop(0)
else:
new_list.append(B[0])
B.pop(0)
if len(A) == 0:
new_list = new_list + B
if len(B) == 0:
new_list = new_list + A
return new_list
def merge_sort(items):
""" Implementation of merge sort """
len_i = len(items)
if len_i == 1:
return items
mid_point = int(len_i / 2)
i1 = merge_sort(items[:mid_point])
i2 = merge_sort(items[mid_point:])
return merge(i1, i2)
def quick_sort(items, index=-1):
""" Implementation of quick sort """
len_i = len(items)
if len_i <= 1:
return items
pivot = items[index]
small = []
large = []
dup = []
for i in items:
if i < pivot:
small.append(i)
elif i > pivot:
large.append(i)
elif i == pivot:
dup.append(i)
small = quick_sort(small)
large = quick_sort(large)
return small + dup + large