Adama Science and Technology University: Data Structures and Algorithm
Adama Science and Technology University: Data Structures and Algorithm
…
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;i++)
{
if(arr[i]==key)
found=1;
}
return found;
Serial Search Analysis
• What are the worst and average case running times for 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 for Serial Search
• For an array of n elements, the worst case time
for serial search requires n array accesses: O(n).
• Consider cases where we must loop over all n
records:
• desired record appears in the last position of the array
• desired record does not appear in the array at all
Average Case for 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
Average Case Time for 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
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
Code for 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
This sort is a sorting algorithm, specifically an in-place comparison sort
and it divides the input list in two parts sorted and unsorted.
• 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
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)
Code for 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
Code for 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;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)
THE END
41