Unit 7 - DS
Unit 7 - DS
Sorting and
Searching
Pass 1 :
34
45 34 34 34
swap
34
45 45 45 45
56 56 56
23 23
23 23 23
56 swap 56
12
swap
12 12 12 12
56
34 34 34 23
34 23 12
23
swap
swap
45 45
23 23 23
34 34
12 12
23
swap
swap
23 23
45 45
12 12 12
34 34
swap
12 12 12
45 45 45 45
56 56 56 56 56 56
Algorithm: Bubble_Sort(A)
for i ← 1 to n do 𝜽 (𝒏)
for j ← 1 to n-i do
if A[j] > A[j+1] then
thenswap(A[j],A[j+1]
temp← ←A[j]
temp A[j] 𝜽 𝒏 )
( 𝟐
A[j]← ←A[j+1]
)A[j] A[j+1]
A[j+1]← ←temp
A[j+1] temp
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
Unsorted Array (elements 2 to 8) Minj = 1, Minx = 5
Find the smallest value
-5
5 1 12 -5
5 16 2 12 14 from the entire Unsorted
1 2 3 4 5 6 7 8 array
Swap
Index = 4, value = -5
Selection Sort
Step 3 : Minj = 2, Minx = 1
Unsorted Array (elements 3 to 8)
Find min value from
-5 1 12 5 16 2 12 14 Remaining unsorted array
1 2 3 4 5 6 7 8 Index = 2, value = 1
Step 4 :
Unsorted Array
(elements 4 to 8) Minj = 3, Minx = 12
Swap
Selection Sort
Step 5 :
Unsorted Array Minj = 4, Minx = 5
(elements 5 to 8)
Find min value from
-5 1 2 5 16 12 12 14 Remaining unsorted array
Index = 4, value = 5
1 2 3 4 5 6 7 8
Step 6 :
Unsorted Array
(elements 6 to 8)
Minj = 5, Minx = 16
-5 1 2 5 12
16 16
12 12 14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 6, value = 12
Swap
Selection Sort
Step 7 :
Unsorted Array Minj = 6, Minx = 16
(elements 7 to 8)
Find min value from
-5 1 2 5 12 12
16 16
12 14 Remaining unsorted array
Index = 7, value = 12
1 2 3 4 5 6 7 8
Swap
Step 8 :
Unsorted Array
(element 8)
Minj = 7, Minx = 16
-5 1 2 5 12 12 14
16 16
14 Find min value from
1 2 3 4 5 6 7 8 Remaining unsorted array
Index = 8, value = 14
Swap
Selection Sort
• Selection sort divides the array or list into two parts,
1. The sorted part at the left end
2. and the unsorted part at the right end.
• Initially, the sorted part is empty and the unsorted part is the
entire list.
• The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element
becomes a part of the sorted array.
• Then it finds the second smallest element and exchanges it
with the element in the second leftmost position.
• This process continues until the entire array is sorted.
Selection Sort - Algorithm
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i;
minx ← A[i];
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
A[minj] ← A[i];
A[i] ← minx;
Selection Sort - Example Pass 1 :
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i; minx ← A[i];
i = 1
for j ← i + 1 to n do minj ← 21
if A[j] < minx then minx ← 34
45
minj ← j ; minx ← A[j];
A[minj] ← A[i]; j = 23
A[i] ← minx; No Change
45
45 34 56 23 12 45
12 34 56 23 12
45
1 2 3 4 5 1 2 3 4 5
45
12 34
23 56
34 23
34
45
56 45
56
Selection Sort - Analysis
# Input: Array A
# Output: Sorted array A
Algorithm: Selection_Sort(A)
for i ← 1 to n-1 do
minj ← i;
minx ← A[i]; 𝜃 ( 𝑛)
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
𝜃 (𝑛 )
2
A[minj] ← A[i];
A[i] ← minx;
The time complexity of Selection sort is 𝜽 (𝒏 )
𝟐
Introduction to Heap
• A heap data structure is a binary tree with the following two
properties.
1. It is a complete binary tree: Each level of the tree is
completely filled, except possibly the bottom level. At this level
it is filled from left to right.
a a
b c b c
d e f d e f
9 9
6 7 6 7
2 4 8 2 4 1
4 10 3 5 1
10
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1
4 10 3 5 1
10 3
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1
4 10 3 5 1
10 3
5
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1
4 10 3 5 1
Now, a binary 4
tree is created
and we have to
10 3
convert it into a
Heap.
5 1
Heap Sort – Example 1
Sort the following elements in Ascending order
4 10 3 5 1
4 10 3 5 1
4 10 3 5 1
4 10 3 5 1
10 4 3 5 1
10 5 3 4 1
10 5 3 4 1
1 5 3 4 10
1 5 3 4 10
5 1 3 4 10
5 4 3 1 10
1 4 3 5 10
1 4 3 5 10
4 1 3 5 10
3 1 4 5 10
3 1 4 5 10
1 3 4 5 10
4 1 7 2 9 3 4 9 7 2 1 3 9 4 7 2 1 3
i=3 1 i=2 1 i=1 1
4 4 94
2 3 2 3 2 3
1 37 19 7 49 7
4 5 6 4 5 6 4 5 6
2 9 3
7 2 19 3 2 1 3
Heap Sort - Algorithm
• # Input: Array A 9 4 7 2 1 3
• # Output: Sorted array A 1
9
• Algorithm: Heap_Sort(A[1,…,n])
2 3
BUILD-MAX-HEAP(A) 7
4
for i length[A] downto 2 6
4 5
do exchange A[1] A[i]2 1 3
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
Heap Sort - Algorithm
• # Input: Array A 3 4 7 2 1 9
• # Output: Sorted array A 1
3
• Algorithm: Heap_Sort(A[1,…,n])
2 3
BUILD-MAX-HEAP(A) 7
4
for i length[A] downto 2 6
4 5
do exchange A[1] A[i]2 1 9
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
Max-Heapify - Algorithm
Algorithm: Max-heapify(A, i, n) 1
3
l LEFT(i) l 2 1 2 3
r RIGHT(i) r 3 4 7
if l ≤ n and A[l] > A[i] Yes 4 5
then largest l largest 2 2 1
else largest i
Yes
if r ≤ n and A[r] > A[largest]
then largest r largest 3
if largest i Yes
then exchange A[i]
A[largest]
MAX-HEAPIFY(A, largest, n)
Heap Sort - Algorithm
• # Input: Array A
1
• # Output: Sorted array A 7
• Algorithm: Heap_Sort(A[1,…,n]) 2 3
4 3
BUILD-MAX-HEAP(A)
4 5
for i length[A] downto 2 2 1
do exchange A[1] A[i]
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
O(𝑛 −1)
O ( log 𝑛 )
Heap Sort - Analysis
• # Input: Array A
• # Output: Sorted array A
• Algorithm: Heap_Sort(A[1,…,n])
BUILD-MAX-HEAP(A)
O(𝑛 log2𝑛)
for i length[A] downto
do exchange A[1] 𝑛A[i]
−1
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1, n)
O(log 𝑛)
Running time of heap sort algorithm is:
Radix Sort
• Radix Sort puts the elements in order by comparing the digits
of the numbers.
• Each element in the -element array has digits, where digit is
the lowest-order digit and digit is the highest order digit.
Algorithm: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort
array A on digit i
• Sort following elements in ascending order using radix sort.
363, 729, 329, 873, 691, 521, 435, 297
Radix Sort - Example
3 6 3
7 2 9
3 2 9
8 7 3
6 9 1
5 2 1
4 3 5
2 9 7
Sort on column 1
Radix Sort - Example
6 9 1
5 2 1
3 6 3
8 7 3
4 3 5
2 9 7
7 2 9
3 2 2
Sort on column 2
Radix Sort - Example
5 2 1
7 2 9
3 2 9
4 3 5
3 6 3
8 7 3
6 9 1
2 9 7
1 2 3 4 5 6 7 8
1 2 1 2 Split 1 2 1 2
724 521 2 98 529 31 189 451
1 1 1 1 1 1 1 1
724 521 2 98 529 31 189 451
0 1 2 3 4 5 6 7 6 7
42 23 74 11 65 58 94 36 99 87
LB UB
Quick Sort - Example
Procedure pivot(T[i,…,j]; 0 1 2 3 4 5 6 7 8 9
var l)
p ← T[i] 42 23 74 11 65 58 94 36 99 87
k ← i; l ← j+1 k l
Repeat
k ← k+1 until T[k] <= p Swap
or k ≥ j
Repeat 42 23 36
74 11 65 58 94 74
36 99 87
l ← l-1 until T[l] > p k l
While k < l do
Swap T[k] and T[l]
Swap
Repeat k ← k+1 until
T[k] <= p 42
11 23 36 42
11 65 58 94 74 99 87
Repeat l ← l-1 until k l
T[l] > p
Swap T[i] and T[l]
LB = 0, UB = 9 p = 42
k = 0
l = 10
Quick Sort - Example
LB UB
Procedure pivot(T[i,…,j]; 0 1 2 3 4 5 6 7 8 9
var l)
p ← T[i] 11 23 36 42 65 58 94 74 99 87
k ← i; l ← j+1
Repeat
k ← k+1 until T[k] <= p 11 23 36
or k ≥ j
Repeat k l
l ← l-1 until T[l] > p
While k > l do LB UB
Swap T[k] and T[l]
Repeat k ← k+1 until 11 23 36 42 65 58 94 74 99 87
T[k] <= p
Repeat l ← l-1 until 23 36
T[l] > p k l
Swap T[i] and T[l]
11 23 36 42 65 58 94 74 99 87
Quick Sort - Example LB UB
4 5 6 7 8 9
Procedure pivot(T[i,…,j]; 11 23 36 42 65 58 94 72 99 87
var l)
Swap
p ← T[i]
k ← i; l ← j+1
Repeat 65 65
58 58 94 72 99 87
k ← k+1 until T[k] <= p
or k ≥ j k l
Repeat
l ← l-1 until T[l] > p 65 65 94 72 99 87
58
While k > l do
Swap T[k] and T[l]
Repeat k ← k+1 until LB UB
T[k] <= p
Repeat l ← l-1 until
11 23 36 42 65
58 65 94 72 99 87
T[l] > p
Swap T[i] and T[l]
Quick Sort - Example LB UB
Swap
Procedure pivot(T[i,…,j]; var
l) 94 72 87
99 99
87
p ← T[i]
k l
k ← i; l ← j+1 Swap
Repeat
k ← k+1 until T[k] <= p or k ≥ 94 72 94
87 87 99
j
k l
Repeat
l ← l-1 until T[l] > p LB UB
While k > l do
Swap T[k] and T[l] Swap
Repeat k ← k+1 until 72 87
87 72 94 99
T[k] <= p
Repeat l ← l-1 until k l
T[l] > p
Swap T[i] and T[l]
11 23 36 42 58 65 72 87 94 99
68
6 5 3 1 8 7 2 4
Pass - 1 : Select First Record and considered as Sorter Sub-array
6 5 3 1 8 7 2 4
Sorted Unsorted
6 5 3 1 8 7 2 4
5 6 3 1 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 3 : Select Third record and Insert at proper place in sorted
array
5 6 3 1 8 7 2 4
3 5 6 1 8 7 2 4
Sorted Unsorted
3 5 6 1 8 7 2 4
1 3 5 6 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 5 : Select Fifth record and Insert at proper place in sorted
array
1 3 5 6 8 7 2 4
8 is at proper position
1 3 5 6 8 7 2 4
Sorted Unsorted
1 3 5 6 8 7 2 4
1 3 5 6 7 8 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in
sorted array
1 3 5 6 7 8 2 4
1 2 3 5 6 7 8 4
Sorted Unsorted
Pass - 8 : Select Eighth Record and Insert at proper place in sorted
array
1 2 3 5 6 7 8 4
1 2 3 4 5 6 7 8
Sorted Unsorted
Linear/Sequential Search
• Suppose, you are given a jar containing
some business cards.
• You are asked to determine whether
the name “Mukesh Ambani" is in the
jar.
• To do this, you decide to simply go
through all the cards one by one.
• How long this takes?
• Can be determined by how many
cards are in the jar, i.e., Size of Input.
Linear Search
• Linear search is a method for finding a particular value
from the given list.
• The algorithm checks each element, one at a time and in
sequence, until the desired element is found.
• Linear search is the simplest search algorithm.
Linear Search - Example
Search for in given array
Comparing value of ith index with the given element one by one, until we get the
required element or end of the array
Step 1: i=0 Step 3: i=2
i i
2≠1 3≠1
Step 2: i=1 Step 4: i=3
𝟏
i i
9≠1 Element found at ith index, i=3
Linear Search Algorithm
# Input : Array A, element
# Output : First index of element in A or
-1 if not found
Algorithm: Linear_Search
for i = 0 to last index of A:
if A[i] equals element:
return i
return -1
Linear Search Time Complexity
• The required element in the given array can be found at,
1. E.g. 2: It is at the first position. Time complexity O(1)
Best Case: minimum comparison is required
2. E.g. 3 or 1: Anywhere after the first position. Time complexity
O((n+1)/2)
Average Case: average number of comparison is
required
3. E.g. 8 or 7: Last position or does not found at all.Time
complexity O(n)
Worst Case: maximum comparison is required Worst
2 9 3 1 8 Case
Best
Case
Average
Case
82
Binary Search
• Binary Search is an extremely well-known instance of
divide-and-conquer approach.
• Let be an array of increasing sorted order; that is
whenever .
• Let be some number. The problem consists of finding in
the array if it is there.
• If is not in the array, then we want to find the position
where it might be inserted.
Binary Search Time Complexity
• Best Case Complexity - In Binary search, best case
occurs when the element to search is found in first
comparison, i.e., when the first middle element itself is the
element to be searched. The best-case time complexity of
Binary search is O(1).
• Average Case Complexity - The average case time
complexity of Binary search is O(logn).
• Worst Case Complexity - In Binary search, the worst
case occurs, when we have to keep reducing the search
space till it has only one element. The worst-case time
complexity of Binary search is O(logn).
Binary Search - Example
Input: sorted array of integer values. .
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
3 6 7 11 32 33 53
IsIs7 7= <midpoint
midpointkey? NO.
value?
YES.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
3 6 7 11 32 33 53
<
=
> value of midpoint? NO.
YES.
87
3 6 7 11 32 33 53