You are on page 1of 3

#include <iostream>

using namespace std;

// Function to perform bubble sort


void bubbleSort(int nums[], int len) {
bool isSwapped;
for (int i = 0; i < len; i++) {
isSwapped = false;
for (int j = 1; j < len - i; j++) {
if (nums[j] < nums[j - 1]) {
swap(nums[j], nums[j - 1]);
isSwapped = true;
}
}
if (!isSwapped) {
break;
}
}
}

// Function to perform quicksort


void quickSort(int nums[], int low, int high) {
if (low >= high) {
return;
}
int start = low, end = high;
int mid = start + ((end - start) / 2);
int pivot = nums[mid];
while (start <= end) {
while (nums[start] < pivot) {
start++;
}
while (nums[end] > pivot) {
end--;
}
if (start <= end) {
swap(nums[start], nums[end]);
start++;
end--;
}
}
quickSort(nums, low, end);
quickSort(nums, start, high);
}

// Function to perform selection sort


void selectionSort(int nums[], int len) {
for (int i = 0; i < len - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < len; j++) {
if (nums[j] < nums[min_idx]) {
min_idx = j;
}
}
swap(nums[i], nums[min_idx]);
}
}
// Function to merge two sorted arrays
void merge(int nums[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];


for (int i = 0; i < n1; i++) {
L[i] = nums[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = nums[mid + 1 + j];
}

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
nums[k] = L[i];
i++;
} else {
nums[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


nums[k] = L[i];
i++;
k++;
}

while (j < n2) {


nums[k] = R[j];
j++;
k++;
}
}

// Function to perform merge sort


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

int main() {
int nums[] = {1, 12, 6, 8, 10};
int size_nums = sizeof(nums) / sizeof(nums[0]);

cout << "Before sorting, the array is:\n";


for (int i = 0; i < size_nums; i++) {
cout << nums[i] << " ";
}
cout << "\n\n";

cout << "Choose a sorting method:\n";


cout << "1. Bubble Sort\n";
cout << "2. Quick Sort\n";
cout << "3. Selection Sort\n";
cout << "4. Merge Sort\n";
int choice;
cin >> choice;

if (choice == 1) {
bubbleSort(nums, size_nums);
} else if (choice == 2) {
quickSort(nums, 0, size_nums - 1);
} else if (choice == 3) {
selectionSort(nums, size_nums);
} else if (choice == 4) {
mergeSort(nums, 0, size_nums - 1);
} else {
cout << "Invalid choice. Exiting.\n";
return 1;
}

cout << "After sorting, the array is:\n";


for (int i = 0; i < size_nums; i++) {
cout << nums[i] << " ";
}
cout << "\n";

// Search for an element


int search_element;
cout << "Enter an element to search: ";
cin >> search_element;

bool found = false;


for (int i = 0; i < size_nums; i++) {
if (nums[i] == search_element) {
cout << "Element " << search_element << " found at index " << i << "\n";
found = true;
break;
}
}

if (!found) {
cout << "Element " << search_element << " not found.\n";
}

return 0;
}

You might also like