5 Searching and Sorting (Selection, Bubble)
5 Searching and Sorting (Selection, Bubble)
1/79
Searching
➢ Searching is the process of determining whether or not a given
value exists in a data structure or a storage media.
Linear Search
Binary Search
2/79
Linear Search
The linear (or sequential) search algorithm on an array
is:
• Start from beginning of an array/list and continues
until the item is found or the entire array/list has
been searched.
• Sequentially scan the array, comparing each array
item with the searched value.
• Linear search algorithm has complexity of O(n).
• linear search can be applied to both sorted and unsorted
arrays.
3/79
Linear Search
11 22 33 44 55
4/79
Linear Search Algorithm
5/79
Binary Search
➢ Binary search algorithm is efficient if the array is sorted.
➢ A binary search is used whenever the list starts to become
large.
➢ The binary search starts by testing the data in the element at the
middle of the array to determine if the target is in the first or
second half of the list.
➢ If it is in the first half, no need to check the second half.
➢ If it is in the second half, no need to test the first half.
➢ In turn eliminate half the list from further consideration.
➢ Repeat this process until the target is found or until the list has
elements to be divided.
➢ If there are no elements to be divided, conclude that the item
does not exist in the list.
➢ To find the middle of the list, we need three variables,
➢ one to identify the beginning of the list,
➢ one to identify the middle of the list, and one to identify the
end of the list. 6/79
Binary Search
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
7/79
Binary Search
➢ At index location 5, the target is greater than the list value (22 >
21).
➢ Therefore, discard the array locations 0 through 5 (mid is
automatically eliminated).
➢ To narrow down the search, assign mid + 1 to first and repeat the
search.
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
22 > 21
8/79
Binary Search
➢ The next loop calculates mid with the new value for
first and determines that the midpoint is now 8 as
follows:
mid = (6 + 11) / 2 = 17 / 2 = 8
Target: 22
first mid last
6 8 11
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
22 < 62
9/79
Binary Search
➢ When we test the target to the value at mid a second time, we discover that the
target is less than the list value (22 < 62).
➢ This time we adjust the end of the list by setting last to mid – 1 and recalculate
mid.
➢ This step effectively eliminates elements 8 through 11 from consideration.
➢ We have now arrived at index location 6, whose value matches our target. This
stops the search.
Target: 22
first mid last
6 6 7
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 7 8 10 14 21 22 36 62 77 81 91
first mid last
6 6 7 22 = 22
function terminates
10/79
Binary Search Algorithm
BINARY(DATA, LB, UB, ITEM, LOC)
1. [Initialize segment variables.]
Set BEG = LB,END =UB and MID = INT(BEG + END)/2).
2. Repeat Steps 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM
3. If ITEM< DATA[MID], then:
Set END = MID -1 else
12/79
What is sorting?
• Sorting is the process of arranging data into
meaningful order to analyze it more effectively.
❖ Unsorted Data
4 3 2 7 1 6 5 8 9
❖ Sorted Data (Ascending)
1 2 3 4 5 6 7 8 9
❖ Sorted Data (Descending)
9 8 7 6 5 4 3 2 1
14/79
Sorting
❖ Sorting can be numerical or
lexicographical order.
17/79
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
18/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
19/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
20/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
21/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
22/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
23/15
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
24/15
Selection Sort
5 1 3 4 6 2
Largest
Comparison
Data Movement
Sorted
25/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
26/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
27/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
28/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
29/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
30/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
31/15
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
32/15
Selection Sort
5 1 3 4 2 6
Largest
Comparison
Data Movement
Sorted
33/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
34/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
35/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
36/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
37/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
38/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
39/15
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
40/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
41/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
42/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
43/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
44/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
45/15
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
46/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
47/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
48/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
49/15
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
50/15
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
51/15
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
52/15
Selection Sort
1 2 3 4 5 6
DONE!
Comparison
Data Movement
Sorted
53/15
Write the code for
Selection Sort
54/15
Is it the right code for Selection Sort?
for ( i = 0 ; i < n-1 ; i++ )
{ index = i;
for ( j = i+1 ; j < n ; j++ )
{ if(A[j] < A[index])
index = j;
}
i = variable to traverse the array A
temp = A[i]; index = variable to store the index
of minimum element
A[i] = A[index];
j = variable to traverse the unsorted
A[index] = temp; sub-array
temp = temporary variable used for
} swapping
55/79
Time Complexity Analysis
Ex. To sort 6 2 11 7 5 in ascending order
Step-01: for i = 0
56/79
Time Complexity Analysis
Step-02: for i = 1
57/79
Time Complexity Analysis
Step-03: for i = 2
58/79
Time Complexity Analysis
Step-04: for i = 3
59/79
Time Complexity Analysis
Step-05: for i = 4
60/79
Time Complexity Analysis
❖ With each loop cycle,
The minimum element in unsorted sub-array is selected.
It is then placed at the correct location in the sorted sub-array
until array A is completely sorted.
❖ Selection sort algorithm consists of two nested loops.
❖ Owing to the two nested loops, it has O(n2) time complexity
Time Complexity
Best Case n2
Average Case n2
Worst Case n2
61/79
Space Complexity Analysis
❖ Selection sort is an in-place algorithm.
❖ It performs all computation in the original array and no other
array is used.
❖ Hence, the space complexity works out to be O(1).
❖ Note :
❖ Selection sort is not a very efficient algorithm when data sets
are large.
❖ This is indicated by the average and worst case complexities.
❖ Selection sort uses minimum number of swap operations
O(n) among all the sorting algorithms.
62/79
Bubble sort
➢It is an in-place sorting algorithm; It uses no auxiliary
data structures (extra space) while sorting.
➢ Bubble sort uses multiple passes (scans) through an
array.
➢In each pass, bubble sort compares the adjacent
elements of the array.
➢It then swaps the two elements if they are in the wrong
order.
➢In each pass, bubble sort places the next largest
element to its proper position.
➢In short, it bubbles down the largest element to its
correct position.
63/79
Bubble sort
➢With the selection sort, we make one exchange at the
end of one pass.
➢The bubble sort improves the performance by
making more than one exchange during its pass.
➢By making multiple exchanges, we will be able to
move more elements toward their correct positions
using the same number of comparisons as the
selection sort makes.
➢The key idea of the bubble sort is to
make pairwise comparisons and exchange the
positions of the pair if they are out of order.
64/79
Bubble Sort
65/79
Bubble sort
Example : We have an unsorted array list ‘S’ having 6 elements as shown below:
1 2 3 4 5 6
8 7 5 11 15 2
An Unsorted Array ‘S’ with 6 elements. Various Passes takes place to
sort the list .
Pass 1 : Number of steps= n-1 =6 -1 = 5.
1 2 3 4 5 6 If(8>7) then
5 11 15 2 interchange
8 7
66/79
Bubble sort
1 2 3 4 5 6
7 8 15 22
5 11 15
If(8>5) then
interchange
67/79
Bubble Sort
1 2 3 4 5 6
7 5 8 11 15 2
7 5 8 11 15 2
7 5 8 11 2 15
The 1st largest element 15 has obtained it’s proper
position S[6] in 5 comparison
68/79
Bubble Sort
7 5 8 11 2 15
If(7>5) then
interchange
5 7 8 11 2 15
If(7>8) then no
interchange
69/79
Bubble Sort
1 2 3 4 5 6
5 7 8 11 2 15
If(8>11) then no
interchange
5 7 8 11 2 15
If(11>2) then
interchange
70/79
Bubble Sort
1 2 3 4 5 6
5 7 8 2 11 15
The 2nd largest element 11 has obtained it’s proper position S[5] in 4
comparisons
Pass 3: n-3 = 6-3 = 3
1 2 3 4 5 6
5 7 8 2 11 15
If(5<7) then no
interchange
71/79
Bubble Sort
1 2 3 4 5 6
5 7 8 2 1 1
1 5
If(7<8) then no
interchange
72/79
Bubble Sort
1 2 3 4 5 6
5 7 2 8 1 1
1 5
The 3rd largest element 8 has obtained its proper position
S[4] in 3 comparisons.
Pass 4: n-4 = 6-4 = 2
1 2 3 4 5 6
5 7 2 8 1 1
1 5
If(5<7) then no
interchange
73/79
Bubble Sort
1 2 3 4 5 6
5 7 2 8 1 1
1 5
If(7>2) then
interchange
5 2 7 8 1 1
1 5
The 4th largest element 7 has obtained its proper position S[3]
in 2 comparisons.
74/79
Bubble Sort
5 2 7 8 1 1
1 5
If(5>2) then
interchange
2 5 7 8 1 1
1 5
The S[1] and S[2] have obtained its proper position in just a
single comparison.
75/79
Write the code for
Bubble Sort
76/79
Is it the right code for Bubble Sort?
for( int pass=1 ; pass<=n-1 ; ++pass) // passes through array
A[i] = A[i+1];
A[i+1] = temp;
}
77/79
// C program for Bubble Sort (Correct ???)
#include <stdio.h>
main()
{ int i,n=5,j,key, A[12] = { 907,556,43,-5,6,404,55,3,22,-122,4,55};
for( i=0;i<n;i++)
printf(" %d ",A[i]);
for( int pass=1 ; pass<=n-1 ; ++pass) // passes through array
{ for( i=0 ; i<=n-2 ; ++i)
{ printf("\n Pass No: %d, i: %d, Comapre A[%d] & A[%d] i.e.%d &
%d",pass,i,i,i+1,A[i],A[i+1]);
if(A[i] > A[i+1]) // If adjacent elements are in wrong order
{ printf(" Swapping %d & %d",A[i],A[i+1]);
int temp = A[i]; // Swap them
A[i] = A[i+1];
A[i+1] = temp;
}
}
}
78/79
printf("\n");
for( i=0;i<n;i++)
printf(" %d ",A[i]);
}
79/79
907 556 43 -5 6
Pass No: 1, i: 0, Comapre A[0] & A[1] i.e.907 & 556 Swapping 907 & 556
Pass No: 1, i: 1, Comapre A[1] & A[2] i.e.907 & 43 Swapping 907 & 43
Pass No: 1, i: 2, Comapre A[2] & A[3] i.e.907 & -5 Swapping 907 & -5
Pass No: 1, i: 3, Comapre A[3] & A[4] i.e.907 & 6 Swapping 907 & 6
Pass No: 2, i: 0, Comapre A[0] & A[1] i.e.556 & 43 Swapping 556 & 43
Pass No: 2, i: 1, Comapre A[1] & A[2] i.e.556 & -5 Swapping 556 & -5
Pass No: 2, i: 2, Comapre A[2] & A[3] i.e.556 & 6 Swapping 556 & 6
Pass No: 2, i: 3, Comapre A[3] & A[4] i.e.556 & 907
Pass No: 3, i: 0, Comapre A[0] & A[1] i.e.43 & -5 Swapping 43 & -5
Pass No: 3, i: 1, Comapre A[1] & A[2] i.e.43 & 6 Swapping 43 & 6
Pass No: 3, i: 2, Comapre A[2] & A[3] i.e.43 & 556
Pass No: 3, i: 3, Comapre A[3] & A[4] i.e.556 & 907
Pass No: 4, i: 0, Comapre A[0] & A[1] i.e.-5 & 6
Pass No: 4, i: 1, Comapre A[1] & A[2] i.e.6 & 43
Pass No: 4, i: 2, Comapre A[2] & A[3] i.e.43 & 556
Pass No: 4, i: 3, Comapre A[3] & A[4] i.e.556 & 907
-5 6 43 556 907
80/79
Is it the right code for Bubble Sort?
for( int pass=1 ; pass<=n-1 ; ++pass) // passes through array
A[i] = A[i+1];
A[i+1] = temp;
}
81/79
// C program for Bubble Sort
#include <stdio.h>
main()
{ int i,n=5,j,key, A[12] = { 907,556,43,-5,6,404,55,3,22,-122,4,55};
for( i=0;i<n;i++)
printf(" %d ",A[i]);
for( int pass=1 ; pass<=n-1 ; ++pass) // passes through array
{ for( i=0 ; i<=n-pass-1 ; ++i)
{ printf("\n pass %d, i =%d, comapre %d & %d",pass,i,A[i] , A[i+1]);
if(A[i] > A[i+1]) // If adjacent elements are in wrong order
{ int temp = A[i]; // Swap them
A[i] = A[i+1];
A[i+1] = temp;
}
}
}
82/79
printf("\n");
for( i=0;i<n;i++)
printf(" %d ",A[i]);
}
83/79
907 556 43 -5 6
pass 1, i =0, comapre 907 & 556
pass 1, i =1, comapre 907 & 43
pass 1, i =2, comapre 907 & -5
pass 1, i =3, comapre 907 & 6
pass 2, i =0, comapre 556 & 43
pass 2, i =1, comapre 556 & -5
pass 2, i =2, comapre 556 & 6
pass 3, i =0, comapre 43 & -5
pass 3, i =1, comapre 43 & 6
pass 4, i =0, comapre -5 & 6
-5 6 43 556 907
84/79
Complexity of Bubble Sort Algorithm
❖ The complexity of any sorting algorithm is analysed
through the number of Comparisons required during the
sorting procedure.
❖ In this algorithm an Array of size n gets sorted after n-1
passes.
❖ n-1 comparisons take Place during the 1st pass which
places the largest element of the array on the last
Position
❖ n-2 comparisons take place in the 2nd pass which places
the second largest Element of the array on the second
last position
85/79
Complexity of Bubble Sort Algorithm
❖ kth pass requires n-k comparisons which places kth
largest element at (n-k+1)th position of the array and
❖ the last pass requires only one comparison.
❖ Total number of comparison will be:
F(n) =(n-1)+(n-2)+(n-3)+………….+(n-k)+2+1
=(n-1)+(n-2)+………..+2+1
= ((n-1)*n)/2
❖ The complexity of bubble sort algorithm will be Big
O(n2)
86/79