0% found this document useful (0 votes)
6 views4 pages

9.selsort and 10.quicksort Programs

The document provides C/C++ programs for sorting a set of n integer elements using Selection Sort and Quick Sort methods, including time complexity analysis. It instructs to run the programs for n values greater than 5000, record the sorting time, and plot the results. The programs generate random numbers for sorting and include functions for printing the array and measuring execution time.

Uploaded by

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

9.selsort and 10.quicksort Programs

The document provides C/C++ programs for sorting a set of n integer elements using Selection Sort and Quick Sort methods, including time complexity analysis. It instructs to run the programs for n values greater than 5000, record the sorting time, and plot the results. The programs generate random numbers for sorting and include functions for printing the array and measuring execution time.

Uploaded by

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

9.

Design and implement C/C++ Program to sort a given set of n integer elements using Selection 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. analysis: worst case, average case and best case.

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

void selection(int a[], int n)


{
int i, j, min;
for (i = 0; i <= n-2; i++)
{
min = i;
for (j = i+1; j <= n-1; j++)
{
if (a[j] < a[min])
min = j;
}
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}

int main()
{
int a[10000],n, i;
struct timeval t;
double s, e;

printf("Enter the number of elements\n");


scanf("%d",&n);

printf("Before sorting array elements are - \n");


for(i=0;i<n;i++)
a[i]=random()%100;
printArr(a, n);
gettimeofday(&t,NULL);
s=t.tv_sec+(t.tv_usec/1000000.0);
selection(a, n);
gettimeofday(&t,NULL);
e=t.tv_sec+(t.tv_usec/1000000.0);

printf("\nAfter sorting array elements are - \n");


printArr(a, n);

printf("\ntime taken to sort %d elements is %lf\n", n, (e-s));


return 0;
}
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.

int partition (int a[], int low, int high)


{
int p,i,j,temp;
p=a[low];
i=low ;
j=high+1;
do
{
do {i++;}while(a[i]<p);
do {j--;}while(a[j]>p);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}while(i<j);
temp=a[i];
a[i]=a[j];
a[j]=temp;

temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
void qsort(int a[], int low, int high)
{
if(low<high)
{
int s=partition(a, low, high);
qsort(a,low,s-1);
qsort(a,s+1,high);
}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}

int main()
{
int a[10000],n, i;
struct timeval t;
double s, e;

printf("Enter the number of elements\n");


scanf("%d",&n);

printf("Before sorting array elements are - \n");


for(i=0;i<n;i++)
a[i]=random()%100;
printArr(a, n);

gettimeofday(&t,NULL);
s=t.tv_sec+(t.tv_usec/1000000.0);
qsort(a, 0, n-1);
gettimeofday(&t,NULL);
e=t.tv_sec+(t.tv_usec/1000000.0);

printf("\nAfter sorting array elements are - \n");


printArr(a, n);

printf("\ntime taken to sort %d elements is %lf\n", n, (e-s));


return 0;
}

You might also like