Skip to content

Commit 5214b7c

Browse files
authored
Sync Fork
Sync Fork
2 parents 02b92cf + 056d7f9 commit 5214b7c

File tree

16 files changed

+732
-1
lines changed

16 files changed

+732
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Bucket sort, or bin sort, is a sorting algorithm that works by distributing
3+
the elements of an array into a number of buckets. Each bucket is then sorted
4+
individually, either using a different sorting algorithm, or by recursively
5+
applying the bucket sorting algorithm.
6+
7+
https://en.wikipedia.org/wiki/Bucket_sort
8+
9+
"""
10+
11+
from random import randint
12+
13+
def Bucket_Sort(A, Digit):
14+
B = [[] for x in range(10)] #Creating 10 Buckets [10 different lists at every position]
15+
16+
#Storing elements in Bucket according to their first digit
17+
for each in A:
18+
B[int(str(each).rjust(Digit, '0')[0])].append(each)
19+
20+
#Sorting Buckets
21+
for each in range(10):B[each].sort()
22+
23+
A=[]
24+
25+
#Assembling elements from all the buckets
26+
for bucket in B:
27+
for each in bucket:
28+
A.append(each)
29+
30+
return A
31+
32+
A = [randint(0,99) for x in range(15)] #Generate a random list
33+
34+
print("Orignal Array = ", A)
35+
36+
A = Bucket_Sort(A, 2)
37+
38+
print(" Sorted Array = ", A)

Arrays-searching/src/binary_search/binary_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def binary_search(sorted_array, search_key):
1919
last_index = len(sorted_array) - 1
2020
found = False
2121
while first_index <= last_index and not found:
22-
mid = (first_index + last_index) / 2
22+
mid = (first_index + last_index) // 2
2323
if sorted_array[mid] == search_key:
2424
found = True
2525
break

Data_Structure/src/Queue.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Queue:
2+
3+
def __init__(self, Size = 10):
4+
self.__size = Size #Store the size of the queue
5+
self.__queue = [None]*Size #generate a List of length "Size" containing None (initial value)
6+
self.__head = self.__tail = 0 #Initial value of head & tail = 0
7+
self.__i = -1 # i is used as index in __next__ method to iterate through the Queue object
8+
9+
def isEmpty(self):
10+
return self.__head == self.__tail #If head == tail, Queue is empty
11+
12+
def isFull(self):
13+
return self.__tail == self.__size #if tail == size of queue, queue is full
14+
15+
def EnQueue(self, value):
16+
if(self.isFull()): raise IndexError("Overflow Error - Queue is Full")
17+
self.__queue[self.__tail] = value #insert "value" at tail
18+
self.__tail += 1 #increment tail
19+
20+
def DeQueue(self):
21+
if(self.isEmpty()): raise IndexError("Unerflow Error - Queue is Empty")
22+
self.__head += 1 #Increment head
23+
return self.__queue[self.__head-1] #Return value at head-1 (previous head = value to be deleted)
24+
25+
def __len__(self):
26+
return self.__tail - self.__head #return number of elements in the queue
27+
28+
def __getitem__(self, index): # __getitem__ method is used to get element stored at a particular index
29+
if(self.__head <= (index + self.__head) < self.__tail): #if the index is valid (between head & tail)
30+
return self.__queue[index + self.__head] #return the value
31+
else: raise IndexError("Index out of range") #else raise error
32+
33+
# __next__ and __iter__ methods are used to iterate through the object (used internally at Line 58 in for loop to print all element)
34+
35+
# __next__ is used to get the next element in the iterable object
36+
def __next__(self):
37+
self.__i += 1 # i is the index of the element to return
38+
if(self.__i < len(self)): return self.__getitem__(self.__i) #use __getitem__ to get the value at index i
39+
else: raise StopIteration() # if i is larger than len, all the elements have been traversed, raise StopIteration to stop iteration
40+
41+
# __iter__ returns an iterable object (self) & sets i = -1, to iterate through 0 to end
42+
def __iter__(self):
43+
self.__i = -1
44+
return self
45+
46+
47+
if __name__ == '__main__':
48+
49+
print("\nCreated Queue of size 5")
50+
Q1 = Queue(5)
51+
52+
print("\nEnQueue (Insert) 5")
53+
Q1.EnQueue(5)
54+
print("EnQueue (Insert) 15")
55+
Q1.EnQueue(15)
56+
57+
print("\nPrint Queue")
58+
for i in Q1:
59+
print(i, end=", ")
60+
61+
print("\n\nDeQueue (Delete), Deleted element = ", Q1.DeQueue())
62+
print("DeQueue (Delete), Deleted element = ", Q1.DeQueue())
63+
64+
print("\nQueue empty\n")
65+
66+
print("\n\nDeQueue / Delete, Deleted element = ", Q1.DeQueue()) #This line raises error as the Queue is empty & cannot delete any element

Data_Structure/src/Queue_Circular.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class Queue_Circular:
2+
3+
def __init__(self, Size=10):
4+
self.__size = Size+1 #Store Size+1 in size
5+
self.__queue = [None]*(Size+1) #generate a List of length "size" containing None (initial value)
6+
self.__head = self.__tail = 0 #Initial value of head & tail = 0
7+
self.__i = -1 # i is used as index in __next__ method to iterate through the Queue object
8+
9+
def isEmpty(self):
10+
return self.__head == self.__tail #If head == tail, Queue is empty
11+
def isFull(self):
12+
return (self.__tail+1)%self.__size == self.__head #if next tail (circular increment) == head, queue is full
13+
14+
def EnQueue(self, value):
15+
if(self.isFull()): raise IndexError("Overflow Error - Queue is Full")
16+
self.__queue[self.__tail] = value #store "value" at tail
17+
self.__tail = (self.__tail+1)%self.__size #increment tail by 1 & mod size (circular increment)
18+
19+
def DeQueue(self):
20+
if(self.isEmpty()): raise IndexError("Unerflow Error - Queue is Empty")
21+
self.__head = (self.__head+1)%self.__size # increment head (circular increment)
22+
return self.__queue[(self.__head-1)%self.__size] # Return value at head-1 (circular decrement) (previous head = value to be deleted)
23+
24+
#if tail > head, return tail - head
25+
#else, return size - head + tail
26+
def __len__(self):
27+
return self.__tail-self.__head if(self.__tail>=self.__head) else self.__size-self.__head+self.__tail
28+
29+
# __getitem__ is used to get value at a particular index
30+
def __getitem__(self, index):
31+
if(0 <= index < len(self)): #if index is between 0 & len of queue
32+
return self.__queue[(index+self.__head)%self.__size] #return the value at index
33+
else: raise IndexError("Index out of range") #else, raise exception
34+
35+
# __next__ and __iter__ methods are used to iterate through the object (used internally at Line 56 & 69 in for loop to print all element)
36+
37+
# __next__ is used to get the next element in the iterable object
38+
def __next__(self):
39+
self.__i += 1 # i is the index of the element to return
40+
if(self.__i < len(self)): return self.__getitem__(self.__i) #use __getitem__ to get the value at index i
41+
else: raise StopIteration() # if i is larger than len, all the elements have been traversed, raise StopIteration to stop iteration
42+
43+
# __iter__ returns an iterable object (self) & sets i = -1, to iterate through 0 to end
44+
def __iter__(self):
45+
self.__i = -1
46+
return self
47+
48+
if __name__ == '__main__':
49+
print("\nCreate Circular Queue of size 5\n")
50+
Q1 = Queue_Circular(5)
51+
52+
#insert 5, 10, 15, 20, 25 in Queue Q1
53+
for i in range(5, 30, 5):
54+
print("EnQueue (Insert) ", i)
55+
Q1.EnQueue(i)
56+
57+
print("\nPrint Queue")
58+
for i in Q1:
59+
print(i, end=", ")
60+
61+
print("\n\nEnQueue (Insert) 30")
62+
try: Q1.EnQueue(30) #This raises error because the Queue is full
63+
except IndexError: print("Queue is Full")
64+
65+
print("\nDeQueue (Delete), Deleted value = ", Q1.DeQueue())
66+
67+
print("\nEnQueue (Insert) 30")
68+
Q1.EnQueue(30)
69+
70+
print("\nPrint Queue")
71+
for i in Q1:
72+
print(i, end=", ")

Data_Structure/src/Stack.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Stack:
2+
3+
def __init__(self, Size=10):
4+
self.__size = Size
5+
self.__stack=[]
6+
self.__i=-1
7+
8+
def isEmpty(self):
9+
return len(self.__stack) == 0
10+
def isFull(self):
11+
return len(self.__stack) == self.__size
12+
13+
def push(self, value):
14+
if(self.isFull()): raise IndexError("Overflow Error - Stack is Full")
15+
else: self.__stack.append(value)
16+
17+
def pop(self):
18+
if(self.isEmpty()): raise IndexError("Unerflow Error - Stack is Empty")
19+
return self.__stack.pop()
20+
21+
def __len__(self):
22+
return len(self.__stack)
23+
24+
def __next__(self):
25+
self.__i+=1
26+
if(self.__i < len(self)): return self.__stack[self.__i]
27+
else: raise StopIteration()
28+
29+
def __iter__(self):
30+
self.__i=-1
31+
return self
32+
33+
if __name__ == '__main__':
34+
print("Create stack of size 10")
35+
S1 = Stack(10)
36+
37+
print("\nPush 10 to stack")
38+
S1.push(10)
39+
print("Push 100 to stack")
40+
S1.push(100)
41+
42+
print("\nPrint stack")
43+
for i in S1:
44+
print(i, end=", ")
45+
46+
print("\n\nPop Stack, Popped value = ", S1.pop())
47+
print("Pop Stack, Popped value = ", S1.pop())
48+
49+
print("\nStack is empty\n")
50+
51+
print("\nPop Stack, Popped value = ", S1.pop())
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import defaultdict
2+
3+
# This class represents a directed graph
4+
# using adjacency list representation
5+
class Graph:
6+
7+
# Constructor
8+
def __init__(self):
9+
10+
# default dictionary to store graph
11+
self.graph = defaultdict(list)
12+
13+
# function to add an edge to graph
14+
def addEdge(self,u,v):
15+
self.graph[u].append(v)
16+
17+
# Function to print a BFS of graph
18+
def BFS(self, s):
19+
20+
# Mark all the vertices as not visited
21+
visited = [False] * (len(self.graph))
22+
23+
# Create a queue for BFS
24+
queue = []
25+
26+
# Mark the source node as
27+
# visited and enqueue it
28+
queue.append(s)
29+
visited[s] = True
30+
31+
while queue:
32+
33+
# Dequeue a vertex from
34+
# queue and print it
35+
s = queue.pop(0)
36+
print (s, end = " ")
37+
38+
# Get all adjacent vertices of the
39+
# dequeued vertex s. If a adjacent
40+
# has not been visited, then mark it
41+
# visited and enqueue it
42+
for i in self.graph[s]:
43+
if visited[i] == False:
44+
queue.append(i)
45+
visited[i] = True
46+
47+
# Driver code
48+
49+
# Create a graph given in
50+
# the above diagram
51+
g = Graph()
52+
g.addEdge(0, 1)
53+
g.addEdge(0, 2)
54+
g.addEdge(1, 2)
55+
g.addEdge(2, 0)
56+
g.addEdge(2, 3)
57+
g.addEdge(3, 3)
58+
59+
print ("Following is Breadth First Traversal"
60+
" (starting from vertex 2)")
61+
g.BFS(2)

0 commit comments

Comments
 (0)