CHAPTER 2 Simple Searching and Sorting
CHAPTER 2 Simple Searching and Sorting
06/14/2024 1
Searching
06/14/2024 2
Problem: Search
• We are given a list of records.
• Each record has an associated key.
• Give efficient algorithm for searching for a record containing a
particular key.
06/14/2024 3
Search
[0] [1] [2] [3] [4] [ 700 ]
…
Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322
06/14/2024 5
Pseudocode for Seq. Search
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
int found=0;
int key=37;
for(int i=0;i<n && !found; i++)
{
if(arr[i]==key)
found=1;
}
return found;
06/14/2024 6
Sequential Search Analysis
• What are the worst and average case running times for sequential
(serial) search?
• We must determine the O-notation for the number of operations
required in search.
• Number of operations depends on n, the number of entries in the list.
06/14/2024 7
Worst Case Time Complexity of Sequential Search
06/14/2024 8
Average Case Time Complexity of Seq. Search
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1 array
access; if the second, then 2 array accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
06/14/2024 9
Average Case Time Complexity of Seq. Search
06/14/2024 10
Binary Search
• Perhaps we can do better than O(n) in the average case?
• Assume that we are given an array of records that is sorted. For
instance:
• an array of records with integer keys sorted from smallest to largest (e.g., ID
numbers), or
• an array of records with string keys sorted in alphabetical order (e.g., names).
06/14/2024 11
Binary Search Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
06/14/2024 12
Binary Search
3 6 7 11 32 33 53
06/14/2024 13
Binary Search
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
06/14/2024 17
Binary Search
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
06/14/2024 22
Binary Search
3 6 7 11 32 33 53
06/14/2024 } 24
return found;
Simple Sorting Algorithms
06/14/2024 25
Bubble sort
Compare each element (except the last one) with its neighbor to the right
(or to the left)
If they are out of order, swap them
This puts the largest element at the very end (or puts the smallest element at top)
The last (or the first) element is now in the correct and final place
Compare each element (except the last two) with its neighbor to the right
(or to the left)
If they are out of order, swap them
This puts the second largest element next to last (or puts the second smallest
element next to the first one)
The last two (or the first two) elements are now in their correct and final places
Compare each element (except the last three) with its neighbor to the
right (or to the left)
Continue as above until you have no unsorted elements on the left (or on the right)
06/14/2024 26
Example of bubble sort
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
06/14/2024 27
Implementation of bubble sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=n-1;i>0;i--)
{
for(int j=0;j<=i; j++)
{
if(arr[j+1]<arr[j])
{
int temp =arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
06/14/2024 28
}
Analysis of bubble sort
06/14/2024 29
Selection sort
• Given an array of length n,
• Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
• Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
• Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
• Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3
• Continue in this fashion until there’s nothing left to search
06/14/2024 30
Example and analysis of selection sort
The selection sort might swap an
7 2 8 5 4
array element with itself--this is
harmless, and not worth checking for
2 7 8 5 4
Analysis:
The outer loop executes n-1 times
2 4 8 5 7
The inner loop executes about n/2
times on average (from n to 2 times)
2 4 5 8 7 Work done in the inner loop is constant
(swap two array elements)
2 4 5 7 8 Time required is roughly (n-1)*(n/2)
You should recognize this as O(n2)
06/14/2024 31
Implementation of selection sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++)
{
int small=i;
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[small])
{
small=j;
}
}
int temp =arr[i];
arr[i]=arr[small];
arr[small]=temp;
06/14/2024 32
}
Insertion sort
• The outer loop of insertion sort is:
for (outer = 1; outer < n; outer++) {...}
• The invariant is that all the elements to the left of outer
are sorted with respect to one another
• For all i < outer, j < outer, if i < j then a[i] <= a[j]
• This does not mean they are all in their final correct place; the
remaining array elements may need to be inserted
• When we increase outer, a[outer-1] becomes to its left; we must
keep the invariant true by inserting a[outer-1] into its proper
place
• This means:
• Finding the element’s proper place
• Making room for the inserted element (by shifting over other
elements)
• Inserting the element
06/14/2024 33
Example insertion sort
06/14/2024 34
Implementation of Insertion Sort
//Insertion sort
int arr[]={89,2,67,37,72,17,4};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=1;i<n;i++)
{
for(int j=i;j>0 && arr[j]<arr[j-1];j--)
{
if(arr[j]<arr[j-1])
{
int temp =arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
}
06/14/2024 35
Analysis of insertion sort
We run once through the outer loop, inserting each of n elements;
this is a factor of n
On average, there are n/2 elements already sorted
The inner loop looks at (and moves) half of these
This gives a second factor of n/4
Hence, the time required for an insertion sort of an array of n
elements is proportional to n2/4
Discarding constants, we find that insertion sort is O(n2)
06/14/2024 36
A comparison of the asymptotic
complexities for three simple sorting
algorithms.
Bubble Selection Insertion
Comparison :
Best case O(n2) O(n2) O(n)
Average case O(n2) O(n2) O(n2)
Worst case O(n2) O(n2) O(n2)
Swap:
Best case 0 0 0
Average case O(n2) O(n) O(n2)
Worst case O(n2) O(n) O(n2)
06/14/2024 37
THE END
06/14/2024 38