Skip to content

Commit 27d4ae9

Browse files
authored
Essential sort and search algorihms
0 parents  commit 27d4ae9

17 files changed

+680
-0
lines changed

ArrayStack.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
class ArrayStack:
3+
4+
def __init__(self):
5+
self.data=[]
6+
7+
def __len__(self):
8+
return len(self.data)
9+
10+
def is_empty(self):
11+
return len(self.data) == 0
12+
13+
def push(self,e):
14+
self.data.append(e)
15+
16+
def top(self):
17+
if self.is_empty():
18+
return None
19+
else:
20+
return self.data[-1]
21+
22+
def pop(self):
23+
if self.is_empty():
24+
return None
25+
else:
26+
return self.data.pop()
27+
28+
def reverse_file(filename):
29+
S=ArrayStack()
30+
original = open(filename)
31+
for line in original:
32+
S.push(line.rstrip("\n"))
33+
original.close()
34+
35+
output=open(filename,"w")
36+
while not S.is_empty():
37+
output.write(S.pop() + "\n")
38+
output.close()
39+
40+
41+
42+
43+
44+
45+

DoublyLinkedList.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
class Node:
3+
def __init__(self,data):
4+
self.data=data
5+
self.next=None
6+
self.prev=None
7+
8+
class DoublyLinkedList:
9+
def __init__(self):
10+
self.head=None
11+
12+
def push(self,new_data):
13+
new_node=Node(new_data)
14+
new_node.next=self.head
15+
if self.head is not None:
16+
self.head.prev=new_node
17+
self.head=new_node
18+
19+
def insert_after(self,prev_node,new_data):
20+
if prev_node is None:
21+
print("The given previous node cannot be NULL")
22+
return
23+
new_node=Node(new_data)
24+
new_node.next=prev_node.next
25+
prev_node.next=new_node
26+
new_node.prev=prev_node
27+
if new_node.next:
28+
new_node.next.prev=new_node
29+
30+
def append(self,new_data):
31+
new_node=Node(new_data)
32+
if self.head is None:
33+
self.head=new_node
34+
return
35+
else:
36+
temp=self.head
37+
while temp.next is not None:
38+
temp=temp.next
39+
temp.next=new_node
40+
41+
42+
def print_list(self,node):
43+
print("\n Traversal in forward direction.")
44+
while node:
45+
print(node.data,end=" ")
46+
last=node
47+
node=node.next
48+
print("\n Traversal in reverse direction.", end=" ")
49+
while last:
50+
print(last.data,end=" ")
51+
last=last.prev
52+
53+
#driver code
54+
55+
if __name__=="__main__":
56+
llist=DoublyLinkedList()
57+
# llist.append(6)
58+
llist.push(7)
59+
llist.push(1)
60+
# llist.append(4)
61+
llist.insert_after(llist.head.next,8)
62+
print(" Created DLL is : ",end=" ")
63+
llist.print_list(llist.head)
64+
65+
66+
67+

LinkedQueue.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
class Node:
3+
4+
def __init__(self,data):
5+
self.data=data
6+
self.next=None
7+
8+
9+
class LinkedQueue:
10+
11+
def __init__(self):
12+
self.head=None
13+
self.tail= None
14+
15+
def is_empty(self):
16+
return self.head == None
17+
18+
19+
def dequeue(self):
20+
if self.is_empty():
21+
return None
22+
else:
23+
answer=self.head.data
24+
self.head=self.head.next
25+
if self.is_empty():
26+
self.tail=None
27+
return answer
28+
29+
def enqueue(self,e):
30+
newest=Node(e)
31+
if self.is_empty():
32+
self.head=newest
33+
34+
else:
35+
self.tail.next= newest
36+
self.tail= newest
37+
38+
39+
if __name__=="__main__":
40+
q=LinkedQueue()
41+
q.enqueue(10)
42+
q.enqueue(20)
43+
q.dequeue()
44+
q.enqueue(30)
45+
q.enqueue(40)
46+
q.enqueue(50)
47+
48+
q.dequeue()
49+
50+
print("\n Queue head: " + str(q.head.data if q.head != None else -1))
51+
print("\n Queue tail: " + str(q.tail.data if q.tail != None else -1))
52+
53+
54+
55+
56+
57+
58+

LinkedStack.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
class Node:
3+
4+
def __init__(self,data):
5+
self.data=data
6+
self.next=None
7+
8+
class Stack:
9+
10+
def __init__(self):
11+
self.head=None
12+
13+
def is_empty(self):
14+
if self.head == None:
15+
return True
16+
else:
17+
return False
18+
19+
def push(self,data):
20+
if self.head == None:
21+
self.head = Node(data)
22+
else:
23+
new_node=Node(data)
24+
new_node.next = self.head
25+
self.head=new_node
26+
27+
def pop(self):
28+
if self.is_empty():
29+
return None
30+
else:
31+
popped_node=self.head
32+
self.head=self.head.next
33+
popped_node.next=None
34+
return popped_node.data
35+
36+
def peek(self):
37+
if self.is_empty():
38+
return None
39+
else:
40+
return self.head.data
41+
42+
43+
def display(self):
44+
iternode=self.head
45+
if self.is_empty():
46+
print("Stack Undeflow")
47+
else:
48+
while iternode != None:
49+
print(iternode.data,end=" ")
50+
iternode=iternode.next
51+
if iternode != None:
52+
print("->",end=" ")
53+
return
54+
55+
if __name__=="__main__":
56+
MyStack=Stack()
57+
MyStack.push(11)
58+
MyStack.push(22)
59+
MyStack.push(33)
60+
MyStack.push(44)
61+
62+
MyStack.display()
63+
64+
print("\n Top element is ",MyStack.peek())
65+
66+
MyStack.pop()
67+
MyStack.pop()
68+
MyStack.display()
69+
70+
print("\n Top element is ",MyStack.peek())
71+
72+
73+
74+
75+
76+
77+

beadsort.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
di
2+
3+
def beadsort(input_list):
4+
return_list = []
5+
transposed_list= [0] * max(input_list)
6+
for num in input_list:
7+
transposed_list[:num] = [n + 1 for n in transposed_list[:num]]
8+
9+
for i in range(len(input_list)):
10+
return_list.append(sum(n > i for n in transposed_list))
11+
12+
return return_list
13+
14+
input_list=[1,45,77,23,2,78,5,67,4,89]
15+
print(beadsort(input_list))
16+
17+

bin_search.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env Python3
2+
3+
def bin_search(array,low,high,x):
4+
5+
if high >= low:
6+
7+
mid=((low+high) // 2)
8+
9+
if array[mid] == x:
10+
return mid
11+
12+
elif array[mid] > x:
13+
return bin_search(array,low,mid-1,x)
14+
15+
else:
16+
return bin_search(array,mid+1,high,x)
17+
18+
else:
19+
return -1
20+
21+
22+
array=[2,56,78,34,12,8,4,63,122,66,1,3,77]
23+
x=66
24+
25+
26+
result=bin_search(array,0,len(array)-1,x)
27+
28+
if result != -1:
29+
print("Element is present at index:", str(result))
30+
else:
31+
print("Element is not present")
32+
33+
34+
35+

bin_search_iterative.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
def bin_search_iterative(data,target):
4+
5+
low=0
6+
high=len(data) -1
7+
while low <= high:
8+
mid=(low + high) // 2
9+
if target == data[mid]:
10+
return True
11+
elif target < data[mid]:
12+
high=mid - 1
13+
else:
14+
low=mid + 1
15+
return False
16+
17+
18+
19+
20+

bubble_sort.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env Python3
2+
3+
from random import randint
4+
5+
6+
def bubble_sort(array):
7+
8+
swapped=True
9+
while swapped:
10+
swapped=False
11+
for i in range(len(array)-1):
12+
if array[i] > array[i+1]:
13+
array[i],array[i+1] = array[i+1],array[i]
14+
swapped=True
15+
16+
17+
ARRAY_LENGTH=1000
18+
19+
array=[randint(0,1000) for i in range(ARRAY_LENGTH)]
20+
21+
bubble_sort(array)
22+
23+
print(array)
24+

0 commit comments

Comments
 (0)