From 9ad68d5bdd7f11093787bc9818c8a558db237232 Mon Sep 17 00:00:00 2001 From: AmanGupta03 Date: Fri, 20 Dec 2019 21:13:17 +0530 Subject: [PATCH 1/3] Added FenwickTree --- FenwickTree.cpp | 47 ++++++++++++++++ Sorting Algos/countsort.cpp | 103 ++++++++++++++++++++---------------- 2 files changed, 104 insertions(+), 46 deletions(-) create mode 100644 FenwickTree.cpp diff --git a/FenwickTree.cpp b/FenwickTree.cpp new file mode 100644 index 0000000..4e20138 --- /dev/null +++ b/FenwickTree.cpp @@ -0,0 +1,47 @@ +#include + +template +class FenwickTree { + private: + int n; + vector bit; + public: + FenwickTree(int n) { + this -> n = n; + bit = vector(n+1); + } + + void update(int x, T delta) { + for(; x <= n; x += x & -x) { + bit[x] += delta; + } + } + + T query(int x) { + T res = 0; + for(; x > 0; x -= x & -x) { + res += bit[x]; + } + return res; + } + + T query_range(int l, int r) { + return query(r) - query(l-1); + } +}; + +int main() { + // creating FenwickTree object for index from 1 to 10 and type integers; + FenwickTree ft(10); + + //updates + ft.update(3, 5); + ft.update(4, 1); + ft.update(7, 2); + + //query in range + std::cout << ft.query_range(3, 7) <<'\n'; + std::cout << ft.query_range(4, 6) <<'\n'; + + return 0; +} \ No newline at end of file diff --git a/Sorting Algos/countsort.cpp b/Sorting Algos/countsort.cpp index 7b1b66e..dd8474e 100644 --- a/Sorting Algos/countsort.cpp +++ b/Sorting Algos/countsort.cpp @@ -1,51 +1,62 @@ -#include -#include -#define n 200 - +#include using namespace std; +void countingSort(int *a,int n,int range) +{ + int *freq=new int[range]; + int *output=new int[n]; + memset(freq,0,sizeof(freq[0])*range); + /*cout<<"Range of elements entered "<=0;i--){ - arr[i]=arr[i-1]; - } - - int outputarr[num]; - for(i=0;i=0;i--){ + freq[a[i]]--; + output[freq[a[i]]]=a[i]; + } + for(int i=0;i>s>>e; + n=abs(e-s+1); + int *a=new int[n]; -int main(){ - cout<<"Input format : Number of elements of array\n The elements\n"; - int k,i=0; - scanf("%d",&k); - int a[k]; - for(i=0;i>a[i]; - } - count_sort(a,k); - for(i=0;i>a[i]; + } + //cout<<"No of elements entered "< Date: Fri, 20 Dec 2019 21:21:46 +0530 Subject: [PATCH 2/3] Update FenwickTree.cpp --- FenwickTree.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/FenwickTree.cpp b/FenwickTree.cpp index 4e20138..a035497 100644 --- a/FenwickTree.cpp +++ b/FenwickTree.cpp @@ -1,14 +1,15 @@ #include +#include template class FenwickTree { private: int n; - vector bit; + std::vector bit; public: FenwickTree(int n) { this -> n = n; - bit = vector(n+1); + bit = std::vector(n+1); } void update(int x, T delta) { @@ -44,4 +45,4 @@ int main() { std::cout << ft.query_range(4, 6) <<'\n'; return 0; -} \ No newline at end of file +} From 92eb98930931cc48829f38f46a62746af419c203 Mon Sep 17 00:00:00 2001 From: Aman Gupta <45457791+AmanGupta03@users.noreply.github.com> Date: Tue, 31 Dec 2019 20:52:37 +0530 Subject: [PATCH 3/3] Delete CountSort.cpp --- Sorting Algos/CountSort.cpp | 62 ------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 Sorting Algos/CountSort.cpp diff --git a/Sorting Algos/CountSort.cpp b/Sorting Algos/CountSort.cpp deleted file mode 100644 index 364f8bb..0000000 --- a/Sorting Algos/CountSort.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -using namespace std; -void countingSort(int *a,int n,int range) -{ - int *freq=new int[range]; - int *output=new int[n]; - memset(freq,0,sizeof(freq[0])*range); - /*cout<<"Range of elements entered "<=0;i--){ - freq[a[i]]--; - output[freq[a[i]]]=a[i]; - } - for(int i=0;i>s>>e; - n=abs(e-s+1); - int *a=new int[n]; - - for(int i=0;i>a[i]; - } - //cout<<"No of elements entered "<