Sorting Techniques: Bubble Sort (Slowest)
Sorting Techniques: Bubble Sort (Slowest)
Sorting Techniques
def bubbleSort(arr):
l = len(arr)
for i in range(l-1):
swapped = False
for j in range(l-i-1):
if arr[j]>arr[j+1]: #change the comparision operator to < for decending order
arr[j],arr[j+1]= arr[j+1],arr[j]
swapped = True
if not swapped:
return arr
return arr
print(bubbleSort([2,5,4,1,9,7]))
[1, 2, 4, 5, 7, 9]
Insertion Sort
def insertionSort(arr):
l = len(arr)
for i in range(1,l):
x = arr[i]
for j in range(i):
if x<arr[j]:
for k in range(i,j,-1):
arr[k]=arr[k-1]
arr[j] = x
1 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
break
return arr
print(insertionSort([-1,2,5,4,0,1,-1,9,7]))
[-1, -1, 0, 1, 2, 4, 5, 7, 9]
Selection Sort
def selectionSort(arr):
l = len(arr)
for i in range(l):
min = i
for j in range(i+1,l):
if arr[min]>arr[j]:
min = j
if i !=min:
arr[i],arr[min] = arr[min],arr[i]
return arr
print(selectionSort([-1,2,5,4,0,1,-1,9,7]))
[-1, -1, 0, 1, 2, 4, 5, 7, 9]
def mergeTwoSortedArrays(nums1,nums2):
i = 0
j = 0
right = nums1[i]
left = nums2[j]
x = []
while i<len(nums1) and j<len(nums2):
right = nums1[i]
left = nums2[j]
if right<left
2 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
if right<left:
x.append(right)
i+=1
else:
x.append(left)
j+=1
if j<len(nums2):
for i in range(j,len(nums2)):
x.append(nums2[i])
elif i<len(nums1):
for j in range(i,len(nums1)):
x.append(nums1[j])
return x
def mergeSort(arr):
mid = len(arr)//2
arr1 = arr[:mid]
arr2 = arr[mid:]
if len(arr1)>1:
arr1 = mergeSort(arr1)
if len(arr2)>1:
arr2 = mergeSort(arr2)
return mergeTwoSortedArrays(arr1,arr2)
print(mergeSort([-1,2,5,4,0,1,-1,9,7]))
[-1, -1, 0, 1, 2, 4, 5, 7, 9]
def quickSort(arr):
x = [arr[-1]]
left = [arr[i] for i in range(len(arr)-1) if arr[i]<=x[0]]
right = [arr[i] for i in range(len(arr)-1) if arr[i]>x[0]]
if len(left)>1:
3 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
if len(left)>1:
left = quickSort(left)
if len(right)>1:
right = quickSort(right)
return left+x+right
print(quickSort([-1,2,5,4,0,1,-1,9,7]))
[-1, -1, 0, 1, 2, 4, 5, 7, 9]
max_val = max(arr)
count = [0] * (max_val + 1)
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
print(countingSort([4, 2, 2, 8, 3, 3,1]))
[1, 2, 2, 3, 3, 4, 8]
4 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
import math,random
class linkedList:
def __init__(self):
self.head = None
def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = node(data)
def insert(self,pos,data):
if pos == 0:
temp = node(data)
temp.next = self.head
self.head = temp
else:
temp = self.head
for i in range(pos-1):
temp = temp.next
5 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
temp = temp.next
if not temp:
return None
newNode = node(data)
newNode.next = temp.next
temp.next = newNode
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
def length(self):
temp = self.head
i = 0
while temp:
i+=1
temp = temp.next
return i
def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data == data:
return i
temp = temp.next
i+=1
return None
def merge(self,list2,list1=None):
if not list1:
list1= self.head
if not list1:
return list2
if not list2:
list1
6 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
return list1
def sorted(self,x):
if not x or not x.next:
return x
left = x
slow = x
fast = x
pre = None
while fast and fast.next:
fast = fast.next.next
pre = slow
slow = slow.next
if pre:
pre.next = None
right = slow
if left:
left = self.sorted(left)
if right:
right = self.sorted(right)
7 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
return self.merge(left,right)
def sort(self):
self.head = self.sorted(self.head)
def remove(self,data):
if not self.head:
return None
if self.head.data == data:
self.head = self.head.next
else:
pre = self.head
temp = self.head.next
while temp:
if temp.data == data:
break
pre = temp
temp = temp.next
if temp:
pre.next = temp.next
def pop(self,index=None):
if not self.head:
return None
if index == 0:
self.head = self.head.next
elif index == None:
if not self.head.next:
self.head = None
return
pre = self.head
temp = self.head.next
while temp and temp.next:
pre = temp
temp = temp.next
pre.next = None
else:
8 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
pre = self.head
if not self.head.next:
return None
temp = self.head.next
for i in range(index - 1):
if not temp:
return None
pre = temp
temp = temp.next
if temp:
pre.next = temp.next
def concat(self,other):
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = other.head
def reverse(self):
if not self.head or not self.head.next:
return
temp = self.head
prev = None
while temp.next:
next = temp.next
temp.next = prev
prev = temp
temp = next
temp.next = prev
self.head = temp
ll1 = linkedList()
for val in [10, 20, 30, 40]: ll1.append(val)
ll1.display()
9 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
ll1.display()
ll1.remove(20) # Remove 20
ll1.pop() # Pop last
ll1.pop(2) # Pop at index 2
ll1.display()
ll1.reverse()
ll1.display()
ll2 = linkedList()
for i in range(10):
ll2.append(random.randint(0,10))
ll2.display()
ll2.sort()
ll2.display()
10 20 30 40
5 10 15 20 30 40 50
Search 15: 2
Search 99: None
10 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
class Stack:
def __init__(self):
self.head = None
def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = node(data)
def pop(self):
if not self.head:
return
if not self.head.next:
self.head = None
else:
temp = self.head
while temp and temp.next and temp.next.next:
temp = temp.next
temp.next = None
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
11 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
print()
s = Stack()
# Push elements
s.append(10)
s.append(20)
s.append(30)
s.append(40)
# Pop once
s.pop()
print("Stack after popping once (should remove 40):")
s.display()
# Pop again
s.pop()
print("Stack after popping again (should remove 30):")
s.display()
12 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
s.display()
class Queue:
def __init__(self):
self.head = None
def append(self,data):
if self.head == None:
self.head = node(data)
else:
temp = self.head
while temp and temp.next:
temp = temp.next
temp.next = node(data)
def pop(self):
if not self.head:
return
if not self.head.next:
self.head = None
else:
self.head = self.head.next
def display(self):
temp = self.head
13 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
14 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
class node:
def __init__(self,data=None):
self.data = data
self.next = None
self.prev = None
class doublyLinkedList:
def __init__(self):
self.head = None
def append(self,data):
15 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
if not self.head:
self.head = node(data)
else:
temp = self.head
while temp.next:
temp = temp.next
newNode = node(data)
newNode.prev = temp
temp.next = newNode
def display(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
def pop(self,index=None):
if index == 0 and self.head:
self.head=self.head.next
if self.head:
self.head.prev = None
16 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
for i in range(index-1):
if not temp:
break
pre = temp
temp = temp.next
if temp:
pre.next = temp.next
if temp.next:
temp.next.prev = pre
def insert(self,data,index=None):
newNode = node(data)
if index == 0:
if not self.head:
self.head = newNode
else:
newNode.next = self.head
self.head.prev = newNode
self.head = newNode
elif index is not None:
pre = None
temp = self.head
for i in range(index):
if not temp:
break
pre = temp
temp = temp.next
if pre:
newNode.next = pre.next
pre.next = newNode
newNode.prev = pre
if temp:
temp.prev = newNode
def length(self):
temp = self.head
i = 0
while temp:
17 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
temp
i+=1
temp = temp.next
return i
def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data == data:
return i
temp = temp.next
i+=1
return None
def remove(self,data):
temp = self.head
pre = None
while temp:
if temp.data == data:
break
pre = temp
temp = temp.next
if temp:
if pre:
pre.next = pre.next.next
if pre.next:
pre.next.prev = pre
else:
self.head = self.head.next
if self.head:
self.head.prev = None
dll = doublyLinkedList()
18 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
# Append elements
dll.append(10)
dll.append(20)
dll.append(30)
# Display list
print("List after appending 10, 20, 30:")
dll.display() # Expected: 10 20 30
19 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
# Check length
print("Length of list:", dll.length()) # Expected: 2
class node:
def __init__(self,data=None) -> None:
self.data = data
self.next = None
class circularLinkedList:
def __init__(self) -> None:
self.head = None
self.tail = None
def append(self,data):
newNode = node(data)
if not self.head:
self.head = newNode
self.tail = self.head
self.tail.next == self.head
else:
20 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
self.tail.next = newNode
newNode.next = self.head
self.tail = newNode
def display(self):
temp = self.head
while temp:
print(temp.data,end=" ")
if temp == self.tail:
break
temp = temp.next
print()
def insertAtBeginning(self,data):
newNode = node(data)
if not self.head:
self.head = newNode
self.tail = self.head
self.tail.next == self.head
else:
newNode.next = self.head
self.head = newNode
self.tail.next = self.head
def pop(self):
if not self.head:
return
if self.head == self.tail:
self.head = self.tail = None
else:
temp = self.head
while temp.next != self.tail:
temp = temp.next
temp.next = self.head
self.tail = temp
def deleteFromBeginning(self):
21 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
def deleteFromBeginning(self):
if not self.head:
return
if self.head == self.tail:
self.head = self.tail = None
else:
self.head = self.head.next
self.tail.next = self.head
def search(self,data):
temp = self.head
i = 0
while temp:
if temp.data==data:
print(i)
if temp == self.tail:
break
temp = temp.next
i+=1
cll = circularLinkedList()
for val in [10, 20, 30]: cll.append(val)
cll.search(20)
for val in [5, 2]: cll.insertAtBeginning(val)
cll.display()
for _ in range(2): cll.pop()
cll.display()
for _ in range(2): cll.deleteFromBeginning()
cll.display()
1
2 5 10 20 30
2 5 10
10
22 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
class node:
def __init__(self,data=None) -> None:
self.data = data
self.next = None
class number:
def __init__(self,data):
temp = None
for i in data:
newNode = node(int(i))
newNode.next = temp
temp = newNode
self.head = newNode
temp = self.head
while temp:
temp = temp.next
def add(self,other):
other = other.head #You're passing other as a number object, but treating it like a node.
sum = 0
carry = 0
temp = self.head
while temp or other:
a=b=0
if other:
a = other.data
other = other.next
if temp:
b = temp.data
temp = temp.next
s = a+b+carry
sum*=10
sum+=(s)%10
carry = s//10
if carry:
sum*=10
sum+=carry
23 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
print(sum)
num1 = "987654321987654321987654321987654321987654321987654321"
num2 = "123456789123456789123456789123456789123456789123456789"
n1 = number(num1)
n2 = number(num2)
n1.add(n2) #1111111111111111111111111111111111111111111111111111110
111111111111111111111111111111111111111111111111111111
Adding Polynomials
Given two polynomials represented as linked lists sorted by decreasing exponent order, add them and return the resulting
polynomial as a new linked list
class node:
def __init__(self,coef=1,exp=1) -> None:
self.coef = coef
self.exp = exp
self.next = None
class polynomial:
def __init__(self):
self.head = None
self.superscript_map = {
'0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹',
'+': '⁺', '-': '⁻', '=': '⁼', 'a': 'ᵃ', 'b': 'ᵇ', 'c': 'ᶜ', 'd': 'ᵈ', 'e': 'ᵉ', 'f': 'ᶠ', 'g': 'ᵍ',
'h': 'ʰ', 'i': 'ⁱ', 'j': 'ʲ', 'k': 'ᵏ', 'l': 'ˡ', 'm': 'ᵐ', 'n': 'ⁿ', 'o': 'ᵒ', 'p': 'ᵖ', 'q': 'ᵠ',
'r': 'ʳ', 's': 'ˢ', 't': 'ᵗ', 'u': 'ᵘ', 'v': 'ᵛ', 'w': 'ʷ', 'x': 'ˣ', 'y': 'ʸ', 'z': 'ᶻ'}
24 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
def append(self,coef=1,exp=1):
if coef == 0:
return
if not self.head:
self.head = node(coef,exp)
else:
temp = self.head
flag = True
if exp<self.head.exp:
newNode = node(coef,exp)
newNode.next = self.head
self.head = newNode
flag = False
while temp.next and flag:
if exp==temp.exp:
flag = False
temp.coef+=coef
break
if exp<temp.next.exp:
newNode = node(coef,exp)
newNode.next = temp.next
temp.next = newNode
flag = False
break
temp = temp.next
if exp==temp.exp and flag:
flag = False
temp.coef+=coef
if flag:
temp.next = node(coef,exp)
def add(self,other):
other = other.head
while other:
self.append(other.coef,other.exp) #EASY APPROACH
other = other.next
temp = self.head
25 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
node.next = temp
temp = node
if other:
temp.next = other
def display(self):
temp = self.head
output = ""
while temp:
26 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
temp
x = ""
if temp.coef == 0:
temp = temp.next
continue
if temp.coef>0:
if temp.next:
x+="+"
x+=str(temp.coef)
if temp.exp != 0:
if temp.exp>9:
k = str(temp.exp)
power = ""
for i in k:
power+=self.superscript_map[i]
x+="x"+power
else:
x+=f"x{self.superscript_map[str(temp.exp)]}"
x+=" "
output= x+output
temp = temp.next
print(output)
poly1 = polynomial()
poly1.append(2, 1)
poly2 = polynomial()
poly2.append(1, 0)
poly2.append(3, 2)
poly1.add(poly2)
poly1.display()
3x² +2x¹ +1
27 of 28 4/28/2025, 9:58 PM
DSA College - Colab https://colab.research.google.com/drive/1Q756FMJorK8j6KA6NLoRu08JfgD58Hzr#scrollTo...
28 of 28 4/28/2025, 9:58 PM