-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigo.py
More file actions
104 lines (84 loc) · 3.18 KB
/
Copy pathbigo.py
File metadata and controls
104 lines (84 loc) · 3.18 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
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
class BigO(object):
"""
Big O Notation by example
Each method below illustrates what algorithms of increasing complexity look like.
"""
from copy import deepcopy
def __init__(self, data_collection):
"""
:params data_collection: represents the n variable of Big O
"""
self.data = data_collection
self._original = deepcopy(data_collection)
def _restore(self):
"""Reset BigO with the original data set, unsorted."""
self.data = self._original
def add_item_to_list(self, value):
"""Constant O(1)
The size of the array doesn't matter. It performs exactly the same.
"""
self.data.append(value)
def find_item(self, value):
"""Linear O(n)
Time complete grows in direct proportion of the amount of the data.
To find all items that match what we're searching for, we have to search each item of the array.
"""
matches = []
for item in self.data:
if item == value:
matches.append(item)
return matches
def bubble_sort(self):
"""O(n^2)
Time to complete will be proportional to the square of the amount of data.
Bubble sort gets slower as it processes. Poor performance.
"""
for n in range(self.data-1, 0, -1):
for i in range(n):
if self.data[i] > self.data[i+1]:
tmp = self.data[i]
self.data[i] = self.data[i+1]
self.data[i+1] = tmp
def binary_search(self, value):
"""O(logn)
Data used decreases each time it's run. Extremely efficient.
The inverse of exponential algorithms.
"""
low = 0
high = len(self.data) - 1
while low <= high:
middle = (high + low)/2
if self.data[middle] < value:
low = middle + 1
elif self.data[middle] > value:
high = middle + 1
else:
print("Match found.")
low = high + 1
def _partition(self, start, end, pivot):
"""For self.quick_sort."""
self.data[pivot], self.data[end] = self.data[end], self.data[pivot]
store_index = start
for i in xrange(start, end):
if self.data[i] < self.data[end]:
self.data[i], self.data[store_index] = self.data[store_index], self.data[i]
store_index += 1
self.data[store_index], self.data[end] = self.data[end], self.data[store_index]
return store_index
def _sort(self, start, end):
"""For self.quick_sort"""
if start >= end:
return self.data
pivot = randrange(start, end + 1)
new_pivot = self._partition(start, end, pivot)
self._sort(start, new_pivot - 1)
self._sort(new_pivot + 1, end)
def quick_sort(self):
"""O(nlogn)
Values are only compared once, rather than repeatedly, with increasing efficiency.
The amount of comparisons = log n + log(n-1) ...log(1) or n log n
"""
self._sort(0, len(self.data) - 1)
return self.data
def merge_sort(self):
return