Binary Search
Binary Search
array is sorted in ascending order, and find the complexity of your algorithm.
input parameters
a: an array to search in (sorted in ascending order)
: an integer index to the first element of the array
r: an integer index to the last element of the array
key: the element to search for
We make use of the fact that the array is sorted by comparing the key
with the element in the middle of the array: if they are equal we are done, else
if the key < this element then, we search in the left half of the array, else we
search in the right half of the array. Binary search is one of the well-known
... ...
l m r
i
int binarySearch ( a [ ], , r, key )
if l <= r
m = ( l + r ) / 2;
if ( key = am ) return m;
else if (key < am) // search the left half of the array
return binarySearch (a, l, m – 1, key);
else // key > am: search the right half of the array
return binarySearch (a, m + 1, r, key);
else
return –1; // key not found
Time complexity
relation:
( ) ( ), ( )
This shows that the binary search algorithm is much more efficient than the
input parameters
a: an array to be sorted
ii
: an integer index to the first element of the array
r: an integer index to the last element of the array
output parameter
a: the array after sorting
Quick sort applies divide and conquer by dividing the array into 2 sub
arrays (hopefully of equal size) and then applies the same idea to the left and
right sub arrays over and over until we have arrays of size 1, at which point
we are finished.
...
(compared to a pivotal element x) are moved to the left and large ones are
...≤x x ...≥x
l i r
That is:
, ,
, , and
.
The element x is called the pivot. Now, to sort the array a, we proceed as
follows:
iii
void quick_sort ( a [ ], l, r )
if l < r
partition ( a, l, r, i ); // i is the index of the pivot
quick_sort ( a, l, i-1 ); // sort the left sub array
(quick_sort ( a, i+1, r); // sort the right sub array
The partition algorithm, shown next, is the part which is doing the
actual work of comparing and moving the array elements. Notice that the
iv
Time complexity of the quick sort algorithm
elements in the best case. The best case happens if we are so lucky that the
partition algorithm will divide the array into 2 equal sub arrays.
( ) ( ), ( )
Its’ solution is given by: ( ) It can also be shown that the average
sort algorithm is much more efficient than many other sorting algorithms such
input parameters
a, b: 2 arrays, each of which is sorted in increasing order
na, nb: the number of elements in a and b
output parameters
c: an array, the result of the merge in increasing order
nc: the number of elements in c
In the general step, we compare ai with bj and copy the smaller element
into ck. If 2 elements are equal we copy one and only one of them to ck.
v
...
i ...
...
j ...
... ...
k ...
the merge operation in the worst case. Each time we go through the while
If 2 elements are equal one of them is copied into c. The worst case happens
when there are no equal elements and the ends of the two arrays are reached at
the same time inside the while loop. Hence the total number of comparisons =
na+nb-1 = O(na+nb).
vii