0% found this document useful (0 votes)
33 views86 pages

5 Searching and Sorting (Selection, Bubble)

Searching and sorting

Uploaded by

Aditya Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views86 pages

5 Searching and Sorting (Selection, Bubble)

Searching and sorting

Uploaded by

Aditya Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

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.

➢ Two searching algorithms

 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

• The elements of the array need not be in sorted order.


• It can be applied on any linear data structures even if elements
of data structures don’t occupy the contiguous memory locations.
• Start from beginning and compare with each element and continues
until element is found or searched is made.
• The complexity of linear search is Big O(n).
Example : consider a linear array a as

11 22 33 44 55

4/79
Linear Search Algorithm

Searching the position of given element “Data” in an array ‘S’


having ‘n’ elements.
1. Repeat steps 2 and 3 for i=1 to n
2. If S[i] = Data then
Print “ Element is found at position “: i
Exit
[End If]
3. Set i= i+1 [End loop]
4. Print: “ Desired element Data is NOT found in the array”
5. Exit

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

➢ Search for 22 in the above sorted list

➢ The three indexes are first, mid and last.


➢ Given first as 0 and last as 11, mid is calculated as
follows:
mid = (first + last) / 2
mid = (0 + 11) / 2 = 11 / 2 = 5

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.

first mid last


Target: 22
0 5 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 > 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

Set BEG =MID +1.(end of if structure)


4. Set MID =INT((BEG + END)/2). (End of step 2 loop.)
5. If DATA[MID]=ITEM then: Set LOC= MID.
Else:
Set LOC= NULL.
(end of if structure)
6.Exit.
11/79
Complexity Analysis of Binary Search
• The best case for binary search occurs when the element being searched for is
exactly at the middle of the sorted array. In this case only comparison is
required to find that desired element giving a best case runtime of Big O(1).
• Worst case for the binary search occurs when the element is not found in the
array. since binary search halves the sorted array in each step until there are
no values that can be halved.
• The efficiency of binary search in this case can be expressed as logarithmic
function. For calculating the worst complexity, the number of elements in the
array as power of two (i.e. n=2x).
After 1st comparison, number of elements remains=n/21 =n/2
After 2nd comparison, number of elements remains=n/22 =n/2
After 3rd comparison, number of elements remains=n/23 =n/2
:
After xth comparison, number of elements remains=n/2x=1
Total number of maximum comparisons = x =log2 n

12/79
What is sorting?
• Sorting is the process of arranging data into
meaningful order to analyze it more effectively.

• Order sales data by calendar month to produce a


graph of sales performance.

• Sort text data into alphabetical order.

• Sort numeric data into numerical order.

• Group sort data to many levels.

• Sort on City within Month within Year


13/79
Sorting
❖ Sorting is the process of rearranging data
elements/Items in ascending or descending order

❖ 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.

❖ Seldom we sort isolated values

❖ Usually we sort a number of records


containing a number of fields based
on a key.
15/79
Sorting
19991532 Suresh Mohan VV Puram Bangalore
19990253 Robert D'Souza Ghatkoper Mumbai
19985832 Kalim Pasha M G Road Mangalore
20003541 Ganesh Karker Sardar Road Hubli
19981932 Carol Mathew Avenue Road Mulki
Numerically by ID Number 20003287 Robinson David Kalsanka Udupi

19981932 Carol Mathew Avenue Road Mulki


19985832 Kalim Pasha M G Road Mangalore
19990253 Robert D'Souza Ghatkoper Mumbai
19991532 Suresh Mohan VV Puram Bangalore
20003287 Robinson David Kalsanka Udupi
20003541 Ganesh Karker Sardar Road Hubli
Lexicographically by surname, then given name

19981932 Carol Mathew Avenue Road Mulki


20003541 Ganesh Karker Sardar Road Hubli
19985832 Kalim Pasha M G Road Mangalore
20003287 Robinson David Kalsanka Udupi
19990253 Robert D'Souza Ghatkoper Mumbai
19991532 Suresh Mohan VV Puram Bangalore
16/79
Sorting algorithms
❖ Bogo Sort: shuffle and pray
❖ Selection Sort: look for the smallest element, move to front
❖ Bubble Sort: swap adjacent pairs that are out of order
❖ Insertion Sort: build an increasingly large sorted front portion
❖ Merge Sort: recursively divide the array in half and sort it
❖ Heap Sort: place the values into a sorted tree structure
❖ Quick Sort: recursively partition array based on a middle value

❖ other specialized sorting algorithms:


❖ Bucket Sort: cluster elements into smaller groups, sort them
❖ Radix Sort: sort integers by last digit, then 2nd to last, then ...
❖ ...

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

S[1] is compared with S[2] ; as S[1]>S[2] then exchange takes


place.

66/79
Bubble sort

1 2 3 4 5 6
7 8 15 22
5 11 15

If(8>5) then
interchange

S[2] is compared with S[3];as S[2]>S[3],then exchange


1 2 3 4 5 6
7 5 8 11 15 2

If(8>11) then no interchange

S[3] is compared with S[4];as S [3]< S[4],then no exchange takes


place.

67/79
Bubble Sort

1 2 3 4 5 6
7 5 8 11 15 2

If(11>15) then no interchange


S[4] is compared with S[5];as S[4]< S[5],then no exchange takes place.
1 2 3 4 5 6

7 5 8 11 15 2

If(15>2) then interchange

S[5] is compared with S[6]; as S[5]>S[6], then exchange


.
1 2 3 4 5 6

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

Pass 2: n-2 = 6-2 = 4


1 2 3 4 5 6

7 5 8 11 2 15

If(7>5) then
interchange

S[1] is compared with S[2];as S[1]> S[2],then exchange takes


pl1ace. 2 3 4 5 6

5 7 8 11 2 15

If(7>8) then no
interchange

S[2] is compared with S[3];as S[2]<S[3],then no exchange takes


place.

69/79
Bubble Sort

1 2 3 4 5 6
5 7 8 11 2 15

If(8>11) then no
interchange

S[3] is compared with S[4];as S[3]<S[4],then no exchange


1 2 3 4 5 6

5 7 8 11 2 15

If(11>2) then
interchange

S[4] is compared with S[5];as S[4]>S[5],then exchange

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

S[1] is compared with S[2];as S[1]<S[2],then no exchange takes


place.

71/79
Bubble Sort

1 2 3 4 5 6

5 7 8 2 1 1
1 5
If(7<8) then no
interchange

S[2] is compared with S[3];as S[2]<S[3],then no


e1xcha2nge 3take4s pla5ce. 6
5 7 8 2 1 1
1 5
If(8>2) then
interchange

S[3] is compared with S[4];as S[3]>S[4],then exchange takes


place.

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

S[1] is compared with S[2];as S[1]<S[2],then no exchange takes


place.

73/79
Bubble Sort

1 2 3 4 5 6

5 7 2 8 1 1
1 5
If(7>2) then
interchange

S[2] is compared with S[3];as S[2]>S[3],then exchange takes


place.
1 2 3 4 5 6

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

Pass 5: n-5 = 6-5 = 1


1 2 3 4 5 6

5 2 7 8 1 1
1 5
If(5>2) then
interchange

S[1] is compared with S[2];as S[1]>S[2],then exchange takes


pl1ace. 2 3 4 5 6

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

{ for( int i=0 ; i<=n-2 ; ++i)

{ 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;

}
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

{ for( int i=0 ; i<=n-pass-1 ; ++i)

{ 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;

}
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

You might also like