Module 5 DSA 24
Module 5 DSA 24
Module 5 DSA 24
FACULTY OF ENGINEERING
Course outcomes
Topic CO
Introduction to Sorting and Searching 1
Insertion Sort 2
Quick Sort, Merge Sort 3
Heap Sort 3
Sorting on several keys 3
List and Table sort 3
Comparison of sorting algorithms 4
SPECIFIC LEARNING OBJECTIVES
• Linear search algorithm finds a given element in a list of elements with O(n) time
complexity where n is total number of elements in the list.
• This search process starts comparing search element with the first element in the
list.
• If both are matched then result is element found otherwise search element is
compared with the next element in the list.
• Repeat the same until search element is compared with the last element in the list,
if that last element also doesn't match, then the result is "Element not found in the
list". That means, the search element is compared with element by element in the
list.
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
# Example usage
arr = [3, 5, 2, 4, 9]
target = 4
result = linear_search(arr, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found in the array")
Binary Search Algorithm
• Binary search algorithm finds a given element in a list of elements with O(log n) time
complexity where n is total number of elements in the list.
• The binary search algorithm can be used with only a sorted list of elements. That
means the binary search is used only with a list of elements that are already
arranged in an order.
• The binary search can not be used for a list of elements arranged in random order.
• This search process starts comparing the search element with the middle element in
the list. If both are matched, then the result is "element found". Otherwise, we check
whether the search element is smaller or larger than the middle element in the list.
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Example usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 4
result = binary_search(arr, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found in the array")
Sorting Algorithms
• Step 1 - Select the first element of the list (i.e., Element at first position in the list).
• Step 2: Compare the selected element with all the other elements in the list.
• Step 3: In every comparison, if any element is found smaller than the selected
element (for Ascending order), then both are swapped.
• Step 4: Repeat the same procedure with element in the next position in the list till
the entire list is sorted.
Sort the list in ascending order
Radix Sort Algorithm
• Radix sort is one of the sorting algorithms used to sort a list of integer numbers in
order.
• In radix sort algorithm, a list of integer numbers will be sorted based on the digits of
individual numbers.
• Sorting is performed from least significant digit to the most significant digit.
• Radix sort algorithm requires the number of passes which are equal to the number
of digits present in the largest number among the list of numbers.
• For example, if the largest number is a 3 digit number then that list is sorted with 3
passes.
Step by Step Process
• Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
• Step 2 - Consider the least significant digit of each number in the list which is to be
sorted.
• Step 3 - Insert each number into their respective queue based on the least significant
digit.
• Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have
inserted into their respective queues.
• Step 5 - Repeat from step 3 based on the next least significant digit.
• Step 6 - Repeat from step 2 until all the numbers are grouped based on the most
significant digit.
Merge Sort
• traverse from left and compare adjacent elements and the higher one is placed at
right side.
• In this way, the largest element is moved to the rightmost end at first.
• This process is then continued to find the second largest and place it and so on until
the data is sorted.
Input: arr[] = {6, 0, 3, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the array.
Second Pass:
• A heap is a binary tree-based data structure that satisfies the heap property: the
value of each node is greater than or equal to the value of its children.
• This property makes sure that the root node contains the maximum or minimum
value (depending on the type of heap), and the values decrease or increase as you
move down the tree.
• Types of Heaps
• There are two main types of heaps:
• Max Heap: The root node contains the maximum value, and the values decrease as
you move down the tree.
• Min Heap: The root node contains the minimum value, and the values increase as
you move down the tree.
• Every heap data structure has the following properties...
• Property #2 (Structural): All levels in a heap must be full except the last level and all
nodes must be filled from left to right strictly.
Max Heap
• Max heap is a specialized full binary tree in which every parent node contains greater
or equal value than its child nodes.
Operations on Max Heap
• Finding the node which has maximum value in a max heap is very simple. In a max
heap, the root node has the maximum value than all other nodes. So, directly we can
display root node value as the maximum value in max heap.
Insertion Operation in Max Heap
• Consider the below max heap. Delete root node (90) from the max heap.
Step 1 - Swap the root node (90) with last node 75 in max heap.
After swapping max heap is as follows...
Delete last node. Here the last node is 90. After deleting node
with value 90 from heap, max heap is as follows...
Step 3 - Compare root node (75) with its left child (89).
Step 4 - Here, left child value (89) is larger than its right sibling
(70), So, swap root (75) with left child (89).
Step 5 - Now, again compare 75 with its left child (36).
Here, node with value 75 is larger than its left child. So, we
compare node 75 with its right child 85.
Step 6 - Here, node with value 75 is smaller than its right child
(85). So, we swap both of them. After swapping max heap is as
follows...
Step 7 - Now, compare node with value 75 with its left child
(15).
Here, node with value 75 is larger than its left child (15) and it
does not have right child. So we stop the process.
2. Heapify Method
Time complexity O(n)
Prepare max heap from given sequence using Insert key one by
one in given order 14,24,12,11,25,8,35
Prepare min heap from given sequence using heapify method
145,40,25,65,12,48,18,1,100,27,7,3,45,9,3
Heap Sort Algorithm
• Heap sort is one of the sorting algorithms used to arrange a list of elements in order.
Heapsort algorithm uses one of the tree concepts called Heap Tree.
• In this sorting algorithm, we use Max Heap to arrange list of elements in Descending
order and Min Heap to arrange list elements in Ascending order.
Quick Sort Algorithm
• Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in
the list).
• Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
• Step 3 - Increment i until list[i] > pivot then stop.
• Step 4 - Decrement j until list[j] < pivot then stop.
• Step 5 - If i < j then exchange list[i] and list[j].
• Step 6 - Repeat steps 3,4 & 5 until i > j.
• Step 7 - Exchange the pivot element with list[j] element.
FOR PRACTICE:
• 35,50,15,25,80,20,90,45
Bubble sort 64, 25, 12, 22, 11