0% found this document useful (0 votes)
15 views3 pages

Example 4-7: Question No 8

The document discusses different strategies for selecting the pivot element in Quicksort, including choosing the first, last, random, or median-of-k elements. It provides examples of Quicksort code that uses partitioning around a pivot element.

Uploaded by

gulzar ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Example 4-7: Question No 8

The document discusses different strategies for selecting the pivot element in Quicksort, including choosing the first, last, random, or median-of-k elements. It provides examples of Quicksort code that uses partitioning around a pivot element.

Uploaded by

gulzar ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question no 8

The choice of pivot strongly influences the relative sizes of the two


subarrays after the execution of partition. Instead of just
choosing pivot to be a random element, one can choose it to be the
median (middle value) of k random elements for some odd k. Later,
in the "Variations" section, we discuss different strategies for
selecting the pivot. The  Quicksort  implementation shown
in Example 4-7 can be defined in terms of the functions already
presented in "Median Sort." We use a standard optimization
technique that uses  Insertion Sort  when the size of the subarray to
be sorted falls below a predetermined minimum size. Selecting the
pivot element from a subarray A[left, left+n) must be an efficient
operation; it shouldn't require checking all n elements of the
subarray. Some alternatives are:
 Select first or last: A[left] or A[left+n−1]
 Select random element in A[left, left+n−1]
 Select median-of-k: the middle value of k random elements
in A[left, left+n−1]

Example:

/* low --> Starting index, high --> Ending index */


quickSort(arr[], low, high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is now
at right place */
pi = partition(arr, low, high);

quickSort(arr, low, pi - 1); // Before pi


quickSort(arr, pi + 1, high); // After pi
}
}

Alogrithm:
#include<iostream.h>
#include<conio.h>
inta[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout<<"enter 10 elements";
for(i=0;i<10;i++)
cin>> a[i];
l=0;
u=9;
quick(a,l,u);
cout<<"sorted elements";
for(i=0;i<10;i++)
cout<< a[i] << " ";
getch();
}
 
void quick(int a[],intl,int u)
{
intp,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
while(a[i] <= p &&i<j )
i++;
while(a[j]>p &&i<=j )
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;}
}
temp=a[j];
a[j]=a[l];
a[l]=temp;
cout<<"\n";
for(i=0;i<10;i++)
cout<<a[i]<<" ";
quick(a,l,j-1);
quick(a,j+1,u);
}
}

You might also like