From 6996ab6c68f084ac385fcb8db2bed0c8cc95b37b Mon Sep 17 00:00:00 2001 From: RonakSurana-2001 Date: Wed, 27 Mar 2024 17:35:51 +0530 Subject: [PATCH] Added Top 5 Sorting Algorithms --- Sorting_Algorithms/Bubble_Sort.cpp | 46 ++++++++++++++++ Sorting_Algorithms/Insertion_Sort.cpp | 38 ++++++++++++++ Sorting_Algorithms/Merge_Sort.cpp | 76 +++++++++++++++++++++++++++ Sorting_Algorithms/Quick_Sort.cpp | 52 ++++++++++++++++++ Sorting_Algorithms/Selection_Sort.cpp | 44 ++++++++++++++++ 5 files changed, 256 insertions(+) create mode 100644 Sorting_Algorithms/Bubble_Sort.cpp create mode 100644 Sorting_Algorithms/Insertion_Sort.cpp create mode 100644 Sorting_Algorithms/Merge_Sort.cpp create mode 100644 Sorting_Algorithms/Quick_Sort.cpp create mode 100644 Sorting_Algorithms/Selection_Sort.cpp diff --git a/Sorting_Algorithms/Bubble_Sort.cpp b/Sorting_Algorithms/Bubble_Sort.cpp new file mode 100644 index 00000000..d32f7dba --- /dev/null +++ b/Sorting_Algorithms/Bubble_Sort.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; +void bubbleSort(int arr[], int n); +void printArray(int arr[], int n); +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + bubbleSort(arr, n); +} +void bubbleSort(int arr[], int n) +{ + int flag; + for (int i = 0; i < n; i++) + { + flag = 0; + for (int j = 0; j < n - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + arr[j] ^= arr[j + 1]; + arr[j + 1] ^= arr[j]; + arr[j] ^= arr[j + 1]; + flag = 1; + } + } + if (flag == 0) + { + break; + } + } + printArray(arr, n); +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/Sorting_Algorithms/Insertion_Sort.cpp b/Sorting_Algorithms/Insertion_Sort.cpp new file mode 100644 index 00000000..bc997699 --- /dev/null +++ b/Sorting_Algorithms/Insertion_Sort.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +void insertionSort(int arr[],int n); +void printArray(int arr[], int n); +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + insertionSort(arr, n); +} +void insertionSort(int arr[],int n) +{ + for(int i=1;i0 && arr[hole-1]>value) + { + arr[hole]=arr[hole-1]; + hole--; + } + arr[hole]=value; + } + printArray(arr, n); +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/Sorting_Algorithms/Merge_Sort.cpp b/Sorting_Algorithms/Merge_Sort.cpp new file mode 100644 index 00000000..cae50f39 --- /dev/null +++ b/Sorting_Algorithms/Merge_Sort.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; +void mergeSort(int arr[], int n); +void Merge(int left[], int right[], int arr[], int x, int y,int n); +void printArray(int arr[], int n); +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + mergeSort(arr, n); + printArray(arr,n); +} +void mergeSort(int arr[], int n) +{ + if (n < 2) + { + return; + } + int mid = n / 2; + int left[mid]; + int right[n - mid]; + for (int i = 0; i < mid; i++) + { + left[i] = arr[i]; + } + for (int i = mid; i < n; i++) + { + right[i - mid] = arr[i]; + } + mergeSort(left, mid); + mergeSort(right, n - mid); + Merge(left, right, arr, mid, n - mid,n); +} +void Merge(int left[], int right[], int arr[], int x, int y,int n) +{ + int i = 0, j = 0, k = 0; + while (i < x && j < y) + { + if (left[i] <= right[j]) + { + arr[k] = left[i]; + i++; + } + else + { + arr[k] = right[j]; + j++; + } + k++; + } + while (i < x) + { + arr[k] = left[i]; + i++; + k++; + } + while (j < y) + { + arr[k] = right[j]; + j++; + k++; + } +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} \ No newline at end of file diff --git a/Sorting_Algorithms/Quick_Sort.cpp b/Sorting_Algorithms/Quick_Sort.cpp new file mode 100644 index 00000000..bccf570e --- /dev/null +++ b/Sorting_Algorithms/Quick_Sort.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +void swap(int *a, int *b) +{ + int temp = *a; + *a = *b; + *b = temp; +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} +int partition(int arr[], int start, int end) +{ + int pivot = arr[end]; + int pIndex = start - 1; + for (int i = start; i <= end - 1; i++) + { + if (arr[i] < pivot) + { + pIndex++; + swap(&arr[pIndex], &arr[i]); + } + } + swap(&arr[pIndex + 1], &arr[end]); + return pIndex + 1; +} +void quickSort(int arr[], int start, int end) +{ + if (start < end) + { + int pIndex = partition(arr, start, end); + quickSort(arr, start, pIndex - 1); + quickSort(arr, pIndex + 1, end); + } +} +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + quickSort(arr, 0, n - 1); + printArray(arr, n); +} \ No newline at end of file diff --git a/Sorting_Algorithms/Selection_Sort.cpp b/Sorting_Algorithms/Selection_Sort.cpp new file mode 100644 index 00000000..d53afb43 --- /dev/null +++ b/Sorting_Algorithms/Selection_Sort.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +void selectionSort(int arr[], int n); +void printArray(int arr[], int n); +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + selectionSort(arr, n); +} +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[i] && arr[j] <= arr[min]) + { + min = j; + } + } + if (min != i) + { + arr[i] ^= arr[min]; + arr[min] ^= arr[i]; + arr[i] ^= arr[min]; + } + } + printArray(arr, n); +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} \ No newline at end of file