Chapter 2
Chapter 2
Assistant Professor
IT-ICT Department, LJIET
Mr. Prayag Patel [email protected]
Problems solved by Algorithms
Sorting Combinatorial
Problem.
Searching
Problems Graph
solved by Problem
Algorithms
Numerical FA
Problem String Processing Problem
Mr. Prayag Patel Department: IT/ICT-LJIET
Applications of Sorting
1. Phone Bill: the calls made are date wise sorted.
2. Bank statement or Credit card Bill: transactions made
are date wise sorted.
3. Filling forms online: “select country” drop down box
will have the name of countries sorted in Alphabetical
order.
4. Online shopping: the items can be sorted price wise,
date wise or relevance wise.
5. Files or folders on your desktop are sorted date wise.
Example: Bubble sort, Selection sort, Insertion Sort, Heap sort, Shell sort, Quick sort.
Assistant Professor
IT-ICT Department, LJIET
Mr. Prayag Patel [email protected]
Bubble Sort
▪ It is a simple sorting algorithm that works by
• Comparing each pair of adjacent items and swapping them if they are in
the wrong order.
• The pass through the list is repeated until no swaps are needed, which indicates
that the list is sorted.
▪ As it only uses comparisons to operate on elements, it is a
comparison sort.
▪ Although the algorithm is simple, it is too slow for practical use.
▪ It is stable sorting algorithm.
▪ It is In place sorting algorithm.
Pass 1 :
34
45 34 34 34
swap
34
45 45 45 45
56 56 56
23 23
swap
23 23 23
56 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
2) 1 = 1 + 1 + 1 +……+ 1 = n – 1
3) i = 1 + 2 + 3 +……+ n = n(n+1) /2
Only one iteration takes place from 0 to n-2 (n-1-1), i.e. Pass 1
Assistant Professor
IT-ICT Department, LJIET
Mr. Prayag Patel [email protected]
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.
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) i = 1, Value A[i] = 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
Min = 4, value A[min] = -5
Swap
Step 4 :
Unsorted Array
(elements 4 to 8) i = 3, Value A[i] = 12
Swap
Step 6 :
Unsorted Array
(elements 6 to 8)
i = 5, value A[i] = 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
min = 6, value A[min] = 12
Swap
Swap
Step 8 :
Unsorted Array
(element 8)
i = 7, Value A[i] = 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
min = 8, value A[min]= 14
Swap
45 34 56 23 12 45
12 34 56 23 12
45
1 2 3 4 5 1 2 3 4 5
45
12 23
34 34
56 34
23
45
56 45
56
min ← j ;
temp ← A[i]; C4
A[i] ← A[min]; C5
A[min] ← temp; C6
= an2 + bn + c
= O (n2)
Assistant Professor
IT-ICT Department, LJIET
Mr. Prayag Patel [email protected]
Insertion Sort
Sort the following elements in Ascending order
5 1 12 -5 16 2 12 14
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Step 2 :
15 1 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Shift down
1 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
1
-5 5 12 -5 16 2 12 14
1 2 3 4 5 6 7 8
Shift down
Shift down
Shift down
-5 1 5 12 16 2 12 14
1 2 3 4 5 6 7 8
Step 6 :
-5 1 5
2 12 16 2 12 14
1 2 3 4 5 6 7 8
-5 1 2 12 12 14
5 12 16
1 2 3 4 5 6 7 8
Shift down
Step 8 :
-5 1 2 14 14
5 12 12 16
1 2 3 4 5 6 7 8
Shift down
Sorted List -5 1 2 5 12 12 14 16
j ← i - 1;
while x < T[j] and j > 0 do
T[j+1] ← T[j];
j ← j – 1;
T[j+1] ← x;
The best case time complexity of Insertion sort is
3 9 6 5 23 14 23
9 6 5 53 23 14
Step 2 :
Step 6 :
39 93 6 5 23 14
23 9
14 6 5 3 14
Step 3 :
39 36 63 5 23 14
Step 4 :
39 36 35 53 23 14
Assistant Professor
IT/ICT Department, LJIET
Mr. Prayag Patel [email protected]
Heap Sort
▪ 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
2 4 1
1
2. Min-Heap − Where the value of the root
node is less than or equal to either of its
2 4 children.
6 7 9
14 10
8 7 9 3
2 4 1
Array representation of heap
16 14 10 8 7 9 3 2 4 1
1
19
2 3 16
7
4 1 5 14 17
6
4 1 5 14 17 1 5 7 16
6 4 6
1 2 3 4 5 6
19 14 17 1 7 16
1 19
Swap &
remove
2 14 3 17 the last
element
6
4 1 5 7 16
1 2 3 4 5 6
1
16 14 17 1 7 19 16
2 14 17
3
4 5
1 7
1 2 3 4 5 6 1 2 3 4 5 6
17 14 16 1 7 19 7 14 16 1 17 19
1 17 1 7
Swap &
remove
2 14 3 16 the last 14 3
16
2
element
4 1 5 7 4 1
1 2 3 4 5 6 1 2 3 4 5 6
16 14 7 1 17 19 1 14 7 16 17 19
1 16 1 1
Swap &
remove
14 3 7 14 3 7
2 the last 2
element
4 1
1 2 3 4 5 6 1 2 3 4 5 6
14 1 7 16 17 19 7 1 14 16 17 19
1 1 7
14 Swap &
remove
the last
element 2 1
2 1 3 7
Already a Max-heap
Mr. Prayag Patel Department: IT/ICT-LJIET
Heap Sort Example
Step 3: swap the last node with the Step 2: Create Max-heap
root node and delete the last node
from the heap 1 2 3 4 5 6
1 7 14 16 17 19
1 2 3 4 5 6
1
7 1 14 16 17 19 1
1 7 Swap &
Remove the last element
remove
the last
element
2 1
1 2 3 4 5 6
1 7 14 16 17 19
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 9
1 7 49 7
4 5 6 4 5 6 4 5 6
2 9 3
7 2 19 3 2 1 3
4 5 6
2 1 3
4 5 6
2 1 9
Yes
Yes
4 5
2 1
Assistant Professor
IT/ICT Department, LJIET
Mr. Prayag Patel [email protected]
Counting Sort
• Sort the following elements in an ascending order.
3 6 4 1 3 4 1 4 2
Step 1
Index
Elements 3 6 4 1 3 4 1 4 2
Step 2
Index
Elements 0 0 0 0 0 0
3 6 4 1 3 4 1 4 2
Step 3
Index
Elements 2 1 2 3 0 1
+ +
Step 4
Index
Elements 2 3 5 8 8 9
Input Array A 3 6 4 1 3 4 1 4 2
Temporary Array C 23
2 5 87 8 9
Output Array B 1 1 2 3 3 4 4 4 6
Input Array A 3 6 4 1 3 4 1 4 2
Temporary Array C 6
21 2
3 5 78 8 9
Output Array B 1 1 2 3 3 4 4 4 6
Input Array A 3 6 4 1 3 4 1 4 2
Output Array B 1 1 2 3 3 4 4 4 6
The final array is sorted