AKJ - STD - Module-5 - Sorting - Searching - Hashing
AKJ - STD - Module-5 - Sorting - Searching - Hashing
Dr Anant. K. Jayswal
MTech(CSE), PhD(CSE)-JNU
BSc(CS),MSc(CS)-BHU
GATE(CS)
UGC-NET(CS)
Amity School of Engineering & Technology (CSE)
Binary Search
16
Amity School of Engineering & Technology (CSE)
17
Amity School of Engineering & Technology (CSE)
18
Amity School of Engineering & Technology (CSE)
19
Amity School of Engineering & Technology (CSE)
20
Amity School of Engineering & Technology (CSE)
21
Linear Vs Binary Search
Amity School of Engineering & Technology (CSE)
22
Sorting Algorithms
⚫ One of the fundamental problems of computer science is ordering a list of items.
Some sorting algorithms are simple and intuitive, such as the bubble sort.
Others, such as the quick sort are extremely complicated, but produce
lightening-fast results.
23
Bubble Sort
⚫ Suppose the list of numbers are given as: A[1], A[2], A[3], ……., A[N]. The
Bubble sort algorithm works as follows:
24
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
❑ 1st for loop for no of passes and 2nd for loop for no of comparison in each iterations. 25
Bubble Sort (Modified version1)-
-number of comparison in subsequent iteration should be reduced (pass1: (n-1) comparison, pass2: n-2
comparisons, and so on.
26
Bubble Sort (optimized version)-
- Sometimes not all passes are required to sort the given array of n-elements.
For example, an array of 10-elements may be sorted after 4 passes,
Or
If array is already sorted, no swapping is done in any pass then not all
passes are required in this case.
27
Bubble Sort (optimized version)-
- Some times not all passes are required to sort the given array of n-elements. For
example an array of 10-elements may be sorted after 4 passes. Or If array is already
sorted, no swapping is done in any pass then not all passes are required in this case.
𝑓𝑜𝑟 𝑖 = 0; 𝑖 < 𝑛 − 1; 𝑖 + +
{
𝒇𝒍𝒂𝒈 = 𝟎;
𝑓𝑜𝑟(𝑗 = 0; 𝑖 < 𝑛 − 1 − 𝑖; 𝑗 + +)
{
𝑖𝑓(𝐴[𝑗] > 𝐴[𝑗 + 1]
{
𝑡𝑒𝑚𝑝 = 𝐴 𝑗 ;
𝐴 𝑗 =𝐴 𝑗+1 ;
𝐴 𝑗 + 1 = 𝑡𝑒𝑚𝑝;
𝒇𝒍𝒂𝒈 = 𝟏;
}
} //end of inner for loop
𝑖𝑓 (𝑓𝑙𝑎𝑔 == 0)
𝑏𝑟𝑒𝑎𝑘;
}
28
Sorting Algorithms
Selection Sort:
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted
portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the
list and swaps it with the first element of the unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is sorted.
31
Selection Sort Algorithms…
32
Sorting Algorithms
Selection Sort:
How does Selection Sort Algorithm work?
Let's consider the following array as an example: arr[] = {64, 25, 12, 22, 11}
33
Sorting Algorithms…
Selection Sort:
34
Sorting Algorithms…
Selection Sort:
35
Sorting Algorithms…
Selection Sort:
36
Sorting Algorithms…
Selection Sort:
37
𝒇𝒐𝒓 𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏; 𝒊 + +
{
𝒊𝒏𝒕 𝒎𝒊𝒏 = 𝒊;
𝒇𝒐𝒓(𝒋 = 𝒊 + 𝟏 ; 𝒊 < 𝒏; 𝒋 + +)
{
𝒊𝒇(𝑨 𝒋 < 𝑨[𝒎𝒊𝒏]);
{
𝒎𝒊𝒏 = 𝒋;
}
}
𝒊𝒇 (𝒎𝒊𝒏!=i)
{
𝒔𝒘𝒂𝒑(𝑨 𝒊 𝒘𝒊𝒕𝒉 𝑨 𝒎𝒊𝒏 );
}
Time complexity:
In pass 1: (n-1) comparison to find the smallest elements, in
Pass2: (n-2) comparisons and so on..
So, f(n)=(n-1)+(n-2)+……._+2+1=O(n^2)
𝒇𝒐𝒓 𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏; 𝒊 + +
{
𝒊𝒏𝒕 𝒎𝒊𝒏 = 𝒊;
𝒇𝒐𝒓(𝒋 = 𝒊 + 𝟏 ; 𝒊 < 𝒏; 𝒋 + +)
{
𝒊𝒇(𝑨 𝒋 < 𝑨[𝒎𝒊𝒏]);
{
𝒎𝒊𝒏 = 𝒋;
}
}
𝒊𝒇 (𝒎𝒊𝒏!=i)
{
𝒔𝒘𝒂𝒑(𝑨 𝒊 𝒘𝒊𝒕𝒉 𝑨 𝒎𝒊𝒏 );
}
Sorting Algorithms…
Insertion Sort:
Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part
40
Amity School of Engineering & Technology (CSE)
Insertion Sort
41
Amity School of Engineering & Technology (CSE)
42
Amity School of Engineering & Technology (CSE)
43
Amity School of Engineering & Technology (CSE)
44
Amity School of Engineering & Technology (CSE)
Example2: A=[5,1,6,2,4,3]
45
Amity School of Engineering & Technology (CSE)
46
Quick Sort and Merge Sort
Divide and Conquer Approach
Divide-and- Amity School of Engineering & Technology (CSE)
conquer
49
Amity School of Engineering & Technology (CSE)
50
Quick Sort
Amity School of Engineering & Technology (CSE)
51
Amity School of Engineering & Technology (CSE)
Quick Sort
52
Amity School of Engineering & Technology (CSE)
Quick Sort
53
Amity School of Engineering & Technology (CSE)
Quick Sort
54
Amity School of Engineering & Technology (CSE)
Quick Sort
55
Amity School of Engineering & Technology (CSE)
Quick Sort
56
Amity School of Engineering & Technology (CSE)
57
Amity School of Engineering & Technology (CSE)
58
Amity School of Engineering & Technology (CSE)
Quick Sort-
Best case
59
[When array elements
Amity School of are unsorted
Engineering ] (CSE)
& Technology
60
Amity School of Engineering & Technology (CSE)
61
[When input array is already sorted]
Amity School of Engineering & Technology (CSE)
62
Amity School of Engineering & Technology (CSE)
Improving
Worst case
Behavior of
Take pivot element as Median.
Quicksort
63
Amity School of Engineering & Technology (CSE)
Improving
Worst case
Behavior of
Quicksort
64
Amity School of Engineering & Technology (CSE)
65
Amity School of Engineering & Technology (CSE)
Recurrence Relation(Average-case)
𝑇(𝑛) = 𝑇(𝑛/10) + 𝑇(9𝑛/10) + 𝑂(𝑛)
66
Amity School of Engineering and Technology
Merge Sort
67
Amity School of Engineering & Technology (CSE)
Divide into
A FirstPart SecondPart
two halves
Recursively
sort
FirstPart SecondPart
Merge
A is sorted!
68
Amity School of Engineering & Technology (CSE)
Merge (Concept)
Two sorted array A[ ], B[ ] is given, Lets see how to merge these two sorted
array into one sorted array C[ ].
𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 𝑀𝑒𝑟𝑔𝑒 𝐴, 𝐵, 𝑚, 𝑛
{
𝑖 = 1, 𝑗 = 1, 𝑘 = 1;
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚 && 𝑗 <= 𝑛
{
𝑖𝑓(𝐴[𝑖] < 𝐵[𝑗])
𝐶 𝑘++ =𝐴 𝑖++ ;
e𝑙𝑠𝑒
𝐶 𝑘++ =𝐵 𝑗++ ;
}
wℎ𝑖𝑙𝑒 𝑖 <= 𝑚
{
𝐶 𝑘 = 𝐴 𝑖 ; 𝑖 + +; 𝑘 + +;
}
wℎ𝑖𝑙𝑒 𝑗 <= 𝑚
{
𝐶 𝑘 = 𝐵 𝑗 ; 𝑗 + +; 𝑘 + +;
} 69
Amity School of Engineering & Technology (CSE)
Merge-sort (Example)
70
Merge Sort: Algorithm Amity School of Engineering & Technology (CSE)
71
Amity School of Engineering & Technology (CSE)
Merge-sort
(Example2)
Amity School of Engineering & Technology (CSE)
Here p=0, r=6
Or simply write
73
Merge Sort: Algorithm
Amity School of Engineering & Technology (CSE)
Two sorted array A[ ], B[ ] is given, Lets see how to merge these two sorted array into one
sorted array C[ ].
Sorted
A:
merge
Sorted Sorted
FirstPart SecondPart
A:
76
Amity School of Engineering & Technology (CSE)
77
Amity School of Engineering & Technology (CSE)
78
Amity School of Engineering & Technology (CSE)
79
Amity School of Engineering & Technology (CSE)
Merge-Sort Analysis
n cn
log n levels
n/4 n/4 n/4 n/4 4 × cn/4 = cn
n/2 × 2c = cn
2 2 2
Total: cn log n
Merge-Sort Summary
Approach: divide and conquer
Time
– Most of the work is in the merging
– Total time: (n log n)
Space:
– (n), more space than other sorts.
81
Amity School of Engineering and Technology
Binary Heaps:
The (binary) heap data structure is an array object that can be viewed as a complete binary tree, as shown in Figure.
Each node of the tree corresponds to an element of the array that stores the value in the node. The tree is completely
filled on all levels except possibly the lowest, which is filled from the left up to a point. An array A that represents a
heap is an object with two attributes: length[A], which is the number of elements in the array, and heap-size[A], the
number of elements in the heap stored within array A. The root of the tree is A[1], and given the index i of a node, the
indices of its parent PARENT(i), left child LEFT(i), and right child RIGHT(i) can be computed simply:
82
Amity School of Engineering and Technology
85
Amity School of Engineering and Technology
86
Amity School of Engineering and Technology
87
Amity School of Engineering and Technology
88
Amity School of Engineering and Technology
89
Amity School of Engineering and Technology
93
Heapsort Algorithm
Amity School of Engineering and Technology
Analysis:
BUILD_ MAX-HEAP: O(n)
For Loop: n-1 times
Exchange Element: O(1)
Max_Heapify: O(logn)
103
Amity School of Engineering and Technology
HASHING
• In searching techniques, search time depends on the number of elements (say n) in the array.
• A Hashing (or Hash addressing) is independent on the number of elements in an array and
expected search time is O(1).
• In hashing techniques, we use a Hash Function, which gives a hash address (location), where
the data (or key) is stored in the hash table.
• Generally we are taking a set of very large number and mapping into a set of small number.
115
Amity School of Engineering and Technology
Collision:
The hashing process generates a small number for a big key, so there is a possibility that two
keys could produce the same value. The situation where the newly inserted key maps to an
already occupied, and it must be handled using some collision handling technology, that is
h(k1)=h(k2)
116
Amity School of Engineering and Technology
117
Amity School of Engineering and Technology
Hash Function:
118
Amity School of Engineering and Technology
119
Amity School of Engineering and Technology
120
Amity School of Engineering and Technology
121
Amity School of Engineering and Technology
122
Amity School of Engineering and Technology
123
Amity School of Engineering and Technology
124
Amity School of Engineering and Technology
125
Amity School of Engineering and Technology
126
Amity School of Engineering and Technology
127
Amity School of Engineering and Technology
129
Amity School of Engineering and Technology
Module V completed
131