Quick Sort Viva2
Quick Sort Viva2
Quick Sort Viva2
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].
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
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