0% found this document useful (0 votes)
21 views21 pages

Lecture 6

Uploaded by

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

Lecture 6

Uploaded by

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

Analysis of

Algorithm
LECTURE 6
INSTRUCTOR: DR . HUSNAIN ASHFAQ
Binary Search

The binary search is much more efficient than the linear search.
The array should be sorted
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new
search region is the lower half of the data.
5. If your target is greater than the middle data value, the
new search region is the higher half of the data.
6. Continue from Step 2.
index
0 1 2 3 4 5 6 7 8 9
6 11 17 25 26 46 60 66 77 88
array

left middle right

n= 10

Searching for 60.

Left Right Middle


0 9 4
Element > Array[middle] so we will change our left and left
becomes middle +1 in the next search region
index
0 1 2 3 4 5 6 7 8 9
6 11 17 25 26 46 60 66 77 88
array

left middle right

Searching for 59.

Left Right Middle


5 9 7

Element < Array[middle] so we will change our right and right


becomes middle -1 in the next search region
index
0 1 2 3 4 5 6 7 8 9
6 11 17 25 26 46 60 66 77 88
array

left right
middle

Searching for 59.

Left Right Middle


5 6 5

Element > Array[middle] so we will change our left and left


becomes middle +1 in the next search region
index
0 1 2 3 4 5 6 7 8 9
6 11 17 25 26 46 60 66 77 88
array

right
left
middle
Element

Searching for 59.

Left Right Middle


6 6 6
index
0 1 2 3 4 5 6 7 8 9
6 11 17 25 26 46 60 66 77 88
array

Lets suppose the element


you are searching is not in right
left
the array then how do we
know when to stop for middle
example. Searching for 61
instead of 59.

Left Right Middle


7 6 6

 When left > right means you have to stop and return element not
found in the array
int binarySearch(arr, n, target) {
int left = 0;
int right = arr.size() - 1;
while (left <= right) {
int mid = (left +right) / 2;
if (arr[mid] == target) {
return mid; // Target found }

if (arr[mid] < target) {


left = mid + 1; // Search the right half
} else {
right = mid - 1; // Search the left half
}
}
return -1; // Target not found
Time Complexity Analysis
Best Case
Average Case
Worst Case
Time Complexity (worst Case
and average case)
T(n) = T (n/2) + 1
Stop condition: n/ = 1
T(n) = T( n/4) +1+1 n = ……… Equation (2)

T(n)= T(n/8) +1+1+1 For finding K taking on both sides of equation 2


T(n)= T(n/16) +1+1+1+1
= ()………….. Equation 3
.
using logarithm property, the equation 3 becomes
.
. =
Put the value of k and n in equation 1
T(n)= T(n/ + k …… Equation (1)
T(n) = T(/ + = T(1 +

T(n) = O ()
Time Complexity Analysis
Best Case O(1)
Average Case O(log n)
Worst Case O (log n)
Bubble Sort
Compare adjacent elements. If the first is greater than
the second, swap them.

Do this for each pair of adjacent elements, starting


with the first two and ending with the last two. At this
point the last element should be the greatest.

Repeat the steps for all elements except the last one.

Continue for one less element each time, until there


are no more pairs to compare.
0 1 2 3 4
14 15 5 7 4

Pass 1
Pass 3
14 15 5 7 4 5 7 4 14 15
Iteration Iteration
s 14 15 5 7 4 s 5 7 4 14 15
14 5 15 7 4 5 4 7 14 15
14 5 7 15 4
5 4 7 14 15
14 5 7 4 15 5 4 7 14 15
Pass 2 Pass 4
5 4 7 14 15
14 5 7 4 15
Iteration Iteration 4 5 7 14 15
s 5 14 7 4 15 s
4 5 7 14 15
5 7 14 4 15
5 7 4 14 15 4 5 7 14 15
Sorted
5 7 4 14 15 4 5 7 14 15 Array
How to write a code:
1.Outer loop will be number of passes
2.Inner loop will be number of iteration in one pass

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


{
for (j=0 ; j<n-1; j++)
{
if A[j] > A[j+1]
{ temp = A[j];
A [j]= A[j+1];
A[j+1] = temp;}
}
}
Time complexity Analysis
The outer loop run for n-1 passes
The inner loop runs for n-1 iterations
T(n)= (n-1)*(n-1)
T(n)= -n-n-1=
T(n) = O
Modification in the
bubble sort code
0 1 2 3 4
14 15 5 7 4

No these are
already sorted
Pass 1 i=0 it will work
Pass 3 i=2
but it will
14 15 5 7 4 5 7 4 14 15
Iteration Iteration increase the
s 14 15 5 7 4 s time
5 7 4 14 15
14 5 15 7 4 complexity
5 4 7 14 15 Do we need
to compare
14 5 7 15 4
5 4 7 14 15 these two?
14 5 7 4 15 5 4 7 14 15
Pass 2 i=1 Pass 4 i=3
5 4 7 14 15
14 5 7 4 15
Iteration Iteration 4 5 7 14 15 Do we need
s 5 14 7 4 15 s to compare
4 5 7 14 15 these two?
5 7 14 4 15
5 7 4 14 15 Do we need 4 5 7 14 15
to compare
5 7 4 14 15 these two? 4 5 7 14 15
By adding this the
for (i= 0; i< n-1 ; i++) unnecessary iteration will
{ not be done. How it will
for (j=0 ; j<n-1-i; j++) work? Lets take one case
{ i=2
if A[j] > A[j+1] When i value is
{ temp = A[j]; increasing the number
A [j]= A[j+1]; of comparisons are
A[j+1] = temp;} decreasing
} J=0 J< n-1-i A[0]>A[1] swap if
} 0<5-1-2 condition is true.

J=1 J<n-1-i A[1]>A[2] swap if


1<5-1-2 condition is true.

J=2 J<n-1-i Will get out from


2<5-1-2 (condition inner loop and i++
fail) and i become 3
0 1 2 3 4
14 15 4 5 7

Pass 1 i=0 Pass 3 i=2


14 15 4 5 7 4 5 7 14 15
Iteration Iteration
s 14 15 4 5 7 s 4 5 7 14 15
14 4 15 5 7 4 7 14 15
14 4 5 15 7
4 5 7 14 15
14 4 5 7 15
No swapping is done in pass 3
Pass 2 i=1
and array is already sorted but
14 4 5 7 15 according to previous code
Iteration
then pass 4 will be executed
s 4 14 5 7 15 and its unnecessary. Then you
4 5 14 7 15 have to check if no swapping in
the pass then you have to
4 5 7 14 15 break the for loop.
If no swapping is done then we can break the
outer loop for example in this case we break after
pass 3 and we did not go

for (i= 0; i< n-1 ; i++) Pass 3 i=2

{ 4 5 7 14 15
Iteration
Flag =0; s 4 5 7 14 15
for (j=0 ; j<n-1-i; j++)
{ 4 7 14 15
if A[j] > A[j+1] 4 5 7 14 15
{ temp = A[j];
A [j]= A[j+1];
A[j+1] = temp;
Flag =1;}
}
If (flag==0)
{Break;}
} This logic is also known as optimized bubble sort
Time complexity comparison of
standard bubble sort and
optimized bubble sort
Time Complexity Comparison
1.Best Case:
•Standard Bubble Sort: O , as it still performs all n−1passes even if the array is already sorted.
•Optimized Bubble Sort: O(n), as it stops after the first pass if no swaps are needed, meaning
the array is already sorted.
2.Worst Case:
•Both versions have a time complexity of O() in the worst case (e.g., when the array is sorted in
reverse order), as they both perform nearly all comparisons and swaps.
3.Average Case:
•Standard Bubble Sort: O.
•Optimized Bubble Sort: O ) in the average case as well, because, in an unsorted array, the flag
will likely not allow early termination for most cases, and both algorithms will perform approximately the
same number of operations.

You might also like