Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views2 pages

Program 10

The document outlines a C/C++ program that implements the Quick Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes a partition function and a quicksort function, along with a main function that generates random integers and sorts them. The program also tracks the number of basic operations performed during sorting.

Uploaded by

vijethviju04
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)
20 views2 pages

Program 10

The document outlines a C/C++ program that implements the Quick Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes a partition function and a quicksort function, along with a main function that generates random integers and sorts them. The program also tracks the number of basic operations performed during sorting.

Uploaded by

vijethviju04
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/ 2

Program 10

Design and implement C/C++ Program to sort a given set of n integer elements using
Quick Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n.
The
elements can be read from a file or can be generated using the random number
generator.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int count=0;
int partition(int a[], int low,int high)
{
int pivot=a[low],temp,i=low+1,j=high;
while(1)
{
//Traverse i from left to right, segregating element of left group
while(i<=high && a[i]<=pivot)//a[i]<=pivot used for avoiding multiple duplicates
{
i++; count++;
}
//Traverse j from right to left, segregating element of right group
while(j>0 && a[j]>pivot)
{
j--; count++;
}
count+=2;
//If grouping is incomplete
if(i<j)
{
temp = a[i];
a[i] = a[j];
a[j] =temp;
}
else if(i>j)//If grouping is completed
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
else //Duplicate of Pivot found
return j;
}
}

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


{
int s;
if(low<high)
{
//partition to place pivot element in between left and right group
s = partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}

int main()
{
int a[10000],n;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
printf("All the elements:");
srand(time(0));
for(int i=0;i<n;i++)
{
a[i]=rand();
printf("%d ",a[i]);
}
quicksort(a,0,n-1);
printf("\nAfter sorting\n");
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\nNumber of basic operations = %d\n",count);
}

Sample Input and Output:


Enter the number of elements in an array:5
All the elements:
24442 6310 12583 16519 22767
After sorting
6310 12583 16519 22767 24442
Number of basic operations = 18

You might also like