0% found this document useful (0 votes)
9 views4 pages

Buubble Sort

The document contains a C program that implements two sorting algorithms: Bubble Sort and Selection Sort. It provides a menu-driven interface for the user to choose a sorting method, input the number of elements, and enter the elements to be sorted. The program continues to prompt the user for additional sorting operations until they choose to exit.

Uploaded by

omphutane2507
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)
9 views4 pages

Buubble Sort

The document contains a C program that implements two sorting algorithms: Bubble Sort and Selection Sort. It provides a menu-driven interface for the user to choose a sorting method, input the number of elements, and enter the elements to be sorted. The program continues to prompt the user for additional sorting operations until they choose to exit.

Uploaded by

omphutane2507
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

#include <stdio.

h>

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

int i, j, temp;

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

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

if (arr[j] > arr[j + 1]) {

// Swap

temp = arr[j];

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

arr[j + 1] = temp;

printf("Array sorted using Bubble Sort:\n");

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

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

printf("\n");

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

int i, j, min, temp;

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

min = i;

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

if (arr[j] < arr[min])

min = j;

if (min != i) {

// Swap
temp = arr[i];

arr[i] = arr[min];

arr[min] = temp;

printf("Array sorted using Selection Sort:\n");

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

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

printf("\n");

int main() {

int arr[50], n, choice, op;

do {

printf("\n--- MENU ---\n");

printf("1. Bubble Sort\n");

printf("2. Selection Sort\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1 || choice == 2) {

printf("Enter number of elements: ");

scanf("%d", &n);

if (n > 50) {

printf("Max size is 50. Setting n = 50.\n");

n = 50;
}

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

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

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

if (choice == 1)

bubbleSort(arr, n);

else

selectionSort(arr, n);

} else if (choice == 3) {

printf("Exiting program.\n");

break;

} else {

printf("Invalid choice.\n");

if (choice != 3) {

printf("Do you want to continue? (1: Yes, 0: No): ");

scanf("%d", &op);

if (op == 0)

break;

} while (1);

return 0;

}
--- MENU ---

1. Bubble Sort

2. Selection Sort

3. Exit

Enter your choice: 1

Enter number of elements: 5

Enter 5 elements: 9 2 5 1 4

Array sorted using Bubble Sort:

12459

Do you want to continue? (1: Yes, 0: No): 1

--- MENU ---

1. Bubble Sort

2. Selection Sort

3. Exit

Enter your choice: 2

Enter number of elements: 4

Enter 4 elements: 8 6 3 1

Array sorted using Selection Sort:

1368
Do you want to continue? (1: Yes, 0: No): 0

You might also like