0% found this document useful (0 votes)
58 views

Quick Sort and Binary Search

Quicksort is a sorting algorithm that uses a divide and conquer approach. It works by picking a pivot element and partitioning the array into subarrays of elements less than and greater than the pivot. It then recursively sorts the subarrays. The steps are: 1) pick a pivot and partition the array; 2) recursively sort the subarrays; 3) join the sorted subarrays. Binary search is a searching algorithm used to find an element in a sorted array. It works by repeatedly dividing the search interval in half and going to the half where the target may lie.

Uploaded by

Geetha M P
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Quick Sort and Binary Search

Quicksort is a sorting algorithm that uses a divide and conquer approach. It works by picking a pivot element and partitioning the array into subarrays of elements less than and greater than the pivot. It then recursively sorts the subarrays. The steps are: 1) pick a pivot and partition the array; 2) recursively sort the subarrays; 3) join the sorted subarrays. Binary search is a searching algorithm used to find an element in a sorted array. It works by repeatedly dividing the search interval in half and going to the half where the target may lie.

Uploaded by

Geetha M P
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

QUICK SORT

M. P. Geetha,
Department of CSE,
Sri Ramakrishna Institute of Technology,
Coimbatore
Quick-sort is a randomized sorting algorithm
based on the divide-and-conquer paradigm:

Divide: pick a random element x (called pivot)


and partition S into
L elements less than x
E elements equal x
G elements greater than x

Recur: sort L and G

Conquer: join L, E and G


Select a pivot (partitioning element)

Rearrange the list so that all the elements in the


positions before the pivot are smaller than or equal to
the pivot and those after the pivot are larger than the
pivot

Exchange the pivot with the last element in the first


(i.e., ≤ sublist) – the pivot is now in its final position
Sort the two sublists

A[i]≤p A[i]>p
The partition algorithm
partition (l,h)
{
pivot = A[l];
i=l; j=h;
while(i<j)
{
do
{
i++;
}while(A[i]<pivot);
do
{
j++;
}while(A[j]>pivot);
if(i<j)
swap(A[i],A[j]);
}
swap(A[l],A[j]);
return j;
}
Quicksort algorithm
quicksort (l,h)
{
if(l<h)
{
j=partition(l,h);
quicksort(l,j);
quicksort(j+1,h);
}
}
Binary Search
Searching
 
Searching is a process of finding a particular element
among several given elements.
The search is successful if the required element is
found.
Otherwise, the search is unsuccessful.
 
Searching Algorithms
 
Searching Algorithms are a variety of algorithms
used for the purpose of searching.

The searching of an element in the given array may


be carried out in the following two ways-
 
 
Linear Search
Binary Search
Binary Search
 
Binary Search is one of the fastest searching algorithms. It
is used for finding the location of an element in a linear
array. It works on the principle of divide and conquer
technique.
 
Binary Search Algorithm can be applied only on Sorted
arrays.
 
So, the elements must be arranged in- Either ascending
order if the elements are numbers. Or dictionary order if
the elements are strings.
 
To apply binary search on an unsorted array,
First, sort the array using some sorting technique.
Then, use binary search algorithm.
Binary Search Algorithm-
 
Consider-
There is a linear array ‘a’ of size ‘n’.
Binary search algorithm is being used to search an
element ‘item’ in this linear array.

If search ends in success, it sets key to the mid of the


element otherwise it sets key to -1.

Variables low (l) and high(h) keeps track of the index of


the first and last element of the array or sub array in
which the element is being searched at that instant.

Variable mid keeps track of the index of the middle


element of that array or sub array in which the element is
being searched at that instant.
 
Example:
Algorithm:
Int RBinSearch(l,h,key)
{
If(l=h)
{
if(A[l] ==key)
return l;
else
return 0;
}
else
{
mid=(l+h)/2;
if(key==A[mid])
return mid;
if(key<A[mid])
return RBinSearch(l,mid-1,key);
else
return RBinSearch(mid+1,h,key);
}

}
Time Complexity Analysis:
Best Case :
If number of elements in the list is 1 and the key is
present means then Time complexity T(n) = 1

Worst Case:
Otherwise Recurrence Relation, T(n) = T(n/2)+1

T(n) = 1 n=1
T(n/2)+1 n>1

Here 1 is the constant to represent comparison

Time complexity = O(log n)


Binary Search Algorithm
Advantages
 
• It eliminates half of the list from further searching by
using the result of each comparison.
• It indicates whether the element being searched is
before or after the current position in the list.
• This information is used to narrow the search.
• For large lists of data, it works significantly better
than linear search.
 
Binary Search Algorithm Disadvantages
 

• It employs recursive approach which requires


more stack space.
• Programming binary search algorithm is error
prone and difficult.
• The interaction of binary search with memory
hierarchy i.e. caching is poor.

You might also like