-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path2_data_structures
243 lines (170 loc) · 4.51 KB
/
2_data_structures
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
For the basic section, we covered- Linear Arrays:
Linear Search
Binary Search
Bubble Sort
Insertion Sort
Advanced- Linked Lists:
Traverse
Search
Insert
Delete
Mindbreaker- Hash Tables:
Associative arrays
Hash functions
Keys-value pairs
Collision, and
Chaining
_____________________________________________________________EXAMPLES____________________________________________________
BASIC - SEARCH & SORT:
LINEAR SEARCH
def search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
arr = [2, 5, 8, 10, 16, 22, 25]
target = 16
result = search(arr, target)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
ITERATIVE BINARY SEARCH
def binary_itr(arr, start, end, target):
while start <= end:
mid = (start + end) // 2
if arr[mid] < target:
start = mid + 1
elif arr[mid] > target:
end = mid - 1
else:
return mid
return start
#return -1
arr = [2, 5, 8, 10, 16, 22, 25]
target = 25
result = binary_itr(arr, 0, len(arr) - 1, target)
#print(result)
if result != -1:
print("Element is present at index %d" % result)
else:
print("Element is not present in array")
RECURSIVE BINARY SEARCH
def binary_recur(arr, start, end, target):
if end >= start:
mid = start + end - 1 // 2
if arr[mid] < target:
binary_recur(arr, mid + 1, end, target)
elif arr[mid] > target:
return binary_recur(arr, start, mid - 1, target)
else:
return mid
else:
return -1
arr = [2, 5, 8, 10, 16, 22, 25]
target = 10
result = binary_recur(arr, 0, len(arr) - 1, target)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
BUBBLE SORT - OPTIMIZED
A = [9, 8, 7, 6, 5, 4, 3, 2, 1]
def bubble_optimized(A):
iterations = 0
for i in range(len(A)):
for j in range(len(A)-i-1):
iterations += 1
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
return A, iterations
print(bubble_optimized(A))
BUBBLE SORT - UNOPTIMIZED
def swap(A, i, j):
temp = A[i]
A[i] = A[j]
A[j] = temp
def bubble_sort_un_op(A):
iterations = 0
for i in A:
for j in range(len(A)-1):
iterations += 1
if A[j] > A[j+1]:
swap(A, j, j + 1)
return A, iterations
print(bubble_sort_un_op(A))
INSERTION SORT - SHIFTING ELEMENTS
def insert_sort(A):
for j in range(1, len(A)):
key = A[j]
i = j - 1
while i >= 0 and A[i] > key:
A[i + 1] = A[i]
i -= 1
A[i + 1] = key
return A
A = [5, 2, 4, 6, 1, 3]
print(insert_sort(A))
INSERTION SORT - SWAPPING ELEMENTS
def swap(A):
for i in range(1, len(A)):
for j in range(i-1, -1, -1):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
else:
break
return A
print(swap(A))
__________________________________________________________
ADVANCED- LINKED LISTS:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def traversal(self):
first = self.head
while first:
print(first.data)
first = first.next
def insert_new_header(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def search(self, x):
temp = self.head
while temp is not None:
if temp.data == x:
return True
temp = temp.next
else:
return False
def delete_node(self, data):
temp = self.head
while temp is not None:
if temp.data == data:
break
prev = temp
temp = temp.next
prev.next = temp.next
def delete_tail(self):
temp = self.head
while temp.next.next is not None:
temp = temp.next
temp.next = None
family = LinkedList()
family.head = Node("Bob")
wife = Node("Amy")
first_kid = Node("Max")
second_kid = Node("Jenny")
family.head.next = wife
wife.next = first_kid
first_kid.next = second_kid
family.insert_new_header("Dave")
#family.delete_tail()
#print(family.search("Bob"))
family.delete_node("Amy")
family.traversal()
_________________________________________________________
MINDBREAKER- HASH TABLES
NO CODE WAS DISCUSSED FOR HASH TABLES