Lecture 6
Lecture 6
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
n= 10
left right
middle
right
left
middle
Element
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 }
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.
Repeat the steps for all elements except the last one.
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
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.
{ 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.