diff --git a/AVLTrees/a.out b/AVLTrees/a.out deleted file mode 100755 index e90f19a..0000000 Binary files a/AVLTrees/a.out and /dev/null differ diff --git a/AVLTrees/avl.c b/AVLTrees/avl.c index dddcf56..6027d66 100644 --- a/AVLTrees/avl.c +++ b/AVLTrees/avl.c @@ -1,23 +1,28 @@ -#include -#include +//Include Header files +#include +#include -struct Node{ +//Struct to represtt a node of +//+ the AVL tree +struct Node { int val; int height; int balance; + struct Node* left; struct Node* right; }; -int maxx(int a, int b) -{ - if(b>a) +//Function that returns the +//+ maximum of two values +int maxx(int a, int b) { + if(b > a) return b; return a; } -int maxim(struct Node* a, struct Node* b) -{ +//Returns the maximum height of the AVL Tree +int maxim(struct Node* a, struct Node* b) { if(b!=NULL && a==NULL) return b->height; if(b==NULL && a!=NULL) @@ -28,8 +33,8 @@ int maxim(struct Node* a, struct Node* b) return maxx(a->height,b->height); } -int getbalance(struct Node* a, struct Node* b) -{ +//FUnction to get difference of the height +int getbalance(struct Node* a, struct Node* b) { if(a!=NULL && b!=NULL) return a->height+1-(b->height+1); if(a!=NULL) @@ -40,44 +45,56 @@ int getbalance(struct Node* a, struct Node* b) return 0; } -struct Node* leftrotate(struct Node* z) -{ +//Left rotate function in the AVL Tree +struct Node* leftrotate(struct Node* z) { + struct Node* temp=z->left; struct Node* temp2=z->right->left; struct Node* tempfin=z->right; + tempfin->left=z; + z->right=temp2; z->left=temp; + tempfin->left->height=maxim(tempfin->left->left,tempfin->left->right)+1; tempfin->height=maxim(tempfin->left,tempfin->right)+1; tempfin->left->balance=getbalance(tempfin->left->left,tempfin->left->right); tempfin->balance=getbalance(tempfin->left,tempfin->right); + return tempfin; } -struct Node* rightrotate(struct Node* z) -{ +//Right rotate function in the AVL Tree +struct Node* rightrotate(struct Node* z) { + struct Node* temp=z->right; struct Node* temp2=z->left->right; struct Node* tempfin=z->left; + tempfin->right=z; + z->left=temp2; z->right=temp; + tempfin->right->height=maxim(tempfin->right->left,tempfin->right->right)+1; tempfin->height=maxim(tempfin->right,tempfin->left)+1; tempfin->right->balance=getbalance(tempfin->right->left,tempfin->right->right); tempfin->balance=getbalance(tempfin->left,tempfin->right); + return tempfin; } -struct Node* insert(int val, struct Node* temp) -{ - if(temp==NULL) - { +struct Node* insert(int val, struct Node* temp) { + + if(temp==NULL) { + struct Node* new=malloc(sizeof(struct Node)); + new->val=val; new->left=NULL; new->right=NULL; + new->height=0; new->balance=0; //printf("%d ", val); @@ -89,45 +106,45 @@ struct Node* insert(int val, struct Node* temp) temp->right=insert(val,temp->right); temp->height=maxim(temp->left,temp->right)+1; temp->balance=getbalance(temp->left,temp->right); - if(temp->balance==-2 && (temp->right->balance==-1||temp->right->balance==0)) - { + + if(temp->balance==-2 && (temp->right->balance==-1||temp->right->balance==0)) { temp=leftrotate(temp); } - else if(temp->balance==-2 && temp->right->balance==1) - { + else if(temp->balance==-2 && temp->right->balance==1) { temp->right=rightrotate(temp->right); temp=leftrotate(temp); } - else if(temp->balance==2 && (temp->left->balance==1||temp->left->balance==0)) - { + else if(temp->balance==2 && (temp->left->balance==1||temp->left->balance==0)) { temp=rightrotate(temp); } - else if(temp->balance==2 && temp->left->balance==-1) - { + else if(temp->balance==2 && temp->left->balance==-1) { temp->left=leftrotate(temp->left); temp=rightrotate(temp); } + return temp; } -void dfs(struct Node* temp) -{ +//dfs Function +void dfs(struct Node* temp) { + if(temp->left!=NULL) dfs(temp->left); + if(temp->right!=NULL) dfs(temp->right); + printf("%d ", temp->val); } -void check(int val, struct Node* temp) -{ - if(temp==NULL) - { +void check(int val, struct Node* temp) { + + if(temp==NULL) { printf("NO\n"); return; } - if(val==temp->val) - { + + if(val==temp->val) { printf("YES\n"); return; } @@ -137,26 +154,32 @@ void check(int val, struct Node* temp) check(val,temp->right); } -int main() -{ +//Main function +int main() { + //Variable declaration struct Node* mytree=NULL; int i,n,a,k; scanf("%d", &k); - while(k!=0) - { + + //Menu driven choice to insert/check + //+ the elements in the AVL Tree + while(k!=0) { scanf("%d", &n); - if(n==1) - { + + if(n==1) { scanf("%d",&a); mytree=insert(a,mytree); } - if(n==2) - { + + if(n==2) { scanf("%d",&a); check(a,mytree); } k--; + } + printf("\n"); return 0; + } diff --git a/AVLTrees/avldelete.c b/AVLTrees/avldelete.c index 24704b8..f850c0c 100644 --- a/AVLTrees/avldelete.c +++ b/AVLTrees/avldelete.c @@ -1,25 +1,31 @@ -#include -#include +//Including header files +#include +#include +//Global variable declaration int numofelem=0; +//Struct to represent node elements +//+ of the AVL Tree struct Node{ int val; int height; int balance; + struct Node* left; struct Node* right; }; -int maxx(int a, int b) -{ - if(b>a) +// Function to return maximum of +//+ two elements +int maxx(int a, int b) { + if(b > a) return b; return a; } -int maxim(struct Node* a, struct Node* b) -{ +//Function to return maximum height +int maxim(struct Node* a, struct Node* b) { if(b!=NULL && a==NULL) return b->height; if(b==NULL && a!=NULL) @@ -30,8 +36,8 @@ int maxim(struct Node* a, struct Node* b) return maxx(a->height,b->height); } -int getbalance(struct Node* a, struct Node* b) -{ +//Function to find difference of heights +int getbalance(struct Node* a, struct Node* b) { if(a!=NULL && b!=NULL) return a->height+1-(b->height+1); if(a!=NULL) @@ -42,49 +48,54 @@ int getbalance(struct Node* a, struct Node* b) return 0; } -struct Node* leftrotate(struct Node* z) -{ +//FUnction to left rotate elements of the AVL tree +struct Node* leftrotate(struct Node* z) { struct Node* temp=z->left; struct Node* temp2=z->right->left; struct Node* tempfin=z->right; + tempfin->left=z; + z->right=temp2; z->left=temp; + tempfin->left->height=maxim(tempfin->left->left,tempfin->left->right)+1; tempfin->height=maxim(tempfin->left,tempfin->right)+1; tempfin->left->balance=getbalance(tempfin->left->left,tempfin->left->right); tempfin->balance=getbalance(tempfin->left,tempfin->right); + return tempfin; } -struct Node* rightrotate(struct Node* z) -{ +//Function to right rotate elements of the AVL tree +struct Node* rightrotate(struct Node* z) { struct Node* temp=z->right; struct Node* temp2=z->left->right; struct Node* tempfin=z->left; + tempfin->right=z; + z->left=temp2; z->right=temp; + tempfin->right->height=maxim(tempfin->right->left,tempfin->right->right)+1; tempfin->height=maxim(tempfin->right,tempfin->left)+1; tempfin->right->balance=getbalance(tempfin->right->left,tempfin->right->right); tempfin->balance=getbalance(tempfin->left,tempfin->right); + return tempfin; } -struct Node* delete(struct Node* temp, int x) -{ - if(temp->val==x && temp->left==NULL && temp->right==NULL) - { +struct Node* delete(struct Node* temp, int x) { + + if(temp->val==x && temp->left==NULL && temp->right==NULL) { numofelem--; return NULL; } else if(temp->left==NULL && temp->right==NULL) return temp; - else - { - if(temp->val==x && temp->left!=NULL) - { + else { + if(temp->val==x && temp->left!=NULL) { struct Node* temp2=temp->left; while(temp2->right!=NULL) temp2=temp2->right; @@ -93,8 +104,7 @@ struct Node* delete(struct Node* temp, int x) temp->height=maxim(temp->left,temp->right)+1; temp->balance=getbalance(temp->left,temp->right); } - else if(temp->val==x && temp->right!=NULL) - { + else if(temp->val==x && temp->right!=NULL) { struct Node* temp2=temp->right; while(temp2->left!=NULL) temp2=temp2->left; @@ -109,32 +119,28 @@ struct Node* delete(struct Node* temp, int x) temp->right=delete(temp->right,x); temp->height=maxim(temp->left,temp->right)+1; temp->balance=getbalance(temp->left,temp->right); - if(temp->balance==-2 && (temp->right->balance==-1||temp->right->balance==0)) - { + + if(temp->balance==-2 && (temp->right->balance==-1||temp->right->balance==0)) { temp=leftrotate(temp); } - else if(temp->balance==-2 && temp->right->balance==1) - { + else if(temp->balance==-2 && temp->right->balance==1) { temp->right=rightrotate(temp->right); temp=leftrotate(temp); } - else if(temp->balance==2 && (temp->left->balance==1||temp->left->balance==0)) - { + else if(temp->balance==2 && (temp->left->balance==1||temp->left->balance==0)) { temp=rightrotate(temp); } - else if(temp->balance==2 && temp->left->balance==-1) - { + else if(temp->balance==2 && temp->left->balance==-1) { temp->left=leftrotate(temp->left); temp=rightrotate(temp); } + return temp; } } -struct Node* insert(int val, struct Node* temp) -{ - if(temp==NULL) - { +struct Node* insert(int val, struct Node* temp) { + if(temp==NULL) { struct Node* new=malloc(sizeof(struct Node)); new->val=val; new->left=NULL; @@ -151,28 +157,27 @@ struct Node* insert(int val, struct Node* temp) temp->right=insert(val,temp->right); temp->height=maxim(temp->left,temp->right)+1; temp->balance=getbalance(temp->left,temp->right); - if(temp->balance==-2 && (temp->right->balance==-1 || temp->right->balance==0)) - { + + if(temp->balance==-2 && (temp->right->balance==-1 || temp->right->balance==0)) { temp=leftrotate(temp); } - else if(temp->balance==-2 && temp->right->balance==1) - { + else if(temp->balance==-2 && temp->right->balance==1) { temp->right=rightrotate(temp->right); temp=leftrotate(temp); } - else if(temp->balance==2 && (temp->left->balance==1 || temp->left->balance==0)) - { + else if(temp->balance==2 && (temp->left->balance==1 || temp->left->balance==0)) { temp=rightrotate(temp); } - else if(temp->balance==2 && temp->left->balance==-1) - { + else if(temp->balance==2 && temp->left->balance==-1) { temp->left=leftrotate(temp->left); temp=rightrotate(temp); } + return temp; } -void dfs(struct Node* temp) -{ + +//dfs Function +void dfs(struct Node* temp) { if(temp==NULL) return; if(temp->left!=NULL) @@ -182,20 +187,24 @@ void dfs(struct Node* temp) printf("%d ", temp->val); } -int main() -{ +//Main function +int main() { + //Variable declaration struct Node* mytree=NULL; int i,n,a; scanf("%d", &n); - for(i=0;i0) mytree=delete(mytree,a); diff --git a/BIT/README.md b/BIT/README.md index a063ec4..05de6f0 100644 --- a/BIT/README.md +++ b/BIT/README.md @@ -1,4 +1,4 @@ -###Binary Indexed Trees (BIT) +# Binary Indexed Trees (BIT) BIT (or Fenwick trees) are generally used in cumulative frequency and cumulative sum problems. The basic underlying principle behind BITs is that each integer can be represented as sum of powers of two. @@ -9,13 +9,15 @@ We have n boxes that contain marbles. Possible queries are 2. Sum marbles from box 1 to box i The simple solution of running a loop through the array structure has time complexity of `O(1)` for query 1 and `O(n)` for query 2. + Another strategy is to create a prefix sum array and store sum from start to i at the i’th index in this array. + Sum of a given range can now be calculated in `O(1)` time, but update operation takes `O(n)` time now. + Using Binary Indexed Trees, we can perform both operations with worst case time complexity of `O(log n)` -####Implementation -BIT can implemented as arrays. Let's assume we have n elements. A node at index y is parent of a node at index x, -iff y can be obtained by removing last set bit from binary representation of x. +## Implementation +BIT can implemented as arrays. Let's assume we have n elements. A node at index y is parent of a node at index x, if y can be obtained by removing last set bit from binary representation of x. For example, `2 -> 0010 -> 0000`,`6 -> 0110 -> 0100` Hence, the nodes which are powers of 2 (1,2,4,8...) have the root node as their parent. (For sake of simplifying visualization of the tree we can create an array of length n + 1 and use 1-indexed notation,the 0th index being a dummy root node.) diff --git a/BIT/a.out b/BIT/a.out deleted file mode 100755 index 985f439..0000000 Binary files a/BIT/a.out and /dev/null differ diff --git a/BIT/bit.c b/BIT/bit.c index e0bba61..7941fad 100644 --- a/BIT/bit.c +++ b/BIT/bit.c @@ -1,44 +1,46 @@ -#include +//Include header files +#include +#include +//Global variable declaration int bit[100000]={0}; //Start from the first index int n; //This is point update and range query //Stores cumilative sum -void change(int index, int val) -{ - while(index<=n) - { +//Changes index in the BIT +void change(int index, int val) { + while(index<=n) { bit[index]+=val; index+=index&((-1)*index); } } -int query(int range) -{ +//Function to check for query +int query(int range) { int ans=0; - while(range>0) - { + while(range>0) { ans+=bit[range]; range-=range&((-1)*range); } return ans; } -int main() -{ +//Main function +int main() { + //Variable declaration int i,j,k,m,a,b; scanf("%d", &n); - for(i=1;i<=n;i++) - { + //Input elements + for(i=1; i<=n; i++) { scanf("%d", &a); change(i,a); } scanf("%d", &m); - for(j=1;j<=m;j++) - { - scanf("%d%d", &a, &b); + + for(j=1;j<=m;j++) { + scanf("%d %d", &a, &b); printf("%d\n", query(b)-query(a-1)); //query of a includes a as well, we dont want to include that vertex as we want sum from a (inclusive of a) till b } return 0; diff --git a/BIT/rangeupdateitspoj.c b/BIT/rangeupdateitspoj.c index 23de1e2..b33ea63 100644 --- a/BIT/rangeupdateitspoj.c +++ b/BIT/rangeupdateitspoj.c @@ -1,58 +1,56 @@ -#include +//Include header files +#include +#include +//Global variable declaration int bit[1000000]={0}; //Start from the first index int n; //Range Update and Query at a single point //Doesnt store cumilative sum -void change(int index, int val) -{ - while(index<=n) - { +void change(int index, int val) { + while(index<=n) { bit[index]+=val; index+=index&((-1)*index); } } -int query(int range) -{ +int query(int range) { int ans=0; - while(range>0) - { + while(range>0) { ans+=bit[range]; range-=range&((-1)*range); } return ans; } -void rangechange(int a, int b, int val) -{ +void rangechange(int a, int b, int val) { change(a,val); change(b+1,(-1)*val); //Only updates elements at those values from a to b } -int main() -{ +//Main function +int main() { + //Variable declaration int i,j,k,m,a,b,c; int tc,kk; scanf("%d", &tc); - for(kk=0;kk -#include +#include +#include // create a struct called `Node` that holds an int diff --git a/BinarySearchTrees/a.out b/BinarySearchTrees/a.out deleted file mode 100755 index 7ad48ab..0000000 Binary files a/BinarySearchTrees/a.out and /dev/null differ diff --git a/BinarySearchTrees/bst.c b/BinarySearchTrees/bst.c index bef4259..3d412c7 100644 --- a/BinarySearchTrees/bst.c +++ b/BinarySearchTrees/bst.c @@ -1,13 +1,16 @@ -#include -#include +//Including Header files +#include +#include -struct Node{ +//Struct to dfine a node in the tree +struct Node { int val; struct Node* left; struct Node* right; }; -struct Queue{ +//Queue using a struct +struct Queue { struct Node* value; struct Queue* next; }; @@ -18,22 +21,18 @@ int state; struct Node* mytree=NULL; -struct Node* delete(struct Node* temp, int x) -{ +struct Node* delete(struct Node* temp, int x) { if(temp->val==x && temp->left==NULL && temp->right==NULL) return NULL; - else - { - if(temp->val==x && temp->left!=NULL) - { + else { + if(temp->val==x && temp->left!=NULL) { struct Node* temp2=temp->left; while(temp2->right!=NULL) temp2=temp2->right; temp->val=temp2->val; temp->left=delete(temp->left,temp2->val); } - else if(temp->val==x && temp->right!=NULL) - { + else if(temp->val==x && temp->right!=NULL) { struct Node* temp2=temp->right; while(temp2->left!=NULL) temp2=temp2->left; @@ -48,42 +47,36 @@ struct Node* delete(struct Node* temp, int x) } } -void preorder(struct Node* temp) -{ - if(temp!=NULL) - { +//Function to traverse in a pre-order manner +void preorder(struct Node* temp) { + if(temp!=NULL) { printf("%d ", temp->val); preorder(temp->left); preorder(temp->right); } } -void postorder(struct Node* temp) -{ - if(temp!=NULL) - { +//Function to traverse in post-order +void postorder(struct Node* temp) { + if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("%d ", temp->val); } } -void inorder(struct Node* temp) -{ - if(temp!=NULL) - { +//Function to traverse inorder +void inorder(struct Node* temp) { + if(temp!=NULL) { inorder(temp->left); printf("%d ", temp->val); inorder(temp->right); } } -void checktree(struct Node* temp, int min, int max) -{ - if(temp!=NULL) - { - if(temp->val>=min && temp->val<=max) - { +void checktree(struct Node* temp, int min, int max) { + if(temp!=NULL) { + if(temp->val>=min && temp->val<=max) { checktree(temp->left,min,temp->val); checktree(temp->right,temp->val+1,max); } @@ -92,43 +85,39 @@ void checktree(struct Node* temp, int min, int max) } } -int find(int x) -{ +//Function to find an element in the BST +int find(int x) { struct Node* temp=mytree; - while(temp!=NULL) - { + while(temp!=NULL) { if(temp->val==x) return 1; if(temp->valright; else temp=temp->left; + } + return 0; } -void insert(int x) -{ +//Function to insert an element in the BST +void insert(int x) { struct Node* temp=mytree; struct Node* temp2=malloc(sizeof(struct Node)); temp2->left=NULL; temp2->right=NULL; temp2->val=x; - if(mytree==NULL) - { + if(mytree==NULL) { mytree=temp2; } - else - { - while(1) - { - if(temp->val>=x && temp->left==NULL) - { + else { + while(1) { + if(temp->val>=x && temp->left==NULL) { temp->left=temp2; break; } - else if(temp->valright==NULL) - { + else if(temp->valright==NULL) { temp->right=temp2; break; } @@ -141,44 +130,38 @@ void insert(int x) return; } -int main() -{ +//Main function +int main() { + //Variable initialisation int x,y,z; scanf("%d", &x); - while(x!=6) - { - if(x==1) - { + //Menu-driven to get a function of BST + while(x!=6) { + if(x==1) { scanf("%d", &y); insert(y); } - if(x==2) - { + if(x==2) { scanf("%d", &y); printf("%d\n", find(y)); } - if(x==3) - { + if(x==3) { inorder(mytree); printf("\n"); } - if(x==4) - { + if(x==4) { preorder(mytree); printf("\n"); } - if(x==5) - { + if(x==5) { postorder(mytree); printf("\n"); } - if(x==7) - { + if(x==7) { scanf("%d", &y); mytree=delete(mytree,y); } - if(x==8) - { + if(x==8) { state=0; checktree(mytree,-100000000,1000000000); if(state==0) diff --git a/Searching/LinearSeach.c b/Searching/LinearSeach.c new file mode 100644 index 0000000..9ba38d6 --- /dev/null +++ b/Searching/LinearSeach.c @@ -0,0 +1,34 @@ +//Standard Header files for Input-Output +#include +#include + +//Linear Search function +void linearSearch(int arr[], int n, int key) { + //Variable declaration + int i; + + //Linear Search + for(i = 0; i < n; i ++) { + if(arr[i] == key) { + printf("Key found at position %d",i); + break; + } + } + +} + +//Main Function +int main() { + //Memory-efficient array + int n; + scanf("%d",&n); + int arr[n],key,x; + + //Key for element to search + printf("Enter the value of the key"); + scanf("%d",&key); + + //Function call + linearSearch(arr,n,key); + return 0; +} diff --git a/Searching/README.md b/Searching/README.md new file mode 100644 index 0000000..bf8c27e --- /dev/null +++ b/Searching/README.md @@ -0,0 +1,9 @@ +# Searching Algorithms + +## Linear Search + + +## Binary Search + + +## Ternary Search \ No newline at end of file diff --git a/Searching/binarysearch.c b/Searching/binarysearch.c index ef9540b..ec84cbf 100644 --- a/Searching/binarysearch.c +++ b/Searching/binarysearch.c @@ -1,14 +1,16 @@ -#include -#include +//Including Header files +#include +#include -int A[1000000]; - -int binarysearch(int size, int x) -{ +//Binary Search function +int binarysearch(int A[],int size, int x) { + //Variable declaration int l,r,m; - l=0; r=size; - while(l<=r) - { + l=0; + r=size; + + //Binary Search implementation + while(l<=r) { m=(l+r)/2; if(A[m] -#include +//Including the header files +#include +#include -int A[1000000]; - -int binarysearchrecursive(int l, int r, int x) -{ +//Binary Search Recursive function +int binarysearchrecursive(int A[],int l, int r, int x) { + + //Varible declaration int m; + if(l>r) return -1; + m=(l+r)/2; + if(A[m]x) - return binarysearchrecursive(l,m-1,x); - return m; + return binarysearchrecursive(A,l,m-1,x); + else + return m; } -int main() -{ +//Main function devclaration +int main() { + //Declaring a memory efficient array int i,n,k,pos; scanf("%d", &n); + int A[n]; + + //Inputting the values of the array for(i=0;i -#include +//Including Header Files +#include +#include -int A[1000000]; - -int ternarysearch(int l, int r, int x) -{ +//Ternary Search Functions +int ternarysearch(int A[], int l, int r, int x) { + //Ternary search function variables int lmid,rmid; lmid=(2*l+r)/3; rmid=(l+2*r)/3; - while(lmid!=rmid) - { + + while(lmid!=rmid) { if(x<=A[lmid]) r=lmid; else if(x>A[rmid]) l=rmid; - else - { + else { l=lmid; r=rmid; } lmid=(2*l+r)/3; rmid=(l+2*r)/3; } + if(x==A[r]) return r; return -1; } -int main() -{ +//Main Function +int main() { + + //Declaring a memory efficient array int i,n,k,pos; scanf("%d", &n); - for(i=0;i -#include +//Including the header files +#include +#include -int A[1000000]; - -int ternarysearchrecursive(int l, int r, int x) -{ +//Ternary Search Recursive Functions +int ternarysearchrecursive(int A[], int l, int r, int x) { + + //Variable declaration int lmid,rmid; + if(A[r]==x) return r; + lmid=(2*l+r)/3; rmid=(l+2*r)/3; + if(lmid==rmid && A[rmid]!=x) return -1; + if(xA[rmid]) - return ternarysearchrecursive(rmid,r,x); - return ternarysearchrecursive(lmid,rmid,x); + return ternarysearchrecursive(A,rmid,r,x); + + return ternarysearchrecursive(A,lmid,rmid,x); } -int main() -{ +//Main function +int main() { + + //Declaring a memory-efficient array int i,n,k,pos; scanf("%d", &n); + int A[n]; + + //Inputting the values of the array for(i=0;i -#include +//Header files in C +#include +#include //Segment Tree consisting of minimums -int min(int a, int b) -{ - if(bend || tillwhere=end) @@ -40,10 +39,9 @@ int rangemin(int start, int end, int fromwhere, int tillwhere, int index) } -int changeval(int start, int end, int pos, int valtoadd, int index) -{ - if(pos>=start && pos<=end) - { +//Function to change values +int changeval(int start, int end, int pos, int valtoadd, int index) { + if(pos>=start && pos<=end) { if(start!=end) mintree[index]=min(changeval(start,(start+end)/2,pos,valtoadd,index*2+1),changeval((start+end)/2+1,end,pos,valtoadd,index*2+2)); else @@ -54,14 +52,19 @@ int changeval(int start, int end, int pos, int valtoadd, int index) return mintree[index]; } -int main() -{ +//Main function declaration +int main() { + //Variable declaration and initialisation int i,j,k,a,b,c; for(i=0;i<10;i++) val[i]=i+1; + //Segment tree values maketree(0,9,0); printf("%d\n", rangemin(0,9,5,8,0)); + + //CHange segment tree values changeval(0,9,8,-5,0); printf("%d\n", rangemin(0,9,5,9,0)); + return 0; } diff --git a/SegmentTrees/segmenttreesum.c b/SegmentTrees/segmenttreesum.c index eab8138..4817e7a 100644 --- a/SegmentTrees/segmenttreesum.c +++ b/SegmentTrees/segmenttreesum.c @@ -1,29 +1,29 @@ -#include -#include +//Include header files +#include +#include //Segment tree consisting of sums +//Global variable declaration int sumtree[100000]={0}; //The tree you construct int val[100000]={0}; //Given array -int maketree(int start, int end, int index) -{ +//Makes the segment tree +int maketree(int start, int end, int index) { //Index is the position in the segment tree //Node 0 is the total sum, Node 1 is the left half sum, Node 2 is the right half sum and so on.....if n is the parent node then 2*n+1 and 2*n+2 are the left and right child nodes of the parent - if(start==end) - { + if(start==end) { sumtree[index]=val[start]; return sumtree[index]; } - else - { + else { sumtree[index]=maketree(start,(start+end)/2,index*2+1)+maketree((start+end)/2+1,end,index*2+2); return sumtree[index]; } } -int rangesum(int start, int end, int fromwhere, int tillwhere, int index) -{ +//Returnns the range of the sum +int rangesum(int start, int end, int fromwhere, int tillwhere, int index) { if(fromwhere>end || tillwhere=end) //The range you require is withing the range of current index node @@ -32,16 +32,14 @@ int rangesum(int start, int end, int fromwhere, int tillwhere, int index) return rangesum(start,(start+end)/2,fromwhere,tillwhere,index*2+1)+rangesum((start+end)/2+1,end,fromwhere,tillwhere,index*2+2); } -void changeval(int start, int end, int pos, int valtoadd, int index) -{ - if(start==end) - { +//Change value of the segment tree +void changeval(int start, int end, int pos, int valtoadd, int index) { + if(start==end) { if(pos==start) sumtree[index]+=valtoadd; return; } - if(pos>=start && pos<=end) //Since the position to add value to in the array lies within the range of the current index node which is from start to end, we need to add valtoadd to the current index node and now we proceed to the subtrees where addition of this value may be required. - { + if(pos>=start && pos<=end) { //Since the position to add value to in the array lies within the range of the current index node which is from start to end, we need to add valtoadd to the current index node and now we proceed to the subtrees where addition of this value may be required. sumtree[index]+=valtoadd; changeval(start,(start+end)/2,pos,valtoadd,index*2+1); changeval((start+end)/2+1,end,pos,valtoadd,index*2+2); @@ -51,14 +49,18 @@ void changeval(int start, int end, int pos, int valtoadd, int index) return; } -int main() -{ +//Main function declaration +int main() { + //Variable initialisation int i,j,k,a,b,c; for(i=0;i<10;i++) val[i]=i+1; + //Make a segment tree maketree(0,9,0); printf("%d\n", rangesum(0,9,0,8,0)); + //Change the value of the segment tree changeval(0,9,8,10,0); printf("%d\n", rangesum(0,9,0,9,0)); + return 0; } diff --git a/Sortings/README.md b/Sorting/README.md similarity index 84% rename from Sortings/README.md rename to Sorting/README.md index f6e5f28..b783aed 100644 --- a/Sortings/README.md +++ b/Sorting/README.md @@ -2,7 +2,7 @@ ## Merge Sort -Merge sort is a sorting technique based on divide and conquer technique. Merge sort first divides the array into equal halves and then combines them in a sorted manner +Merge sort is a sorting technique based on **Divide and Conquer** . Merge sort first divides the array into equal halves and then combines them in a sorted manner recursively. ### Working of Merge Sort: @@ -10,7 +10,9 @@ To understand merge sort, we take an unsorted array as depicted below − 14,33,27,10,35,19,42,44 We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4. + 14,33,27,10 35,19,42,44 + Merge Sort Division : This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves. 14,33 27,10 35,19 42,44 @@ -21,14 +23,15 @@ We further divide these arrays and we achieve atomic value which can no more be ### Merge Sort Division We first compare the element for each list and then combine them into another list in sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order 19 and 35. 42 and 44 are placed sequentially. + Merge Sort Combine + 14,33 10,27 19,35 42,44 In next iteration of combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in sorted order. 10,14,33,27 19,35,42,44 After final merging, the list should look like this − -10,14,19,27,33,35,42,44 -Merge Sort +10,14,19,27,33,35,42,44 Worst case Time Complexity-O(nlogn) @@ -44,11 +47,11 @@ Step 3 − merge the smaller lists into new list in sorted order. ## QUICK SORT -Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. +Quicksort is a **Divide and Conquer** algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are: -1.Pick an element, called a pivot, from the array. +1.Pivot: Pick an element, called a pivot, from the array. 2.Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. diff --git a/Sorting/mergesort.c b/Sorting/mergesort.c new file mode 100644 index 0000000..dc71df8 --- /dev/null +++ b/Sorting/mergesort.c @@ -0,0 +1,68 @@ +//Including header files +#include +#include + +//Merge sort function +void merge(int A[], int start, int end) { + + //Variable declaration + int l,r,val,i,j,k; + int C[10000]; + + //Variable initialisation + val=start; + l=start; + r=(start+end)/2+1; + + while(l<=(start+end)/2 && r<=end) { + if(A[l]start) { + mergesort(A,start,(start+end)/2); + mergesort(A,(start+end)/2+1,end); + merge(A,start,end); + } + + return; +} + +int main() { + + //Variable declaration + int i,j,a,n; + scanf("%d", &n); + //Memory efficient way to declare an array + int A[n]; + + //Inputting the elements of the array + for(i = 0; i < n; i ++) + scanf("%d", &A[i]); + + //Mergesort function call + mergesort(A,0,n-1); + + //SOrted array being displayed + for(i = 0; i < n; i ++) + printf("%d ", A[i]); + printf("\n"); + + return 0; +} diff --git a/Sorting/mergesortinplace.c b/Sorting/mergesortinplace.c new file mode 100644 index 0000000..46fdd6a --- /dev/null +++ b/Sorting/mergesortinplace.c @@ -0,0 +1,78 @@ +//Including header files +#include +#include + +//Merge function +void merge(int A[], int start, int end) { + + //Variable declaration and initialisation + int l,r,val,i,j,k,reml,remr,temp; + reml=(start+end)/2-start+1; + remr=end-start+1-reml; + val=start; + l=start; + r=(start+end)/2+1; + + + while(reml>0 && remr>0) { + if(A[l]=l; j--) + A[j+1]=A[j]; + l++; + r++; + A[val++]=temp; + } + + } + + while(reml>0) { + A[val++]=A[l++]; + reml--; + } + + while(remr>0) { + A[val++]=A[r++]; + remr--; + } + + return; +} + +void mergesort(int A[], int start, int end) { + + if(end>start) { + mergesort(A,start,(start+end)/2); + mergesort(A,(start+end)/2+1,end); + merge(A,start,end); + } + + return; +} + +//Main Function +int main() { + //Varible declaration + int i,j,a,n; + scanf("%d", &n); + int A[n]; + + //INputting the elements of the array + for(i=0;i +#include +//Include time header file for random variable +#include + +//Global variable n declared +int n; + +int partition(int A[], int start, int end, int pivot) { + + //Variable declaration + int i,j,a,b,c,piv,temp; + + //Variable initialisation + piv=A[pivot]; + temp=A[pivot]; + A[pivot]=A[end]; + A[end]=temp; + int place=start; + + //Quick Sort implementation + for(i=start; i<=end; i++) { + if(A[i]<=piv) { + temp=A[place]; + A[place]=A[i]; + A[i]=temp; + place++; + } + } + + return place-1; +} + +void quick(int A[], int start, int end) { + + + int newpiv,random; + + if(start -#include - -void merge(int A[], int start, int end) -{ - int l,r,val,i,j,k; - int C[10000]; - val=start; l=start; r=(start+end)/2+1; - while(l<=(start+end)/2 && r<=end) - { - if(A[l]start) - { - mergesort(A,start,(start+end)/2); - mergesort(A,(start+end)/2+1,end); - merge(A,start,end); - } - return; -} - -int main() -{ - int A[1000000]; - int i,j,a,n; - scanf("%d", &n); - for(i=0;i -#include - -void merge(int A[], int start, int end) -{ - int l,r,val,i,j,k,reml,remr,temp; - reml=(start+end)/2-start+1; - remr=end-start+1-reml; - val=start; l=start; r=(start+end)/2+1; - while(reml>0 && remr>0) - { - if(A[l]=l;j--) - A[j+1]=A[j]; - l++; r++; - A[val++]=temp; - } - - } - while(reml>0) - { - A[val++]=A[l++]; - reml--; - } - while(remr>0) - { - A[val++]=A[r++]; - remr--; - } - return; -} - -void mergesort(int A[], int start, int end) -{ - if(end>start) - { - mergesort(A,start,(start+end)/2); - mergesort(A,(start+end)/2+1,end); - merge(A,start,end); - } - return; -} - -int main() -{ - int A[1000000]; - int i,j,a,n; - scanf("%d", &n); - for(i=0;i -#include -#include - -int n; - -int partition(int A[], int start, int end, int pivot) -{ - int i,j,a,b,c,piv,temp; - piv=A[pivot]; - temp=A[pivot]; - A[pivot]=A[end]; - A[end]=temp; - int place=start; - for(i=start;i<=end;i++) - { - if(A[i]<=piv) - { - temp=A[place]; - A[place]=A[i]; - A[i]=temp; - place++; - } - } - return place-1; -} - -void quick(int A[], int start, int end) -{ - int newpiv,random; - if(start -#include +//Includes the header files +#include +#include +//Global variable declaration int A[100000]; int head=-1; int tail=-1; - +//Size of the queue int sizeq; -void enqueue(int x) -{ - if(head==tail && head==-1) - { +//Enqueues the element passed in the function +void enqueue(int x) { + + if(head==tail && head==-1) { tail=(tail+1); head=tail; A[tail]=x; } - else - { - if((tail+1)%sizeq==head) - { + else { + if((tail+1)%sizeq==head) { tail=(tail+1)%sizeq; A[tail]=x; head++; printf("Overlap\n"); } - else - { + else { tail=(tail+1)%sizeq; A[tail]=x; } } + } -void dequeue() -{ - if(head==tail && head!=-1) - { +//Dequeues the element at the +//front of the queue +void dequeue() { + + if(head==tail && head!=-1) { head=-1; tail=-1; } - else if(head!=-1) - { + else if(head!=-1) { head=(head+1)%sizeq; } + } -void front() -{ +//Prints the element at the +//front of the queue +void front() { if(head!=-1) printf("%d\n", A[head]); } -void print() -{ +//Prints all the elements of the queue +void print() { int temphead=head; - while(temphead!=tail) - { + while(temphead!=tail) { printf("%d ", A[temphead]); temphead=(temphead+1)%sizeq; } @@ -64,24 +65,31 @@ void print() printf("\n"); } -int main() -{ +//Main function +int main() { + scanf("%d", &sizeq); int a,b,c,i,j,k; scanf("%d", &c); - while(c!=5) - { - if(c==1) - { + + //Menu driven loop to use the + //circular queue + while(c!=5) { + //Enqueue + if(c==1) { scanf("%d", &b); enqueue(b); } + if(c==2) dequeue(); + if(c==3) front(); + if(c==4) print(); + scanf("%d", &c); } } diff --git a/StacksAndQueues/queue.c b/StacksAndQueues/queue.c index 9f3da6c..066301c 100644 --- a/StacksAndQueues/queue.c +++ b/StacksAndQueues/queue.c @@ -1,80 +1,93 @@ -#include -#include +//Include header files +#include +#include -struct Queue{ +//Create Queue struct +struct Queue { int value; struct Queue* next; }; +//Initialising head and tail variables struct Queue* head=NULL; struct Queue* tail=NULL; -void enqueue(int x) -{ +//Enqueuing a variable into the array +void enqueue(int x) { + struct Queue* temp=malloc(sizeof(struct Queue)); temp->value=x; temp->next=NULL; - if(head==tail && head==NULL) - { + + if(head==tail && head==NULL) { head=temp; tail=head; } - else - { + else { head->next=temp; head=head->next; } + } -void dequeue() -{ - if(head==tail && head!=NULL) - { +//Dequeues the element in the rear +void dequeue() { + + if(head==tail && head!=NULL) { tail=NULL; head=NULL; } - else if(head!=NULL) - { + else if(head!=NULL) { tail=tail->next; } + } -void front() -{ +//Return the front element of the Queue +void front() { + if(tail!=NULL) printf("%d\n", tail->value); } -void printtot() -{ +//Prints all the elements in the Queue +void printtot() { + struct Queue* ptr=tail; - while(ptr!=NULL) - { + //Traverses throught the array + while(ptr!=NULL) { printf("%d ", ptr->value); ptr=ptr->next; } + printf("\n"); } -int main() -{ - int a; - int i,n,m; +//Main function +int main() { + //Variable initialisation + int i,n,m,a; scanf("%d", &n); - while(n!=5) - { - if(n==1) - { + + //Menu driven method + while(n!=5) { + //Enqueues the element in the Queue + if(n==1) { scanf("%d", &a); enqueue(a); } + //Dequeues the element at the front if(n==2) dequeue(); + //Returns the element at the front if(n==3) front(); + //Prints all the elements of the Queue if(n==4) printtot(); + scanf("%d", &n); } + return 0; } diff --git a/Trees/LeastCommonAncestor.c b/Trees/LeastCommonAncestor.c index 3dd6143..5ef6ef4 100644 --- a/Trees/LeastCommonAncestor.c +++ b/Trees/LeastCommonAncestor.c @@ -1,10 +1,17 @@ -#include -#include -#include +//Includes header files in the program +#include +#include +//String library in C +#include -//Use a stack to store paths when doing dfs, when we encounter a certain node, the values in the stack represent the path from the root to that node. -//We can get LCA also by storing intimes and outtimes etc +//Use a stack to store paths when doing dfs, +//+ when we encounter a certain node, the values +//+ in the stack represent the path from the root +//+ to that node. +//We can get LCA also by storing intimes +//+ and outtimes etc +//Global variable initialsiation int stack[100010]; int stackhead=-1; int afinarr[100010]; @@ -12,74 +19,80 @@ int bfinarr[100010]; int visited[100010]={0}; int a,b,acnt,bcnt; -struct LL{ +struct LL { int val; struct LL* next; }; struct LL* mylist[100010]={NULL}; -void dfs(int x) -{ +//dfs Function +void dfs(int x) { int ii; stackhead++; stack[stackhead]=x; - if(x==a) - { + if(x==a) { for(ii=0;ii<=stackhead;ii++) afinarr[ii]=stack[ii]; acnt=stackhead+1; } - if(x==b) - { + if(x==b) { for(ii=0;ii<=stackhead;ii++) bfinarr[ii]=stack[ii]; bcnt=stackhead+1; } + int i; visited[x]=1; struct LL* temp=mylist[x]; - while(temp!=NULL) - { + + while(temp!=NULL) { if(visited[temp->val]==0) dfs(temp->val); temp=temp->next; } + stackhead--; } -int main() -{ +//Main function +int main() { + //Variable declaration int pp,n,c,i,j,k,q,m,af,bf; scanf("%d", &n); q=n-1; //In a tree there are n-1 edges - for(i=0;ival=b; temp->next=mylist[a]; mylist[a]=temp; + struct LL* temp2=malloc(sizeof(struct LL)); temp2->val=a; temp2->next=mylist[b]; mylist[b]=temp2; } + scanf("%d", &m); //Number of queries - for(k=0;k -#include -#include - -//Program to find sum of distances b/w all nodes using number of children of each node +//Including header files +#include +#include +//Including string library +//to perform string functions +#include +//Program to find sum of distances b/w +//all nodes using number of children of +//each node long long int visited[100100]={0}; long long int a,b,acnt,bcnt; -struct LL{ +struct LL { long long int val; long long int weight; struct LL* next; }; -long long int min(long long int a, long long int b) -{ +//Returns the minimum of two variables +long long int min(long long int a, long long int b) { if(aval]==0) children[x]+=dfs(temp->val)+1; temp=temp->next; @@ -38,39 +41,46 @@ int dfs(long long int x) //printf("%d %d", x, children[x]); } -int main() -{ +//Main function +int main() { + //Variable initialisation long long int pp,n,c,i,j,k,q,m; long long int arr3[100100][4]; scanf("%lld", &n); q=n-1; //In a tree there are n-1 edges - for(i=0;ival=b; temp->weight=c; temp->next=mylist[a]; + struct LL* temp2=malloc(sizeof(struct LL)); temp2->val=a; temp2->weight=c; temp2->next=mylist[b]; + mylist[b]=temp2; mylist[a]=temp; } + dfs(1); //Given that 1 exists in the tree, we can choose any vertex to be root long long int sum=0; - for(i=0;i #include //For storing numbers with upto 10^5 digits - char stack[10000]; int start=-1; int state; -struct Trie{ +//Struct definition +struct Trie { char val; int count; struct Trie* children[11]; @@ -15,14 +16,12 @@ struct Trie{ struct Trie* mytrie=NULL; -void delete(char A[11]) -{ - if(search(A)!=0) - { +//Function to delete element from trie +void delete(char A[11]) { + if(search(A)!=0) { int i=0; struct Trie* temp=mytrie; - while(i<11 && A[i]!='\0') - { + while(i<11 && A[i]!='\0') { temp=temp->children[A[i]-'0']; i++; } @@ -30,29 +29,31 @@ void delete(char A[11]) } } -void frees(struct Trie* temp) -{ +void frees(struct Trie* temp) { + int i; + if(temp==NULL) return; - for(i=0;i<11;i++) - { + + for(i=0;i<11;i++) { frees(temp->children[i]); } + temp=NULL; free(temp); return; } -void insert(char A[11]) -{ +//INsert element in the trie +void insert(char A[11]) { + int i,j,k; i=0; struct Trie* temp=mytrie; - while(i<11 && A[i]!='\0') - { - if(temp->children[A[i]-'0']==NULL) - { + + while(i<11 && A[i]!='\0') { + if(temp->children[A[i]-'0']==NULL) { temp->children[A[i]-'0']=malloc(sizeof(struct Trie)); for(k=0;k<10;k++) temp->children[A[i]-'0']->children[k]=NULL; @@ -63,77 +64,88 @@ void insert(char A[11]) state=1; i++; } + temp->count++; + for(k=0;k<10;k++) if(temp->children[k]!=NULL) state=1; } -int search(char A[11]) -{ +//Search function implemented +int search(char A[11]) { + struct Trie* temp=mytrie; int i=0; - while(i<11 && A[i]!='\0') - { + + while(i<11 && A[i]!='\0') { temp=temp->children[A[i]-'0']; if(temp==NULL) return 0; i++; } + return temp->count; } -void printstack() -{ +//FUnction to print Trie Stack +void printstack() { int i; - for(i=1;i<=start;i++) //We print from the 2nd element in stack as the first element just has the start "#" value which we dont need to print + + for(i=1; i<=start; i++) //We print from the 2nd element in stack as the first element just has the start "#" value which we dont need to print printf("%c",stack[i]); printf("\n"); } -void dfs(struct Trie* temp) -{ +//dfs function +void dfs(struct Trie* temp) { int i; - if(temp!=NULL) - { + if(temp!=NULL) { + start++; stack[start]=temp->val; int k=temp->count; - while(k!=0) - { + + while(k!=0) { printstack(); k--; } + for(i=0;i<10;i++) dfs(temp->children[i]); + start--; } } -int main() -{ +//Main function added +int main() { + //Variable initialisation int i,j,k,tc,n,m,a,b,c; char string[11]; scanf("%d",&tc); - for(i=0;ival='#'; - for(k=0;k<11;k++) + + for(k=0; k<11; k++) mytrie->children[k]=NULL; state=0; scanf("%d\n",&n); - for(j=0;j