From 8a8e648114923fb2ef929150c9458b5f5f580c9e Mon Sep 17 00:00:00 2001 From: Devrajsinh Jhala Date: Sun, 10 Oct 2021 10:29:44 +0530 Subject: [PATCH] Sorting Algorithms Added these Sorting Algos in DSA folder --- .../Bubble_Sort.cpp | 47 +++++++++++++ .../Count_Sort.cpp | 61 +++++++++++++++++ .../DNF_Sort.cpp | 66 +++++++++++++++++++ .../Wave_Sort.cpp | 50 ++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 Data-Structures-And-Algorithms-Hacktoberfest18-master/Bubble_Sort.cpp create mode 100644 Data-Structures-And-Algorithms-Hacktoberfest18-master/Count_Sort.cpp create mode 100644 Data-Structures-And-Algorithms-Hacktoberfest18-master/DNF_Sort.cpp create mode 100644 Data-Structures-And-Algorithms-Hacktoberfest18-master/Wave_Sort.cpp diff --git a/Data-Structures-And-Algorithms-Hacktoberfest18-master/Bubble_Sort.cpp b/Data-Structures-And-Algorithms-Hacktoberfest18-master/Bubble_Sort.cpp new file mode 100644 index 000000000..08ded31fa --- /dev/null +++ b/Data-Structures-And-Algorithms-Hacktoberfest18-master/Bubble_Sort.cpp @@ -0,0 +1,47 @@ +// Bubble Sort == Reapetedly swap two adjacent elements if they are in wrong order +// For n elements in array == (n-1) iterations to get sorted array +// If ith iteration == Check upto (n-i) +#include +using namespace std; + +int main() +{ + int n; + cin >> n; + + int arr[n]; + + + for(int i=0;i> arr[i]; + } + + int counter = 1; + while(counter < n-1) + { + for(int i=0;iarr[i+1]) + { + // Swapping the numbers + int temp = arr[i]; + arr[i] = arr[i+1]; + arr[i+1] = temp; + } + } + counter++; + } + + for(int i=0;i +using namespace std; + +void countSort(int arr[], int n) +{ + + int k = arr[0]; + for(int i=0;i=0;i++) + { + output[--count[arr[i]]] = arr[i]; + } + + for(int i=0;i +using namespace std; + +void swap(int arr[], int i, int j) +{ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + + +void DNF_Sort(int arr[], int n) +{ + int low = 0; + int mid = 0; + int high = n - 1; + + while(mid <= high) + { + if(arr[mid] == 0) + { + swap(arr,low,mid); + low++; mid++; + } + + else if(arr[mid] == 1) + { + mid++; + } + + else + { + swap(arr,mid,high); + high--; + } + } +} + + + +int main() +{ + + int arr[] = {1,0,2,1,0,1,2,1,2}; + DNF_Sort(arr,9); + + for(int i=0;i<9;i++) + { + cout << arr[i] << " "; + } + + return 0; +} \ No newline at end of file diff --git a/Data-Structures-And-Algorithms-Hacktoberfest18-master/Wave_Sort.cpp b/Data-Structures-And-Algorithms-Hacktoberfest18-master/Wave_Sort.cpp new file mode 100644 index 000000000..343be50b1 --- /dev/null +++ b/Data-Structures-And-Algorithms-Hacktoberfest18-master/Wave_Sort.cpp @@ -0,0 +1,50 @@ +// Name: JHALA DEVRAJSINH SHRIPALSINH +// Date: 01/07/2021 +// Purpose: Wave Sort +// Wave form like arr[0]>= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ... + +// Time Complexity: +// O(N/2) === O(N) +// Final time complexity === O(N) + +#include +using namespace std; + +void swap(int arr[], int i, int j) +{ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +void wavesort(int arr[], int n) +{ + for(int i=1; i arr[i-1]) + { + swap(arr,i,i-1); + } + + if(arr[i] > arr[i+1] && i <= n-2) + { + swap(arr, i,i+1); + } + } +} + + + +int main() +{ + + int arr[] = {1,3,4,7,5,6,2}; + wavesort(arr,7); + + for(int i=0;i<7;i++) + { + cout << arr[i] << " "; + } + + return 0; +} \ No newline at end of file