ADTs
ADTs
Binary Search
def binary_search(array, x):
min_value = 0
max_value = len(array) - 1
while min_value <= max_value:
mid = (min_value + max_value) // 2
if array[mid] == x:
print(x, "is present at position", str(mid))
return
elif array[mid] < x:
min_value = mid + 1
else:
max_value = mid - 1
print("Element is not found")
array = [1, 2, 3, 4, 5, 6, 7, 8]
print(array)
x = int(input("Enter the item to search from array = "))
binary_search(array, x)
array = [5, 2, 8, 1, 3]
sorted_array = bubble_sort_descending(array)
print(sorted_array)
Descending Order
def bubble_sort_descending(array):
n = len(array)
for i in range(n):
for j in range(0, n-i-1):
if array[j] < array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array
array = [5, 2, 8, 1, 3]
sorted_array = bubble_sort_descending(array)
print(sorted_array)
Pseudocode
DIM data() as INTEGER:{2,3,8,5,9,6,1,5,2}
For x = 0 to 8
For y= x+1 to 8
IF x < y THEN
End If
End For
End For
Bubble Sort taking inputs from use
Ascending Order
def bubble_sort(array):
n = len(array)
for i in range(n):
for j in range(0, n-i-1):
if array[j] > array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array
array = []
for i in range(3):
num = int(input("Enter integer= "))
array.append(num)
bubble_sort(array)
print("Sorted Array:",array)
Descending Order
def bubble_sort(array):
n = len(array)
for i in range(n):
for j in range(0, n-i-1):
if array[j] < array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
return array
array = []
for i in range(3):
num = int(input("Enter integer= "))
array.append(num)
bubble_sort(array)
print("Sorted Array:",array)
Insertion Sort
Ascending Order
def insertion_sort(array):
for i in range(1, len(array)):
key = array [i]
j = i - 1
while j >= 0 and array [j] > key:
array [j + 1] = array [j]
j -= 1
array [j + 1] = key
array = [5, 2, 8, 1, 3]
insertion_sort(array)
print(array)
Descending Order
def insertion_sort_descending(array):
for i in range(1, len(array)):
key = array[i]
j = i - 1
while j >= 0 and array[j] < key: # here modified condition
array[j + 1] = array[j]
j -= 1
array[j + 1] = key
array = [5, 2, 8, 1, 3]
insertion_sort_descending(array)
print(array)
Queue
Python has built-in support for queues in the queue module. The queue module provides several classes for
implementing different types of queues, including:
1. Regular Queue
2. Priority Queue
3. Circular Queue
4. Dequeue Queue
q = queue.LifoQueue()
q.put(10)
q.put(20)
q.put(30)
print(q.get()) # prints 30
print(q.get()) # prints 20
print(q.qsize()) # prints 1
q = queue.PriorityQueue()
print(q.queue)
print(q.qsize()) # prints 1
Example of Circular Queue
## 1. Defining Class
class CircularQueue:
def __init__(self, max_size):
self.max_size = max_size
self.queue = [None] * max_size
self.front = self.rear = -1
def is_empty(self):
return self.front == -1
def is_full(self):
return (self.rear + 1) % self.max_size == self.front
def dequeue(self):
if self.is_empty():
print("Queue is empty")
elif self.front == self.rear:
item = self.queue[self.front]
self.front = self.rear = -1
return item
else:
item = self.queue[self.front]
self.front = (self.front + 1) % self.max_size
return item
## 6. Defining Display Function
def display(self):
if self.is_empty():
print("Queue is empty")
elif self.rear >= self.front:
for i in range(self.front, self.rear + 1):
print(self.queue[i], end=" ")
print()
else:
for i in range(self.front, self.max_size):
print(self.queue[i], end=" ")
for i in range(0, self.rear + 1):
print(self.queue[i], end=" ")
print()
# Enqueue elements
cq.enqueue(1)
cq.enqueue(2)
cq.enqueue(3)
cq.enqueue(4)
cq.enqueue(5)
# Dequeue elements
print("Dequeue:", cq.dequeue()) # Output: Dequeue: 1
print("Dequeue:", cq.dequeue()) # Output: Dequeue: 2
Stack
In Python, a stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, which means
that the last element added to the stack will be the first one to be removed. The basic operations that can be
performed on a stack are push, pop, and peek. Here's an example implementation of a stack in Python:
In the above example above, we first create an empty stack using a list. We then add three elements ('apple',
'banana', 'orange') to the stack using the append() method, which adds the elements to the end of the list.
We can remove an element from the stack using the pop() method, which removes and returns the last
element from the list. In the example above, we remove the last element ('orange') from the stack using
pop(), and print the updated stack.
Note that we can also access the last element of the stack without removing it using the indexing notation
stack[-1].
_______________________________________________________________________________________
# Create an empty stack
stack = []
# Pop(Remove) the top item from the stack using the pop() method
top_item = stack.pop()
print(top_item) # Output: 3
In this implementation, we define a BinaryTree class that has a constructor that takes a value as input and
initializes the node with that value, as well as left_child and right_child attributes that are initially set to
None. The BinaryTree class also has two methods, insert_left and insert_right, that allow us to add
child nodes to the left or right of the current node.
To use this binary tree, we can create a root node and then add child nodes as desired using the insert_left
and insert_right methods. For example:
StartPointer = 0
EmptyList = 5