Skip to content

Commit c95c466

Browse files
committed
added different implementations of data structures and algorithms
1 parent ae9cb44 commit c95c466

16 files changed

+647
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Data Structures and Algorithm Examples and Samples in Python
2+
3+
## This repository contains different data structure and algorithm types in python. Some of the examples below are:
4+
5+
- Binary Search (for numbers)
6+
7+
- Binary Search (for strings)
8+
9+
- Bogo Sort
10+
11+
- Linear search (for numbers)
12+
13+
- Linear search (for strings)
14+
15+
- Linked List
16+
17+
- Linked List (Merge Sort)
18+
19+
- Merge Sort
20+
21+
- Quick Sort (for numbers)
22+
23+
- Quick Sort (for strings)
24+
25+
- Recursion
26+
27+
- Recursive Binary Search
28+
29+
- Selection Sort
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# iterative implementation of binary search
2+
3+
# python language prefers this method alot compared to the recursive method
4+
def binary_search(list, target):
5+
first = 0
6+
last = len(list) - 1
7+
8+
while first <= last:
9+
midpoint = (first + last) // 2
10+
11+
if list[midpoint] == target:
12+
return midpoint
13+
elif list[midpoint] < target:
14+
first = midpoint + 1
15+
else:
16+
last = midpoint - 1
17+
return None
18+
19+
20+
def verify(index):
21+
if index is not None:
22+
print("Target found at index: ", index)
23+
else:
24+
print("Target not found in the list")
25+
26+
27+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
28+
29+
result = binary_search(numbers, 12)
30+
verify(result)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
# from load import load_strings
3+
4+
# names = load_strings(sys.argv[1])
5+
6+
names = ["mata", "adam", "nasir", "bata", "habib", "amina", "maemuna"]
7+
search_names = ["mata", "adam", "nasir"]
8+
9+
10+
def binary_search(collection, target):
11+
first = 0
12+
last = len(collection) - 1
13+
14+
while first <= last:
15+
midpoint = (first + last) // 2
16+
if collection[midpoint] == target:
17+
return midpoint
18+
elif collection[midpoint] < target:
19+
first = midpoint + 1
20+
else:
21+
last = midpoint - 1
22+
return None
23+
24+
25+
for n in search_names:
26+
index = binary_search(names, n)
27+
print(index)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
import sys
3+
# from load import load_numbers
4+
5+
# number = load_numbers(sys.argv[1])
6+
# print(number)
7+
8+
9+
def is_sorted(values):
10+
for index in range(len(values) - 1):
11+
if values[index] > values[index + 1]:
12+
return False
13+
return True
14+
15+
16+
def bogo_sort(values):
17+
attempts = 0
18+
19+
while not is_sorted(values):
20+
print(attempts)
21+
random.shuffle(values)
22+
attempts += 1
23+
return values
24+
25+
26+
print(bogo_sort([5, 8, 1, 4, 7, 9, 6, 3, 2]))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def linear_search(list, target):
2+
"""
3+
returns the index position of the target element if found, otherwise returns none
4+
"""
5+
for i in range(0, len(list)):
6+
if list[i] == target:
7+
return i
8+
return None
9+
10+
11+
def verify(index):
12+
if index is not None:
13+
print("Target found at index: ", index)
14+
else:
15+
print("Target not found in the list")
16+
17+
18+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
19+
20+
result = linear_search(numbers, 9)
21+
verify(result)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
# from load import load_strings
3+
4+
# names = load_string[sys.argv[1]]
5+
6+
search_names = ["habib", "adam", "nasir", "jelilat"]
7+
8+
9+
def index_of_item(collection, target):
10+
for i in range(0, len(collection)):
11+
if target == collection[i]:
12+
return i
13+
return None
14+
15+
16+
for n in search_names:
17+
index = index_of_item(search_names, n)
18+
print(index)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
class Node:
2+
"""
3+
An object for storing a single node of a linked list
4+
model two attributes - data and the link to the next node in the list
5+
"""
6+
7+
data = None
8+
next_node = None
9+
10+
def __init__(self, data):
11+
self.data = data
12+
13+
def __repr__(self):
14+
return "<Node data: %s>" % self.data
15+
16+
17+
class LinkedList:
18+
"""
19+
singly linked list
20+
"""
21+
22+
def __init__(self):
23+
self.head = None
24+
25+
def is_empty(self):
26+
return self.head == None
27+
28+
def size(self):
29+
"""
30+
returns the number of nodes in the list
31+
takes 0(n) time
32+
"""
33+
current = self.head
34+
count = 0
35+
36+
while current: # or while current!= None:
37+
count += 1
38+
current = current.next_node
39+
return count
40+
41+
def add(self, data):
42+
"""
43+
adds new node containing data at head of the list
44+
takes 0(1) time
45+
"""
46+
new_node = Node(data)
47+
new_node.next_node = self.head
48+
self.head = new_node
49+
50+
def search(self, key):
51+
"""
52+
search for the first node containing data that matches the key
53+
returns the node or "None" if not found
54+
takes 0(n) time
55+
"""
56+
current = self.head
57+
while current:
58+
if current.data == key:
59+
return current
60+
else:
61+
current = current.next_node
62+
return None
63+
64+
def insert(self, data, index):
65+
"""
66+
insert a new node containing data at index position
67+
insertion takes 0(1) time but finding the node at the
68+
insertion point takes 0(n) time
69+
takes overall 0(n) time
70+
"""
71+
if index == 0:
72+
self.add(data)
73+
if index > 0:
74+
new = Node(data)
75+
76+
position = index
77+
current = self.head
78+
79+
while position > 1:
80+
current = node.next_node
81+
position -= 1
82+
83+
prev_node = current
84+
next_node = current.next_node
85+
86+
prev_node.next_node = new
87+
new.next_node = next_node
88+
89+
def remove(self, key):
90+
"""
91+
removes nodes containing data that matches the key
92+
returns the node or "none" if the key doesn't exist
93+
takes 0(n) time
94+
"""
95+
current = self.head
96+
previous = None
97+
found = False
98+
99+
while current and not found:
100+
if current.data == key and current == self.head: # or current is self.head
101+
found = True
102+
self.head = current.next_node
103+
elif current.data == key:
104+
found = True
105+
previous.next_node = current.next_node
106+
else:
107+
previous = current
108+
current = current.next_node
109+
110+
return current
111+
112+
def node_at_index(self, index):
113+
if index == 0:
114+
return self.head
115+
else:
116+
current = self.head
117+
position = 0
118+
119+
while position < index:
120+
current = current.next_node
121+
position += 1
122+
return current
123+
124+
def __repr__(self):
125+
"""
126+
returns a string representation of the list
127+
takes 0(n) time
128+
"""
129+
nodes = []
130+
current = self.head
131+
132+
while current:
133+
if current is self.head:
134+
nodes.append("[head: %s]" % current.data)
135+
elif current.next_node is None:
136+
nodes.append("[Tail: %s]" % current.data)
137+
else:
138+
nodes.append("[%s]" % current.data)
139+
140+
current = current.next_node
141+
return "->".join(nodes)

0 commit comments

Comments
 (0)