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

Quicksort

Uploaded by

bgscse ise
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Quicksort

Uploaded by

bgscse ise
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Sort the given set of elements using quick sort method and determine the time required to sort

the elements. Repeat the experiment for different values of n, the number of elements in the list
to be sorted and plot a graph of the time taken versus n. The number of elements can be read
from a file or can be generated using random number generator.

# include< stdio.h>
# include <time.h>
# include<stdlib.h>
int partition( int a[], int low, int high)
{
int i, j, temp, key ;
key = a[low];
i= low+1;
j= high;
while(1)
{
while ( i< high && key >=a[i] )
i++;
while ( key < a[j] )
j--;
if (i<j)
{
temp = a[i];
a[i]= a[j];
a[j]=temp;
}
else
{
temp = a[low];
a[low]= a[j];
a[j]=temp;
return j;
}
}
}

void quick( int a[], int low, int high)


{
int j;
if( low< high)
{
j=partition (a,low,high);
quick (a,low,j-1);
quick (a, high,j+1);
}
}
void main( )
{
int n, i, a[20] ;
clock_t start, end;
printf( “ Enter the array size \n”);
scanf(“%d”, &n);
for (i=0; i<n; i++)
{
a[i]=rand( )%50;
}
printf( “ The array before sorting is \n”);
for (i=0; i< n; i++)
printf ( “ %d\n”, a[i] );
start = clock ( );
quick( a, 0, n-1 );
delay (1500);
end =clock ( );
printf( “ The sorted array is \n”);
for (i=0; i< n; i++)
printf ( “ %d\n”, a[i] );
printf (“ Time taken to sort is %f seconds \n”,(float)( end- start) / CLOCKS_PER_SEC );
}

You might also like