diff --git a/Logical Problems/reverse-number/reverse_big_int.cpp b/Logical Problems/reverse-number/reverse_big_int.cpp new file mode 100644 index 0000000000..e52c156320 --- /dev/null +++ b/Logical Problems/reverse-number/reverse_big_int.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +string reverse_number(string s){ + reverse(s.begin(),s.end()); + for(auto it:s){ + if(it<'0'||it>'9') return "Error"; + } + return s; +} + +int main(){ + string s; + cin>>s; + cout<=0 && rp +using namespace std; + +struct node{ + int val; + node *left=NULL,*right=NULL,*par=NULL; + bool red; + node(int x=0,bool c=true):val(x),red(c){} +}; + +class RB_tree{ + private: + node* r=NULL; + + private: + node* bst_insert(node* root,int key){ + if(root==NULL) return new node(key); + if(keyval) root->left=bst_insert(root->left,key); + else root->right=bst_insert(root->right,key); + return root; + } + + void left_rotate(node* root){ + node* y=root->right; + root->right=y->left; + if(y->left!=NULL) y->left->par=root; + y->par=root->par; + if(root->par==NULL) r=y; + else if(root==root->par->left) root->par->left=y; + else root->par->right=y; + y->left=root; + root->par=y; + } + + void right_rotate(node* root){ + node* y=root->left; + root->left=y->right; + if(y->right!=NULL) y->right->par=root; + y->par=root->par; + if(root->par==NULL) r=y; + else if(root==root->par->left) root->par->left=y; + else root->par->right=y; + y->right=root; + root->par=y; + } + + bool find(node* root,int key){ + if(root==NULL) return false; + if(root->val==key) return true; + if(key>root->val) return find(root->right,key); + return find(root->left,key); + } + + node* bst_del(node* root,int key){ + if(root==NULL) return root; + if(key==root->val){ + node* curr=root; + if(root->left==NULL) curr=root->right; + else if(root->right==NULL) curr=root->left; + else{ + node* succ=root->right; + while(succ->left!=NULL){ + curr=succ; + succ=succ->left; + } + if(curr!=root) curr->left=succ->right; + else root->right=succ->right; + root->val=succ->val; + curr=succ; + } + delete curr; + return root; + + } + else if(keyval) root->left=bst_del(root->left,key); + else root->right=bst_del(root->right,key); + return root; + } + + void inorder(node* root){ + if(root==NULL) return; + inorder(root->left); + cout<val<<' '; + inorder(root->right); + } + + public: + void rb_insert(int key){ + r=bst_insert(r,key); + node* z=r; + while(z->par!=NULL&&z->par->par!=NULL&&z->par->red){ + if(z->par==z->par->par->left){ + node* y=z->par->par->right; + if(y!=NULL&&y->red){ + z->par->red=false; + y->red=false; + z->par->par->red=true; + z=z->par->par; + } + else{ + if(z==z->par->right){ + z=z->par; + left_rotate(z); + } + z->par->red=false; + z->par->par->red=true; + right_rotate(z->par->par); + } + } + else{ + node* y=z->par->par->left; + if(y!=NULL&&y->red){ + z->par->red=false; + y->red=false; + z->par->par->red=true; + z=z->par->par; + } + else{ + if(z==z->par->left){ + z=z->par; + right_rotate(z); + } + z->par->red=false; + z->par->par->red=true; + left_rotate(z->par->par); + } + } + } + r->red=false; + } + + void rb_del(int key){ + r=bst_del(r,key); + node* x=r; + while(x!=NULL&&x->par!=NULL&&!x->red){ + if(x==x->par->left){ + node* w=x->par->right; + if(w->red){ + w->red=false; + x->par->red=true; + left_rotate(x->par); + w=x->par->right; + } + if((w->left==NULL||!w->left->red)&&(w->right==NULL||!w->right->red)){ + w->red=true; + x=x->par; + } + else{ + if(w->right==NULL||!w->right->red){ + w->left->red=false; + w->red=true; + right_rotate(w); + w=x->par->right; + } + w->red=x->par->red; + x->par->red=false; + w->right->red=false; + left_rotate(x->par); + x=r; + } + } + else{ + node* w=x->par->left; + if(w->red){ + w->red=false; + x->par->red=true; + right_rotate(x->par); + w=x->par->left; + } + if((w->left==NULL||!w->left->red)&&(w->right==NULL||!w->right->red)){ + w->red=true; + x=x->par; + } + else{ + if(w->left==NULL||!w->left->red){ + w->right->red=false; + w->red=true; + left_rotate(w); + w=x->par->left; + } + w->red=x->par->red; + x->par->red=false; + w->left->red=false; + right_rotate(x->par); + x=r; + } + } + } + if(x!=NULL) x->red=false; + } + + bool find(int key){ + return find(r,key); + } + + void print(){ + inorder(r); + cout<<'\n'; + } +}; + +int main() { + + return 0; +} diff --git a/search/binary_search/C/binarySearch.c b/search/binary_search/C/binarySearch.c index 9416dd16f5..9378bd3be3 100644 --- a/search/binary_search/C/binarySearch.c +++ b/search/binary_search/C/binarySearch.c @@ -1,32 +1,37 @@ #include +int main() +{ + int c, first, last, middle, n, search, array[100]; -int binarySearch(int arr[], int start, int end, int x) { + printf("Enter number of elements\n"); + scanf("%d", &n); - if (start <= end) { + printf("Enter %d integers\n", n); - int mid = start + (end - start) / 2; + for (c = 0; c < n; c++) + scanf("%d", &array[c]); - if (arr[mid] == x) - return mid; + printf("Enter value to find\n"); + scanf("%d", &search); - if (arr[mid] > x) - return binarySearch(arr, start, mid - 1, x); + first = 0; + last = n - 1; + middle = (first+last)/2; - return binarySearch(arr, mid + 1, end, x); - } - return -1; -} + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; -int main() -{ - int arr[] = { 2, 3, 7, 8, 78, 99, 102, 5555 }; - int x = 9; - int result = binarySearch(arr, 0, sizeof(arr) / sizeof(arr[0]), x); - - if (result == -1) { - printf("Element not present"); - } - else { - printf("Element found at index %d", result); - } + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; } diff --git a/sort/counting_sort/Java/CountSort.java b/sort/counting_sort/Java/CountSort.java index 238df06ea1..f0c84b7f50 100644 --- a/sort/counting_sort/Java/CountSort.java +++ b/sort/counting_sort/Java/CountSort.java @@ -1,34 +1,33 @@ +public class countingSort { + static void CountSort(int a[],int n){ + int max=a[0]; + for (int i = 0; i max){ + max=a[i]; + } + } + int[] elements=new int[max+1]; + for (int i = 0; i =0; i--) { + output[--elements[a[i]]]=a[i]; + } + for (int i = 0; i < n; i++) { + a[i]=output[i]; + } + + } + public static void main(String[] args) { + int[] a={1,6,4,2,7,5,3,8,5,7,4,6,2,1,5,7,4,7,4,7,5,67}; + CountSort(a, a.length); + for (int i = 0; i < a.length; i++) { + System.out.print(a[i]+" "); + } + } }