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

Daa Lab PR 9 10

The document describes the implementation of two sorting algorithms, Selection Sort and Quick Sort, in C/C++. It includes code for both algorithms, instructions for generating random integers, and measuring the time taken to sort arrays of varying sizes greater than 5000. Additionally, it provides sample output demonstrating the time taken for sorting different numbers of elements.

Uploaded by

Mr. LION
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)
6 views5 pages

Daa Lab PR 9 10

The document describes the implementation of two sorting algorithms, Selection Sort and Quick Sort, in C/C++. It includes code for both algorithms, instructions for generating random integers, and measuring the time taken to sort arrays of varying sizes greater than 5000. Additionally, it provides sample output demonstrating the time taken for sorting different numbers of elements.

Uploaded by

Mr. LION
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/ 5

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.

PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to perform selection sort on an array
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i; // Assume the current element is the minimum
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j; // Update min_idx if a smaller element is found
}
}
// Swap the found minimum element with the current element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Function to generate an array of random numbers
void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}

int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user

if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}

// Allocate memory for the array


int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}

// Generate random numbers and store them in the array


generateRandomNumbers(arr, n);

// Measure the time taken to sort the array


clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();

// Calculate and print the time taken to sort the array


double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);

// Free the allocated memory


free(arr);
return 0;
}
OUTPUT
Enter number of elements: 6000
Time taken to sort 6000 elements: 0.031000 seconds

********************************************************************

Enter number of elements: 7000


Time taken to sort 7000 elements: 0.034000 seconds

********************************************************************

Enter number of elements: 8000


Time taken to sort 8000 elements: 0.047000 seconds

********************************************************************
Enter number of elements: 9000
Time taken to sort 9000 elements: 0.052000 seconds

********************************************************************

Enter number of elements: 10000

Time taken to sort 10000 elements: 0.077000 seconds

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>

// Function to swap two elements


void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

// Partition function for Quick Sort


int partition(int arr[], int low, int high)
{
int pivot = arr[high]; // Pivot element
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++)


{
if (arr[j] < pivot)
{
i++; // Increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

// Quick Sort function


void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);

// Recursively sort elements before and after partition


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Function to generate random numbers


void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100000; // Generate random numbers between 0 and 99999
}
}

int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user

if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}

// Allocate memory for the array


int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}

// Generate random numbers and store them in the array


generateRandomNumbers(arr, n);

// Measure the time taken to sort the array


clock_t start = clock();
quickSort(arr, 0, n - 1);
clock_t end = clock();

// Calculate and print the time taken to sort the array


double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);

// Free the allocated memory


free(arr);
return 0;
}
OUTPUT:
Enter number of elements: 10000
Time taken to sort 10000 elements: 0.0000 seconds

****************************************************************
****

Enter number of elements: 20000


Time taken to sort 20000 elements: 0.015000 seconds

****************************************************************
****

Enter number of elements: 30000


Time taken to sort 30000 elements: 0.011000 seconds

****************************************************************
****

Enter number of elements: 35000


Time taken to sort 35000 elements: 0.003000 seconds

****************************************************************
****

Enter number of elements: 50000


Time taken to sort 50000 elements: 0.015000 seconds

You might also like