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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy