From f87c3a65487c9b21af5a7e522f18c59d69eaf1bd Mon Sep 17 00:00:00 2001 From: Aswin Sampath Date: Sat, 6 Mar 2021 21:51:36 +0530 Subject: [PATCH 1/2] Counting Sort --- src/.vscode/c_cpp_properties.json | 21 +++++ src/counting_sort_demo.cpp | 135 ++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/.vscode/c_cpp_properties.json create mode 100644 src/counting_sort_demo.cpp diff --git a/src/.vscode/c_cpp_properties.json b/src/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..2ffb82d0 --- /dev/null +++ b/src/.vscode/c_cpp_properties.json @@ -0,0 +1,21 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "windowsSdkVersion": "10.0.17763.0", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "${default}", + "compilerPath": "D:/MinGW/bin/g++.exe" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/counting_sort_demo.cpp b/src/counting_sort_demo.cpp new file mode 100644 index 00000000..508675c3 --- /dev/null +++ b/src/counting_sort_demo.cpp @@ -0,0 +1,135 @@ +/* +Author : Aswin Sampath +Topic : Counting Sort + + Counting Sort + When to use? + Vizualise the array/vector elements + Amount of space needed by the algorithm = Range of the vector + Range of vector = Max(vector)- Min(vector) + If Range is of the order 10^5 then we can use counting sort subjet to the compiler + + How to Implement + Sort the numbers according to their frequency/ number of occurences + Place the element in their respective position according to the ranking + + Time Complexity : O(n) + Space COmplexity : O(Max(arr)-Min(arr)); +*/ + + +#include +#include +using namespace std; + +void printVector(vectorv){ + cout<<"\n Vector elements \n "; + for(int element:v){ + cout<arr){ + int minele = arr[0]; + for(int ele:arr){ + if(ele<=minele)minele=ele; + } + return minele; +} + +int findMax(vectorarr){ + int maxele = arr[0]; + for(int ele:arr){ + if(ele>=maxele)maxele=ele; + } + return maxele; +} + + +int findRange(vectorarr){ + int maxele = findMax(arr); + int minele = findMin(arr); + return maxele - minele; +} + +vectorfreq; +vectoroutput; + +void countingSort(vector &arr,int range){ + + // cout<<"Size of arr = "<arr = {2,3,7,4,6,8,9,2,5,4,8,3,1,6,7,2,5,7,6,1,4,2,3,9,8,7,4,5,6,0}; + + /* Frequency Table + ele freq + 0 - 1 + 1 - 2 + 2 - 4 + 3 - 3 + 4 - 4 + 5 - 3 + 6 - 4 + 7 - 4 + 8 - 3 + 9 - 2 + */ + + // Vector Range = max(arr)-min(arr) = 9 - 0 = 9 Hence we require of a 9 space in frequency array + int range = findRange(arr); + cout<<"Before Sorting \n"; + printVector(arr); + countingSort(arr,range); // Sorting algorithm + cout<<"After Sorting \n"; + printVector(arr); +} + From 7d6ce0e4f055c43462fde1d0d2fa0622ec023300 Mon Sep 17 00:00:00 2001 From: Aswin Sampath Date: Sat, 6 Mar 2021 21:54:49 +0530 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=91=8D=20Counting=20sort=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/.vscode/c_cpp_properties.json | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/.vscode/c_cpp_properties.json diff --git a/src/.vscode/c_cpp_properties.json b/src/.vscode/c_cpp_properties.json deleted file mode 100644 index 2ffb82d0..00000000 --- a/src/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "windowsSdkVersion": "10.0.17763.0", - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "${default}", - "compilerPath": "D:/MinGW/bin/g++.exe" - } - ], - "version": 4 -} \ No newline at end of file