Leftovers As Exercises in Class
Leftovers As Exercises in Class
– If the target value is equal to the middle element of the array, then return the
index of the middle element
– Compare target with middle indexed value
– If target is greater than the middle value, then target can only lie in the right
half subarray, so the procedure (step 1 & 2) will be repeated for right subarray.
– If target is smaller than the middle value, then target can only lie in the left half
subarray, so the procedure (step 1 & 2) will be repeated for the left subarray.
else
high = mid - 1;
}
return -1;
}
Binary Search-Time Complexity
● Time Complexities
● Time Complexity
● Time Complexity:
● Worst case = O(n2)
● Average case = O(n2)
● Best case = O (n)
Selection Sort
● Time Complexity:
● Worst case = O(n2)
● Average case = O(n2)
● Best case = O (n2)
Selection Sort
● Disadvantage:
● Advantage:
● Poor efficiency with large list
● Works well on small lists
● It takes n2 no of steps for sorting of n
● No additional storage is needed
elements
● Time complexity