0% found this document useful (0 votes)
33 views6 pages

Quicksort, Mergesort, Selection Sort Program For N 5000

Uploaded by

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

Quicksort, Mergesort, Selection Sort Program For N 5000

Uploaded by

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

Quicksort program for n>5000.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Function to swap two elements

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

// Function to partition the array and return the pivot index

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = (low - 1);

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

if (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

// Function to perform Quick Sort

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);


quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

// Function to generate random numbers between 0 and 999

int generateRandomNumber() {

return rand() % 6000;

int main() {

// Set n value

int n = 6000;

// Allocate memory for the array

int* arr = (int*)malloc(n * sizeof(int));

// Generate random elements for the array

srand(time(NULL));

printf("Random numbers for n = %d:\n", n);

for (int i = 0; i < n; i++) {

arr[i] = generateRandomNumber();

printf("%d ", arr[i]);

printf("\n");

// Record the start time

clock_t start = clock();

// Perform quick sort

quickSort(arr, 0, n - 1);
// Record the end time

clock_t end = clock();

// Calculate the time taken for sorting

double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

// Output the time taken to sort for the current value of n

printf("\nTime taken to sort for n = %d: %lf seconds\n\n", n, time_taken);

// Display sorted numbers

printf("Sorted numbers for n = %d:\n", n);

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n\n");

// Free the dynamically allocated memory

free(arr);

return 0;

}
Program for selection sort using random number

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Function to perform selection sort

void selectionSort(int arr[], int n) {

int i, j, minIndex, temp;

for (i = 0; i < n - 1; i++) {

minIndex = i;

for (j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element

temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

// Function to generate random numbers between 0 and 999

int generateRandomNumber() {

return rand() % 5001;

int main() {

// Set n value

int n = 5001;

// Allocate memory for the array

int* arr = (int*)malloc(n * sizeof(int));


// Generate random elements for the array

srand(time(NULL));

printf("Random numbers for n = %d:\n", n);

for (int i = 0; i < n; i++) {

arr[i] = generateRandomNumber();

printf("%d ", arr[i]);

printf("\n");

// Record the start time

clock_t start = clock();

// Perform selection sort

selectionSort(arr, n);

// Record the end time

clock_t end = clock();

// Calculate the time taken for sorting

double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

// Output the time taken to sort for the current value of n

printf("\nTime taken to sort for n = %d: %lf seconds\n\n", n, time_taken);

// Display sorted numbers

printf("Sorted numbers for n = %d:\n", n);

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n\n");

// Free the dynamically allocated memory

free(arr);

return 0;

You might also like