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

3.quick Sort Ansd Merge Sort

The document contains code for a menu-driven program that allows the user to sort arrays using either quicksort or merge sort. It implements functions for quicksort, merge sort, and partitioning arrays. The main function prompts the user to enter the size of an array, populate it with values, select a sorting algorithm, prints the sorted array, and allows continuing the process or quitting.

Uploaded by

Shruti Ninawe
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)
23 views6 pages

3.quick Sort Ansd Merge Sort

The document contains code for a menu-driven program that allows the user to sort arrays using either quicksort or merge sort. It implements functions for quicksort, merge sort, and partitioning arrays. The main function prompts the user to enter the size of an array, populate it with values, select a sorting algorithm, prints the sorted array, and allows continuing the process or quitting.

Uploaded by

Shruti Ninawe
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/ 6

To perform menu driven code for searching technique:-

 Quick sort
 Merge sort

Code:-
#include <stdio.h>

#include <stdlib.h>

void quickSort(int[], int, int);

void mergeSort(int[], int, int);

int main() {

int n, arr[100];

int i;

int ch, cont;

do {

printf("Enter size of array: ");

scanf("%d", &n);

printf("Enter values of array:\n");

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

scanf("%d", &arr[i]);

printf("Enter sorting algorithm to use:\n1: Quick sort\n2: Merge sort\n");

scanf("%d", &ch);

switch(ch) {

case 1:

quickSort(arr, 0, n - 1);

break;

case 2:
mergeSort(arr, 0, n - 1);

break;

default:

printf("Invalid option!");

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

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

printf("\nContinue? 1/0:\t");

scanf("%d", &cont);

} while(cont == 1);

return 0;

int partition(int arr[], int offset, int size) {

int x = arr[size];

int i = offset - 1;

int j;

int temp;

for(j = offset; j < size; j++) {

if (arr[j] <= x) {

i++;

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

temp = arr[i + 1];


arr[i + 1] = arr[size];

arr[size] = temp;

return i + 1;

void quickSort(int arr[], int offset, int size) {

int pivot;

if (offset < size) {

pivot = partition(arr, offset, size);

quickSort(arr, offset, pivot - 1);

quickSort(arr, pivot + 1, size);

void merge(int arr[], int offset, int mid, int size) {

int n1 = mid - offset + 1;

int n2 = size - mid;

int* l = (int*)calloc(n1 + 1, sizeof(int));

int* r = (int*)calloc(n2 + 1, sizeof(int));

int i, j, k;

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

*(l + i) = arr[offset + i];

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

*(r + j) = arr[mid + j + 1];

*(l + n1) = 32767;

*(r + n2) = 32767;

i = j = 0;

for(k = offset; k <= size; k++) {

if(*(l + i) <= *(r + j)) {


arr[k] = *(l + i);

i++;

else {

arr[k] = *(r + j);

j++;

free(l);

free(r);

void mergeSort(int arr[], int offset, int size) {

if (offset < size) {

int mid = (offset + size) / 2;

mergeSort(arr, offset, mid);

mergeSort(arr, mid + 1, size);

merge(arr, offset, mid, size);

}
Output:-

Enter size of array: 5

Enter values of array:

23

12

67

45

Enter sorting algorithm to use:

1: Quick sort

2: Merge sort

8 12 23 45 67

Continue? 1/0: 1

Enter size of array: 4

Enter values of array:

67

22

Enter sorting algorithm to use:

1: Quick sort

2: Merge sort

2
1 4 22 67

Continue? 1/0: 0

You might also like