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

quicksort and merge sort

The document outlines a C program that implements Quick Sort and Merge Sort algorithms, measuring their execution time for best, worst, and average case scenarios. It includes functions for sorting, filling arrays with specific cases, and measuring time using the clock() function. The program is designed to handle various input sizes defined in an array and outputs the time taken for each sorting method and case type.

Uploaded by

Sekhar Muthangi
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)
12 views

quicksort and merge sort

The document outlines a C program that implements Quick Sort and Merge Sort algorithms, measuring their execution time for best, worst, and average case scenarios. It includes functions for sorting, filling arrays with specific cases, and measuring time using the clock() function. The program is designed to handle various input sizes defined in an array and outputs the time taken for each sorting method and case type.

Uploaded by

Sekhar Muthangi
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

Aim : write c program executable in terminal for quick sort and merge sort and observe the execution

time for various input sizes-average ,worst and best cases

Explanation of the Program:

1. Quick Sort and Merge Sort: The program defines both Quick Sort and Merge Sort algorithms.

2. Input Cases:

o Best case: Sorted array (caseType = 'b').

o Worst case: Reverse sorted array (caseType = 'w').

o Average case: Random array (caseType = 'a').

3. Time Measurement: The clock() function measures the time taken by each sorting algorithm
for the given input size and case.

4. Sizes: The array sizes are defined in the sizes[] array. You can modify these as needed.

Program:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Swap function for Quick Sort

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];

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);

// Quick Sort algorithm

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);

// Merge function for Merge Sort

void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;
}

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Merge Sort algorithm

void mergeSort(int arr[], int l, int r) {

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

// Fill the array with best, worst, or average case

void fillArray(int arr[], int size, char caseType) {

if (caseType == 'b') {

for (int i = 0; i < size; i++)

arr[i] = i + 1; // Best case: already sorted array

} else if (caseType == 'w') {

for (int i = 0; i < size; i++)


arr[i] = size - i; // Worst case: reverse sorted array

} else {

for (int i = 0; i < size; i++)

arr[i] = rand() % 1000; // Average case: random array

// Measure the sorting time

void measureSortTime(int arr[], int size, void (*sortFunc)(int[], int, int), char* sortName, char
caseType) {

clock_t start, end;

double cpu_time_used;

fillArray(arr, size, caseType);

start = clock();

sortFunc(arr, 0, size - 1);

end = clock();

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

printf("%s Sort (Size %d, Case '%c'): %f seconds\n", sortName, size, caseType, cpu_time_used);

int main() {

srand(time(0));

int sizes[] = {1000, 5000, 10000, 50000, 100000}; // Different array sizes

int num_sizes = sizeof(sizes) / sizeof(sizes[0]);

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

int size = sizes[i];

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

printf("Quick Sort Results:\n");

measureSortTime(arr, size, quickSort, "Quick", 'b'); // Best case

measureSortTime(arr, size, quickSort, "Quick", 'w'); // Worst case


measureSortTime(arr, size, quickSort, "Quick", 'a'); // Average case

printf("\nMerge Sort Results:\n");

measureSortTime(arr, size, mergeSort, "Merge", 'b'); // Best case

measureSortTime(arr, size, mergeSort, "Merge", 'w'); // Worst case

measureSortTime(arr, size, mergeSort, "Merge", 'a'); // Average case

free(arr);

printf("\n");

return 0;

You might also like