You are on page 1of 5

MAILAM ENGINEERING COLLEGE

(Approved by AICTE, New Delhi, Affiliated to Anna University Chennai


& Accredited by National Board of Accreditation (NBA) New Delhi)

Mailam, Villupuram - 604 304

DEPARTMENT OF COMPUTER APPLICATIONS


ALGORITHMS LAB MC9229
(VIVA QUESTIONS WITH ANSWERS)

QUICK SORT
1. What is meant by Quick sort?
Quick sort is a well-known sorting algorithm developed by C. A. R. Hoare that,
on average, makes (n log n) comparisons to sort n items. Quick sort is significantly
faster in practice than other (n log n) algorithms, because its inner loop can be
efficiently implemented on most architecture, and in most real-world data.
2. How to choice of pivot element?
In quick sort the leftmost element of the partition would often be chosen as
the pivot element. Selecting a pivot element is also complicated by the existence of
integer overflow.
3. List out the types of Scan methods?
If the boundary indices of the sub-array being sorted are sufficiently large,
the nave expression for the middle index, (left + right)/2, will cause overflow and
provide an invalid pivot index.
This can be overcome by using, for example, left + (right-left)/2 to index the
middle element, at the cost of more complex arithmetic. Similar issues arise in some
other methods of selecting the pivot element.
4. How quick sort works?
The basic idea of Quick sort is to repeatedly divide the array into smaller
pieces, and to recursively sort those partitions. Quick sort divides the current
partition by choosing an element - the pivot - finding which of the other elements is
smaller or larger, sorting them into two different sub-partitions.
5. Write the operations of quick sort?

Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1] and A[s+1..r] such
that each element of the first array is A[s] and each element of the second
array is A[s].

Implication: A[s] will be in its final position in the sorted array.

Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by recursive calls to
quicksort

Combine: No work is needed, because A[s] is already in its correct place after
the partition is done, and the two subarrays have been sorted.

6. What are the three situations may arise depending upon the scanning
indices have crossed in quick sort?

If scanning indices I and j have not corssed, i< j we simply exchange A[i] and
A[j] and resumes the scan by incrementing I and decrementing j respectively:
i
j

p
all are p
are p

...

all

If the scanning indices have crossed over , i> j we have partitioned the array
after exchanging the pivot with A[j]:
Finally if the scanning indices stop while pointing to the same element, i=j,
the value they are pointing to must be equal to p. Thus we have partitioned
the array

7. Write the efficiency of quick sort?


Efficiency of Quick sort Based on whether the partitioning is balanced.

Best case: split in the middle ( n log n)


o C(n) = 2C(n/2) + (n)
//2 subproblems of size n/2 each
Worst case: sorted array! ( n2)
o C(n) = C(n-1) + n+1 //2 subproblems of size 0 and n-1 respectively
Average case: random arrays ( n log n)

SUBJECT IN-CHARGE

HOD

PRINCIPAL

QUICK SORT

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
void quicksort(int [],int,int);
clrscr();
printf("\n\t\t\t QUICK SORT");
printf("\n\t\t\t ==========\n");
printf("\n\t\t ENTER THE NUMBER OF TERMS:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\t\t ENTER %d VALUE:",i+1);
scanf("%d",&a[i]);
}
quicksort(a,0,n-1);
printf("\n\t\t THE SORTED ELEMENTS ARE AS FOLLOWS:");
for(i=0;i<n;i++)
{
printf("\n\n\t\t\t\t\t%d",a[i]);
}
getch();
}
int partition(int a[],int l,int r)
{
int p,i,j,temp;
p = a[l];
i = l;
j = r+1;
while(i<j)
{

do
{
i++;
}
while(a[i]<p);
{
do
{
j--;
}
while(a[j]>p);
{
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
a[l] = a[j];
a[j] = p;
return(j);
}
void quicksort(int a[],int l,int r)
{
int s;
int partition(int [],int,int);
if(l>=r)
{
return;
}
s = partition(a,l,r);

quicksort(a,l,s-1);
quicksort(a,s+1,r);
}
OUTPUT:
QUICK SORT
========
ENTER THE NUMBER OF TERMS:5
ENTER 1 VALUE:56
ENTER 2 VALUE:86
ENTER 3 VALUE:12
ENTER 4 VALUE:-1
ENTER 5 VALUE:0
THE SORTED ELEMENTS ARE AS FOLLOWS:
-1
0
12
56
86

You might also like