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

Menu-Driven Sorting Program in C

Uploaded by

Priyal Aggarwal
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 views6 pages

Menu-Driven Sorting Program in C

Uploaded by

Priyal Aggarwal
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

Priyal Aggarwal AIDS B 35417711924

Experiment 3
AIM: Make a menu driven program for various sort.
Algorithm:
Priyal Aggarwal AIDS B 35417711924
Priyal Aggarwal AIDS B 35417711924

Source Code:
#include <stdio.h>
#include <stdlib.h>

#define SIZE 8 // fixed size

// Function to print array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d\t", arr[i]);
printf("\n");
}

// Insertion Sort
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int t = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > t) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = t;
}
}

// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min])
min = j;
}
if (min != i) {
int t = arr[min];
arr[min] = arr[i];
arr[i] = t;
}
}
}

// Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
Priyal Aggarwal AIDS B 35417711924

}
}

// Merge Sort functions


void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];

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


leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j])
arr[k++] = leftArr[i++];
else
arr[k++] = rightArr[j++];
}
while (i < n1)
arr[k++] = leftArr[i++];
while (j < n2)
arr[k++] = rightArr[j++];
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

int main() {
int choice, arr[SIZE];

printf("Enter %d elements:\n", SIZE);


for (int i = 0; i < SIZE; i++)
scanf("%d", &arr[i]);

while (1) {
printf("\n--- Sorting Menu ---\n");
printf("1. Insertion Sort\n");
printf("2. Selection Sort\n");
printf("3. Bubble Sort\n");
printf("4. Merge Sort\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
Priyal Aggarwal AIDS B 35417711924

int temp[SIZE];
for (int i = 0; i < SIZE; i++) temp[i] = arr[i]; // Copy original array

switch (choice) {
case 1:
insertionSort(temp, SIZE);
printf("Array after Insertion Sort:\n");
printArray(temp, SIZE);
break;
case 2:
selectionSort(temp, SIZE);
printf("Array after Selection Sort:\n");
printArray(temp, SIZE);
break;
case 3:
bubbleSort(temp, SIZE);
printf("Array after Bubble Sort:\n");
printArray(temp, SIZE);
break;
case 4:
mergeSort(temp, 0, SIZE - 1);
printf("Array after Merge Sort:\n");
printArray(temp, SIZE);
break;
case 5:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}

return 0;
}

Output
Priyal Aggarwal AIDS B 35417711924

Learning Outcomes:

You might also like