Simple Searching and Sorting (Slide) (1)
Simple Searching and Sorting (Slide) (1)
…
Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322
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;
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.
Worst Case Time Complexity of Sequential 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
Average Case Time Complexity of Seq. Search
3 6 7 11 32 33 53
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
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
27548
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;
}
}
}
Analysis of bubble sort
•
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
Example and analysis of selection sort
The selection sort might swap an
72854
array element with itself--this is
harmless, and not worth checking for
27854
Analysis:
The outer loop executes n-1 times
24857 The inner loop executes about n/2
times on average (from n to 2 times)
24587 Work done in the inner loop is constant
(swap two array elements)
24578 Time required is roughly (n-1)*(n/2)
You should recognize this as O(n2)
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;
}
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
Example insertion sort
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;
}
}
}
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)
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)
THE END
38