Professional Documents
Culture Documents
something in our day to day life, car keys, books, pen, mobile
charger and what not.
Same is the life of a computer, there is so much data stored in it,
that whenever a user asks for some data, computer has to search
it's memory to look for the data and make it available to the user.
And the computer has it's own techniques to search through it's
memory fast.
Searching in the data structure can be done by implementing
◦ As linear search algorithm does not use any extra space, thus its space
complexity = O(n) for an array of n number of elements.
Time Complexity
◦ Worst-case complexity: O(n) – This case occurs when the search
element is not present in the array.
◦ Best case complexity: O(1) – This case occurs when the first element is
the element to be searched.
◦ Average complexity: O(n) – This means when an element is present
somewhere in the middle of the array.
goes to the middle element of the list, divides the array into 2
parts, and decides element lies in which sub-array the element
exists.
Suppose ARR is an array with sorted n number of elements
within BEG and END, which are the beginning and ending index
of sub-arrays.
The index MID defines the middle index of the array where,
Binary Search Working
Binary Search Algorithm can be implemented in two ways which
x = 4 is found.
Binary Search Algorithm
Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid – 1
Recursive Method
binarySearch(arr, x, low, high)
if low > high
return 0
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid] // x is on the right side
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side
return binarySearch(arr, x, low, mid - 1)
Iterative Method
int binarySearch(int array[], int x, int low, int high)
{ // Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
} return -1;
}
Recursive Method
int binarySearch(int array[], int x, int low, int high)
{
if (high >= low)
{
int mid = low + (high - low) / 2;
// If found at mid, then return it
if (array[mid] == x)
return mid;
// Search the left half
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1);
// Search the right half
return binarySearch(array, x, mid + 1, high);
}
return -1;
}
Binary Search Complexity
Time Complexities
Terminologies
Search Key (24, 52, 91, 67, 48, 83) : We place these search key
Quadratic Probing
In this case we use (h+i^2 mod n)
Example: If we have new value 30 then 30 mod 6 = 0, it mean
oth index should be used to store 30, but this index is already
fulfilled with 24. Then h(value) = 0
Then again we use (h+i^2 mod n), here i represents how many
times we checked the space where 30 should be stored.
Here i=2 then we get : 0+1^2 mod 6 =1, 1 index is already
fulfilled with 19 then prob(i) will be increased by 1, we get
i=2, then again
h+i^2 mod 6 = 0+2^2 mod 6=4, 4th index is available so, 30 can
be inserted in this location.