Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
454ba8b
Edited README
ShivamDubey7 Oct 17, 2020
806e8d5
Renamed
ShivamDubey7 Oct 17, 2020
a62d64e
Renamed parent folder
ShivamDubey7 Oct 17, 2020
c898e32
Added Kmp
ShivamDubey7 Oct 17, 2020
8551799
Merge pull request #1 from ShivamDubey7/string_algorithms
ShivamDubey7 Oct 17, 2020
cede9b6
Added Linear String Search
ShivamDubey7 Oct 17, 2020
c9eddcf
Merge pull request #2 from ShivamDubey7/string_algorithms
ShivamDubey7 Oct 17, 2020
1ece432
Added Fast exponentiation
ShivamDubey7 Oct 17, 2020
0ad9ae4
Merge pull request #3 from ShivamDubey7/maths
ShivamDubey7 Oct 17, 2020
eb43257
Adding Fast Exponentiation: both recursive and iterative method
ShivamDubey7 Oct 17, 2020
8e1d9c5
Merge pull request #4 from ShivamDubey7/maths
ShivamDubey7 Oct 17, 2020
601ecae
Added efficient MergeSort
ShivamDubey7 Oct 17, 2020
db964f8
Merge pull request #5 from the-geeky-wizard/sorting
ShivamDubey7 Oct 17, 2020
ab81275
Added LinearSort
ShivamDubey7 Oct 17, 2020
324fb8a
Merge pull request #6 from the-geeky-wizard/sorting
ShivamDubey7 Oct 17, 2020
4412459
Added HeapSort
ShivamDubey7 Oct 17, 2020
9574e42
Merge pull request #7 from the-geeky-wizard/sorting
ShivamDubey7 Oct 17, 2020
371e6d7
Merge pull request #1 from the-geeky-wizard/sorting
the-geeky-wizard Oct 17, 2020
5929fa4
Added BinarySeach
ShivamDubey7 Oct 17, 2020
5a779e9
Merge pull request #2 from the-geeky-wizard/search_algorithms
the-geeky-wizard Oct 17, 2020
056ede3
Merge pull request #8 from the-geeky-wizard/master
ShivamDubey7 Oct 17, 2020
e53f68e
Create bubblesort_java
huntingcodes Oct 18, 2020
392deed
Create mirror inverse_java
huntingcodes Oct 18, 2020
990b851
Create ascii-char_cpp
huntingcodes Oct 18, 2020
f3708d6
Merge pull request #1 from huntingcodes/huntingcodes-patch-1
huntingcodes Oct 18, 2020
55ffc5f
Merge pull request #9 from huntingcodes/master
ShivamDubey7 Oct 18, 2020
1769869
Added Recursive BinarySeach
ShivamDubey7 Oct 18, 2020
e1e274a
Merge pull request #10 from dubey7/master
ShivamDubey7 Oct 18, 2020
3791d65
Renamed
ShivamDubey7 Oct 18, 2020
d63881b
Removed
ShivamDubey7 Oct 18, 2020
71c7c04
Merge pull request #11 from dubey7/master
ShivamDubey7 Oct 18, 2020
f1a9b4f
Added Iterative exponentiation
ShivamDubey7 Oct 18, 2020
446692b
Merge pull request #12 from dubey7/master
ShivamDubey7 Oct 18, 2020
8dceaf5
Added DFS
ShivamDubey7 Oct 18, 2020
f5f82a4
Merge pull request #13 from dubey7/master
ShivamDubey7 Oct 18, 2020
f32b8f5
Create Trie.java
nirbhaykumar780890 Oct 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
27 changes: 27 additions & 0 deletions Graph/DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int LIM = 50;
int vis[LIM];
vector<vector<int> >AdjList(LIM);
void dfs(int u){
cout<<u<<" , ";
vis[u]=1;
for(auto v:AdjList[u]){
if(vis[v]==0){
dfs(v);
}
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
AdjList[u].pb(v);
AdjList[v].pb(u);
}
dfs(1);

}
18 changes: 18 additions & 0 deletions Maths/EulerGCD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<bits/stdc++.h>
using namespace std;
int iterativeGcd(int a,int b){
while(a!=0){
int rem = b%a;
b = a;
a = rem;
}
return b;
}
int recursiveGcd(int a, int b){
if(a==0)return b;
return recursiveGcd(b%a,a);
}
int main(){
int a = 6,b=4;
cout<<recursiveGcd(a,b);
}
30 changes: 30 additions & 0 deletions Maths/FastExponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll fastExpo(ll base, ll power){
if(power==0)
return 1;
ll temp = fastExpo(base,power/2);
if(power&1){
return temp*temp * base;
}
return temp*temp;
}
ll fastExpoIterative(ll base, ll power){
ll res = 1;
while(power >0){
if(power&1){
res*=base;
}
power/=2;
base*=base;
}
return res;

}
int main(){
ll base = 5;
ll power = 3;
cout<<fastExpoIterative(base,power);

}
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Competitive-Programming-Algos
C++ implementation of algorithms learned and used while competitive programming
# Competitive Programming Runbook
During the years I spent playing around with competetive programming, here's an archive I prepared for the most useful reusable content I have been through.
I'm in the phase of collecting all my codes, making them generic enough to be understood/reused, and put it here.
Feel free to contribute, but make sure to maintain the quality -
<ul>
<li>Don't add a problem's solution here, as that's not reusable.</li>
<li>Let's have generic snippets one can refer for learning algo/implementation</li>
</ul>
36 changes: 36 additions & 0 deletions Searching/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
using namespace std;
//Create a predicate to return T/F for an ind
bool predicate(vector<int>&v, int ind, int search_token){
return v[ind] >= search_token;
}
int binarySeach(vector<int>&v, int search_token){
int low = 0,high = v.size(),mid;
while(low<high){
mid = (low+high)/2;
if(predicate(v,mid,search_token)){
high = mid;
}
else
low = mid+1;
}
if(v[low]==search_token) return low;
return -1;
}
int recursiveBinarySearch(vector<int>&v, int search_token, int begin, int end){
if(begin==end){
if(v[begin]==search_token)
return begin;
return -1;
}
int mid = (begin+end)/2;
if(predicate(v,mid,search_token)){
return recursiveBinarySearch(v,search_token,begin,mid);
}
return recursiveBinarySearch(v,search_token,mid+1,end);
}
int main(){
vector<int> v = {1,2,4,5,11,20,25};
cout<<recursiveBinarySearch(v,6,0,6);
return 0;
}
34 changes: 34 additions & 0 deletions Sorting/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}
40 changes: 40 additions & 0 deletions Sorting/HeapSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
#define sz(v) ((int)v.size())
using namespace std;
void heapify(vector<int>&v, int SIZE, int ind){
int left_child = (ind<<1)+1;
int right_child = left_child+1;
int to_swap;
if(left_child<SIZE && v[left_child] > v[ind])
to_swap = left_child;
else
to_swap = ind;
if(right_child<SIZE && v[right_child] > v[to_swap])
to_swap = right_child;
if(to_swap !=ind){
int temp = v[to_swap];
v[to_swap] = v[ind];
v[ind] = temp;
heapify(v,SIZE,to_swap);
}
}
void heapSort(vector<int>&v){
vector<int>heap(sz(v));
for(int i=(sz(v)-2)/2;i>=0;i--){
heapify(v,sz(v),i);
}
for(int i=sz(v)-1;i>0;i--){
int temp = v[0];
v[0] = v[i];
v[i] = temp;

heapify(v,i,0);
}
}
int main(){
vector<int>temp = {1,3,2,12,444,5};
heapSort(temp);
for(int v:temp){
cout<<v<<" ";
}
}
21 changes: 21 additions & 0 deletions Sorting/LinearSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<bits/stdc++.h>
using namespace std;
void linearSort(vector<int>&v){
for(int i=0;i<v.size();i++){
for(int j=i+1;j<v.size();j++){
if(v[j]<v[i]){
int temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}

}
}
int main(){
vector<int>temp = {1,3,2,12,444,5};
linearSort(temp);
for(int v:temp){
cout<<v<<" ";
}
}
33 changes: 33 additions & 0 deletions Sorting/MergeSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;
void merge(vector<int>&v, int startIndex, int mid, int endIndex){
vector<int>temp;
int i1 = startIndex;
int i2 = mid+1;
while(i1<=mid && i2<=endIndex){
if(v[i1]<=v[i2])
temp.push_back(v[i1]),i1++;
else
temp.push_back(v[i2]),i2++;
}
while(i1<=mid)temp.push_back(v[i1]),i1++;
while(i2<=endIndex)temp.push_back(v[i2]),i2++;
for(int i=0;i<temp.size();i++)
v[startIndex+i] = temp[i];
}
void mergeSort(vector<int>&v, int startIndex, int endIndex){
if(startIndex<endIndex){
int mid = (startIndex+endIndex)/2;
mergeSort(v,startIndex,mid);
mergeSort(v,mid+1,endIndex);
merge(v,startIndex,mid,endIndex);
}

}
int main(){
vector<int>temp = {1,3,2,12,444,5};
mergeSort(temp,0, temp.size()-1);
for(int v:temp){
cout<<v<<" ";
}
}
36 changes: 36 additions & 0 deletions StringSearching/Kmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
using namespace std;
void kmpPreProcess(string pattern, vector<int>&lps){
int i = 0,j=-1;
lps[0] = -1;
while(i<pattern.size()){
while(j>=0 && pattern[i]!=pattern[j])j = lps[j];
i++,j++;
lps[i]=j;
}
}
vector<int> kmpSearch(string text, string pattern){
vector<int> ans;
vector<int> lps(pattern.size()+1);
kmpPreProcess(pattern, lps);
int i = 0, j=-1;
while(i<text.size()){
while(j>=0 && pattern[j]!=text[i])j=lps[j];
i++;j++;
if(j==pattern.size()){
ans.push_back(i-pattern.size());
j = lps[j];
}
}
return ans;
}
int main(){

string text = "Hey there! this is a text string, within which we wanna search a pattern. \
Note that pattern repeats itself, so search for pattern if you wanna see can this report multiple occurrences of pattern.";
string pattern = "pattern";
vector<int> indices = kmpSearch(text,pattern);
for(auto i:indices){
cout<<i<<",";
}
}
27 changes: 27 additions & 0 deletions StringSearching/LinearSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
using namespace std;
vector<int> linearSearch(string text, string pattern){
vector<int>ans;
for(int i=0;i<text.size();i++){
bool found = true;
for(int j=0;j<pattern.size();j++){
if(pattern[j]!=text[i+j]){
found = false;
break;
}
}
if(found)
ans.push_back(i);
}
return ans;
}
int main(){

string text = "Hey there! this is a text string, within which we wanna search a pattern. \
Note that pattern repeats itself, so search for pattern if you wanna see can this report multiple occurrences of pattern.";
string pattern = "pattern";
vector<int> indices = linearSearch(text,pattern);
for(auto i:indices){
cout<<i<<",";
}
}
Loading