Skip to content

Patch 2 #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
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
26d2237
Create AVL.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
154 changes: 154 additions & 0 deletions AVL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Java program for insertion in AVL Tree
class Node {
int key, height;
Node left, right;

Node(int d) {
key = d;
height = 1;
}
}

class AVLTree {

Node root;

// A utility function to get the height of the tree
int height(Node N) {
if (N == null)
return 0;

return N.height;
}

// A utility function to get maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
}

// A utility function to right rotate subtree rooted with y
// See the diagram given above.
Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;

// Perform rotation
x.right = y;
y.left = T2;

// Update heights
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;

// Return new root
return x;
}

// A utility function to left rotate subtree rooted with x
// See the diagram given above.
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;

// Perform rotation
y.left = x;
x.right = T2;

// Update heights
x.height = max(height(x.left), height(x.right)) + 1;
y.height = max(height(y.left), height(y.right)) + 1;

// Return new root
return y;
}

// Get Balance factor of node N
int getBalance(Node N) {
if (N == null)
return 0;

return height(N.left) - height(N.right);
}

Node insert(Node node, int key) {

/* 1. Perform the normal BST insertion */
if (node == null)
return (new Node(key));

if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else // Duplicate keys not allowed
return node;

/* 2. Update height of this ancestor node */
node.height = 1 + max(height(node.left),
height(node.right));

/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
int balance = getBalance(node);

// If this node becomes unbalanced, then there
// are 4 cases Left Left Case
if (balance > 1 && key < node.left.key)
return rightRotate(node);

// Right Right Case
if (balance < -1 && key > node.right.key)
return leftRotate(node);

// Left Right Case
if (balance > 1 && key > node.left.key) {
node.left = leftRotate(node.left);
return rightRotate(node);
}

// Right Left Case
if (balance < -1 && key < node.right.key) {
node.right = rightRotate(node.right);
return leftRotate(node);
}

/* return the (unchanged) node pointer */
return node;
}

// A utility function to print preorder traversal
// of the tree.
// The function also prints height of every node
void preOrder(Node node) {
if (node != null) {
System.out.print(node.key + " ");
preOrder(node.left);
preOrder(node.right);
}
}

public static void main(String[] args) {
AVLTree tree = new AVLTree();

/* Constructing tree given in the above figure */
tree.root = tree.insert(tree.root, 10);
tree.root = tree.insert(tree.root, 20);
tree.root = tree.insert(tree.root, 30);
tree.root = tree.insert(tree.root, 40);
tree.root = tree.insert(tree.root, 50);
tree.root = tree.insert(tree.root, 25);

/* The constructed AVL Tree would be
30
/ \
20 40
/ \ \
10 25 50
*/
System.out.println("Preorder traversal" +
" of constructed tree is : ");
tree.preOrder(tree.root);
}
}
// This code has been contributed by Mayank Jaiswal
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<<" ";
}
}
Loading