Python Program
Python Program
Linear search is a sequential searching algorithm where we start from one end and check every
element of the list until the desired element is found. It is the simplest searching algorithm.
1
2. If x == k, return the index. Element
found
3. Else, return not found.
# Linear Search in Python
array = [2, 4, 0, 1, 9]
x=1
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
Binary Search
Binary Search is a searching algorithm for finding an element's position in a sorted array.
In this approach, the element is always searched in the middle of a portion of an array.
Binary search can be implemented only on a sorted list of items. If the elements are not sorted
already, we need to sort them first.
2
The recursive method follows the divide and conquer approach.
The general steps for both methods are discussed below.
1. The array in which searching is to be performed is:
Initial array
Let x = 4 be the element to be searched.
2. Set two pointers low and high at the lowest and the highest positions respectively.
Setting pointers
3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.
Mid element
4. If x == mid, then return mid.Else, compare the element to be searched with m.
5. If x > mid, compare x with the middle element of the elements on the right side of mid. This
is done by setting low to low = mid + 1.
6. Else, compare x with the middle element of the elements on the left side of mid. This is
done by setting high to high = mid - 1.
3
7. Repeat steps 3 to 6 until low meets high. Mid element
8. x = 4 is found. Found
# Repeat until the pointers low and high meet each other
while low <= high:
if array[mid] == x:
return mid
else:
high = mid - 1
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x=4
4
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each
iteration.
Insertion sort works similarly as we sort cards in our hand in a card game.
We assume that the first card is already sorted then, we select an unsorted card. If the unsorted
card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same
way, other unsorted cards are taken and put in their right place.
Initial array
1. The first element in the array is assumed to be sorted. Take the second element and store it
separately in key.
Compare key with the first element. If the first element is greater than key, then key is
5
placed in front of the first element.
If the first element is greater than key, then key is placed in front of the first element.
2. Now, the first two elements are sorted.
Take the third element and compare it with the elements on the left of it. Placed it just
behind the element smaller than it. If there is no element smaller than it, then place it at the
6
3. Similarly, place every unsorted element at its correct position.
Place 4 behind 1
7
Place 3 behind 1 and the array is
sorted
def insertionSort(array):
# Compare key with each element on the left of it until an element smaller than it is found
# For descending order, change key<array[j] to key>array[j].
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j=j-1
8
data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
Bubble Sort
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until
they are in the intended order.
Just like the movement of air bubbles in the water that rise up to the surface, each element of the
array move to the end in each iteration. Therefore, it is called a bubble sort.
2. If the first element is greater than the second element, they are swapped.
3. Now, compare the second and the third elements. Swap them if they are not in order.
9
4. The above process goes on until the last element.
2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted elements is placed at the end.
10
Put the largest element at the end
In each iteration, the comparison takes place up to the last unsorted element.
11
The array is sorted if all elements are kept in the
right order
def bubbleSort(array):
bubbleSort(data)
In the above algorithm, all the comparisons are made even if the array is already sorted.
Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each
iteration and places that element at the beginning of the unsorted list.
Compare minimum with the third element. Again, if the third element is smaller, then
assign minimum to the third element otherwise do nothing. The process goes on until the
13
3. After each iteration, minimum is placed in the front of the unsorted list.
14
The second iteration
15
The third iteration
16
# put min at the correct position
(array[step], array[min_idx]) = (array[min_idx], array[step])
A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means
the last element inserted inside the stack is removed first.
You can think of the stack data structure as the pile of plates on top of another.
And, if you want the plate at the bottom, you must first remove all the plates on top. This is exactly
how the stack data structure works.
17
LIFO Principle of Stack
In programming terms, putting an item on top of the stack is called push and removing an item is
called pop.
Stack Push
and Pop Operations
In the above image, although item 3 was kept last, it was removed first. This is exactly how
the LIFO (Last In First Out) Principle works.
We can implement a stack in any programming language like C, C++, Java, Python or C#, but the
specification is pretty much the same.
# Creating a stack
def create_stack():
stack = []
return stack
return stack.pop()
stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
A queue is a useful data structure in programming. It is similar to the ticket queue outside a
cinema hall, where the first person entering the queue is the first person who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes
out first.
FIFO
Representation of Queue
In the above image, since 1 was kept in the queue before 2, it is the first to be removed from the
queue as well. It follows the FIFO rule.
In programming terms, putting items in the queue is called enqueue, and removing items from the
queue is called dequeue.
We can implement the queue in any programming language like C, C++, Java, Python or C#, but
the specification is pretty much the same.
19
# Queue implementation in Python
20
class Queue:
def __init__(self):
self.queue = []
# Add an element
def enqueue(self, item):
self.queue.append(item)
# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
21