├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ └── feature_request.md ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yml └── workflows │ ├── Auto_message_on_pr_open.yml │ ├── c-cpp.yml │ ├── codesee-arch-diagram.yml │ ├── greetings.yml │ ├── label.yml │ └── stale.yml ├── .gitignore ├── C++ ├── Artificial Intelligence │ ├── A_Star.cpp │ └── README.md ├── Automata │ └── README.md ├── Backtracking │ └── README.md ├── Binary │ ├── Binary to gray conversion.cpp │ ├── Brain_Keringham.cpp │ ├── hammingCode.cpp │ └── hammingDistance.cpp ├── Caching │ └── README.md ├── Concurrent Algorithms │ ├── README.md │ └── peterson_algo.cpp ├── Cryptography │ ├── Affine_Cipher.cpp │ ├── CaeserCipher.cpp │ ├── ColumnarTranspositionCipher.cpp │ ├── Elgamal.cpp │ ├── HillCipher.cpp │ ├── MorseCodeConvotor.cpp │ ├── README.md │ ├── RSA_Algorithm.cpp │ ├── Railfence.cpp │ ├── SHA256.cpp │ ├── VernamCipher.cpp │ ├── VigenèreCipher.cpp │ ├── homophonic_substitution.cpp │ └── playfair_cipher.cpp ├── Data Compression │ ├── LZ78.cpp │ ├── README.md │ ├── burrow_wheeler_transform.cpp │ └── huffmanCoding.cpp ├── Deadlock Avoidance │ ├── BankerAlgorithm.cpp │ └── README.md ├── Divide-and-Conquer │ ├── N_Queen.cpp │ ├── README.md │ ├── karatsuba.cpp │ └── strassen_matrix_multiplication.cpp ├── Dynamic-Programming │ ├── 01_knapsack.cpp │ ├── Fibonacci_Number.cpp │ ├── LongestCommonSubsequence.cpp │ ├── Matrix_Chain_Multiplication.cpp │ ├── Min_Jump.cpp │ ├── Partition_Problem.cpp │ ├── README.md │ ├── Shortest Common Supersequence Problem.cpp │ ├── TravellingSalesMen.cpp │ ├── coinchange.cpp │ ├── edit_distance.cpp │ └── minimum_sum_partition.cpp ├── Game Theory │ ├── Grundy_Numbers.cpp │ └── README.md ├── Graphics │ ├── README.md │ └── Scanline_algorithm.cpp ├── Graphs │ ├── Kosaraju's Algorithm.cpp │ ├── Bellman_Form_Algorithm.cpp │ ├── BreadthFirstSearch.cpp │ ├── Dijkstra.cpp │ ├── Dinic.cpp │ ├── DisjoinSetUnion.cpp │ ├── Edmonds-Karp_Algorithm.cpp │ ├── Fleury_Algorithm.cpp │ ├── Ford-Fulkerson_Algorithm.cpp │ ├── Hierholzer_Algorithm.cpp │ ├── Hopcroft-Karp algorithm.cpp │ ├── Kruskals.cpp │ ├── Multisource_Shortest_Path.cpp │ ├── Prims_Algorithm.cpp │ ├── README.md │ ├── StoerWagner.cpp │ ├── Tarjan_Algorithm.cpp │ ├── dial_algorithm.cpp │ ├── floyd_warshall.cpp │ └── multistage_graph.cpp ├── Greedy-Algorithm │ ├── Activity_process.cpp │ ├── Egyptian_Fraction.cpp │ ├── JobSequencing.cpp │ ├── Merge_Interval.cpp │ ├── Optimal_merge_Pattern.cpp │ ├── README.md │ └── fractional_knapsack.cpp ├── Hashing │ ├── README.md │ ├── bloom_filter.cpp │ └── cuckoo_hashing.cpp ├── Image Processing │ ├── Canny Edge Detection Algorithm │ │ ├── cannyEdgeDetection.cpp │ │ └── dog.jpeg │ └── README.md ├── Job Scheduling │ ├── First_Come_First_Serve.cpp │ ├── Highest_Response_Ratio_Next.cpp │ ├── README.md │ ├── RoundRobin.cpp │ ├── Shortest_Job_First.cpp │ └── priority_scheduling.cpp ├── Linked-List │ ├── Add Two Numbers.cpp │ ├── CircularLinkedList.cpp │ ├── Detect Loop in Linked List.cpp │ ├── DoublyLinkedList.cpp │ ├── IntersectonOfTwoLinkedList.cpp │ ├── LRUCacheDesign.cpp │ ├── MergeKSortedList.cpp │ ├── MergeSortLinkedList.cpp │ ├── MergeTwoSortedll.cpp │ ├── Reverse a singly linked list.cpp │ ├── Reverse in group of K.cpp │ ├── SinglyLinkedList.cpp │ ├── linklist_length.cpp │ └── palindrome.cpp ├── Machine-Learning │ ├── K-nearest-neighbour-algo.cpp │ ├── FeedFwdNN.cpp │ ├── KMeansClustering.cpp │ ├── LogisticRegression.cpp │ ├── NaiveBayesClassifier.cpp │ └── linearReg.cpp ├── Maths │ ├── Bentley_Ottmann_Algorithm.cpp │ ├── Binary Exponentiation Algorithm.cpp │ ├── Binet-Formula-for-Fibonacci-Sequence.cpp │ ├── BinomialTheorem.cpp │ ├── Chinese_remainder.cpp │ ├── Chudnovsky.cpp │ ├── CongruencyOfTriangles.cpp │ ├── DeciToBinary_BinaryToDeci.cpp │ ├── Extended_Eucledian_Algorithm.cpp │ ├── GCD and LCM.cpp │ ├── Infix_Postfix_Prefix_Interconversion.cpp │ ├── Interquartile_range.cpp │ ├── Is_prime.cpp │ ├── Josephus.cpp │ ├── Lucas_Lehmer_Series.cpp │ ├── Luhn algorithm.cpp │ ├── Manhattan_distance.cpp │ ├── Modular_exponentiation.cpp │ ├── Pascal_Triangle.cpp │ ├── Power_Set.cpp │ ├── QuickHull Algorithm.cpp │ ├── Reverse_integer.cpp │ ├── Roman_to_Decimal.cpp │ ├── RussianPeasant.cpp │ ├── Sieve of Eratosthenes Algorithm.cpp │ ├── SimilarTriangles.cpp │ ├── Strong_Number.cpp │ ├── armstrong.cpp │ ├── bisection.cpp │ ├── closures.cpp │ ├── factorial.cpp │ ├── factors.cpp │ ├── freivald.cpp │ ├── heronsFormula.cpp │ ├── perfect.cpp │ ├── segmentUnionLength.cpp │ ├── shriDharacharya.cpp │ ├── sudoku.cpp │ └── zeller.cpp ├── Memory Allocation │ ├── Best_Fit_Fixed_Memory.cpp │ ├── Next_Fit_Fixed_Memory.cpp │ ├── README.md │ └── Worst_Fit_Fixed_Memory.cpp ├── Optimization │ ├── Hill_climbing.cpp │ ├── README.md │ ├── genetic.cpp │ ├── greywolf.cpp │ ├── particle_swarm_optimization.cpp │ ├── shuffled_frog_leaping_optimization.cpp │ └── simulated_annealing.cpp ├── Pattern-Matching │ ├── AhoCorasickAlgorithm.cpp │ ├── Boyer-moore.cpp │ ├── KMP_Algorithm.cpp │ ├── README.md │ ├── Z_Algorithm.cpp │ └── rabin_karp.cpp ├── README.md ├── Recursion │ ├── Permutations.cpp │ ├── README.md │ └── TowerOfHanoi.cpp ├── Searching │ ├── Binary_search_in_2d_matrix.cpp │ ├── Exponential_search.cpp │ ├── Jump_Search.cpp │ ├── README.md │ ├── Ternary_Search.cpp │ ├── binary_search.cpp │ ├── fibonacci_search.cpp │ ├── interpolation_search.cpp │ ├── linear_search.cpp │ ├── order_agnostic_bin_search.cpp │ ├── quick_select.cpp │ └── sublist_search.cpp ├── Signal Processing │ └── README.md ├── Sliding Window │ └── README.md ├── Sorting │ ├── Bead_sort.cpp │ ├── Bidirectional_Selection_Sort.cpp │ ├── BogoSortIn_C++.cpp │ ├── Bucket_Sort.cpp │ ├── Gnome_sort.cpp │ ├── Odd_Even_Sort.cpp │ ├── README.md │ ├── StoogeSort.cpp │ ├── Topological_Sort.cpp │ ├── Triplet.cpp │ ├── Wave_sort.cpp │ ├── bingo_Sort.cpp │ ├── bubble_sort.cpp │ ├── cocktail_sort.cpp │ ├── comb_sort.cpp │ ├── counting_sort.cpp │ ├── cyclic_sort.cpp │ ├── depth_first_search.cpp │ ├── dutch_national_flag_algorithm.cpp │ ├── heap_sort.cpp │ ├── insertion_sort.cpp │ ├── intro_sort.cpp │ ├── merge_sort.cpp │ ├── pancake_sort.cpp │ ├── pigeonhole sort.cpp │ ├── quick_sort.cpp │ ├── radix_sort.cpp │ ├── randomized_quicksort.cpp │ ├── selection_sort.cpp │ ├── shell_sort.cpp │ └── tim_sort.cpp ├── Stack │ ├── Max_Area_Histogram.cpp │ ├── Next greater element.cpp │ ├── README.md │ ├── Rain_Water.cpp │ └── Stack_using_Priority_queue.cpp ├── String │ ├── Beautiful_string.cpp │ ├── Camel_Case.cpp │ └── Manachers_Algorithm.cpp ├── Sub-Arrays │ ├── Boyer-Moore Majority Voting Algorithm.cpp │ ├── Kadane.cpp │ ├── README.md │ └── Unsorted_Array.cpp ├── Trees │ ├── AO_Star.cpp │ ├── AVLTree.cpp │ ├── BPlusTree.cpp │ ├── BinaryTreeToBST.cpp │ ├── Binary_Tree_View.cpp │ ├── Fenwick Tree.cpp │ ├── MinIndexedDHeap.cpp │ ├── MorrisTraversal.cpp │ ├── README.md │ ├── SegmentTree.cpp │ ├── Symmetric_Tree.cpp │ ├── TernarySearchTree.cpp │ ├── Trie.cpp │ ├── Van Emde Boas.cpp │ ├── binarySearchTree.cpp │ ├── red_black.cpp │ ├── splaytree.cpp │ ├── treap_algo.cpp │ └── tree_traversals.cpp └── arrays │ ├── Diagonal_Printing_in_2D_arrays.cpp │ ├── Fisher_Yates_Shuffle_Algorithm.cpp │ ├── README.md │ ├── monotonic_array.cpp │ └── spiralPrint.cpp ├── C ├── Artificial Intelligence │ ├── A_star.c │ └── README.md ├── Automata │ └── README.md ├── Backtracking │ └── README.md ├── Binary │ ├── Binary to Gray Conversion.c │ ├── Brain_Keringham.c │ ├── hammingCode.c │ └── hammingDistance.c ├── Caching │ └── README.md ├── Concurrent Algorithms │ ├── README.md │ └── peterson_algo.c ├── Cryptography │ ├── Affine_Cipher.c │ ├── CaesarCipher.c │ ├── ColumnarTranspositionCipher.c │ ├── Elgamal.c │ ├── Hill_Cipher.c │ ├── MorseCodeConvortor.c │ ├── README.md │ ├── RSA_Algo.c │ ├── Railfence.c │ ├── SHA256.c │ ├── VernamCipher.c │ ├── homophonic_substitution.c │ └── playfair_cipher.c ├── Data Compression │ ├── LZ78.c │ ├── README.md │ ├── burrow_wheeler_transform.c │ └── huffmanCoding.c ├── Deadlock Avoidance │ ├── README.md │ └── bankers.c ├── Divide-and-Conquer │ ├── N_Queen.c │ ├── README.md │ └── Strassens.c ├── Dynamic-Programming │ ├── 01_knapsack.c │ ├── Fibonacci_Number.c │ ├── Longest_Common_Subsequence.c │ ├── MatrixChainMultiplication.c │ ├── Min_Jump.c │ ├── README.md │ ├── Shortest Common Supersequence Problem.c │ ├── Travelling_Salesman.c │ ├── coinchange.c │ ├── edit_distance.c │ └── minimum_sum_partition.c ├── Game Theory │ ├── Grundy_Numbers.c │ └── README.md ├── Graphics │ └── README.md ├── Graphs │ ├── BreadthFirstSearch.c │ ├── Dijkstra.c │ ├── DisjointSetUnion.c │ ├── Edmonds-Karp_Algorithm.c │ ├── Fleury_Algorithm.c │ ├── FordFulkerson.c │ ├── Hierholzer_Algorithm.c │ ├── Hopcroft-Karp algorithm.c │ ├── Kruskals.c │ ├── Multisource_Shortest_Path.c │ ├── Prims.c │ ├── README.md │ ├── StoerWagner.c │ ├── Tarjan.c │ ├── dial_algorithm.c │ ├── floyd_warshall.c │ └── multistage_graph.c ├── Greedy-Algorithm │ ├── ActivitySelection.c │ ├── Egyptian_Fraction.c │ ├── JobSequencing.c │ ├── Merge_Interval.c │ ├── Optimal_Merge_Pattern.c │ ├── README.md │ └── fractional_knapsack.c ├── Hashing │ ├── README.md │ ├── bloom_filter.c │ └── cuckoo_hashing.c ├── Image Processing │ ├── Canny Edge Detection Algorithm │ │ ├── cannyEdgeDetection.c │ │ └── dog.jpeg │ └── README.md ├── Job Scheduling │ ├── First_Come_First_Serve.c │ ├── Highest_Response_Ratio_Next.c │ ├── README.md │ ├── RoundRobin.c │ ├── Shortest_Job_First.c │ └── priority_scheduling.c ├── Linked-List │ ├── AdditionOfTwoNumbersLL.c │ ├── Circular Linked List.c │ ├── DoublyLinkedList.c │ ├── IntersectionOfLinkedList.c │ ├── LRUCacheDesign.c │ ├── MergeKSortedList.c │ ├── MergeSortLinkedList.c │ ├── Reverse a singly linked list.c │ ├── Reverse in group of K.c │ ├── SinglyLinkedList.c │ ├── linklist_length.c │ ├── mergetwosortedll.c │ └── palindrome.c ├── Machine Learning │ ├── KNN.c │ ├── Kmeans.c │ ├── LogisticRegression.c │ ├── README.md │ └── linearregression.c ├── Maths │ ├── Bentley_Ottmann_Algorithm.c │ ├── Binary_expo.c │ ├── Binet-Formula-for-Fibonacci-Sequence.c │ ├── BinomialTheorem.c │ ├── Chinese_reaminder.c │ ├── Chudnovsky.c │ ├── CongruencyOfTriangles.c │ ├── Extended_Eucledian_Algorithm.c │ ├── GCD and LCM.c │ ├── Interquartile_range.c │ ├── Lucas_Lehmer_Series.c │ ├── Luhn algorithm.c │ ├── Manhattan_distance.c │ ├── ModularExponentiation.c │ ├── Pascal_Triangle.c │ ├── Power_Set.c │ ├── QuickHull Algorithm.c │ ├── Reverse Integer.c │ ├── Roman_to_Decimal.c │ ├── RussianPeasant.c │ ├── SimilarTriangles.c │ ├── Strong_Number.c │ ├── armstrong.c │ ├── binary_to_decimal_&_decimal_to_binary.c │ ├── bisection.c │ ├── closures.c │ ├── factorial.c │ ├── factors.c │ ├── freivald.c │ ├── heronsFormula.c │ ├── is_Prime.c │ ├── josephus.c │ ├── perfect.c │ ├── segmentUnionLength.c │ ├── shriDharacharya.c │ ├── sieveOfEratosthenes.c │ ├── sudoku.c │ └── zeller.c ├── Memory Allocation │ ├── Best_Fit_Fixed_Memory.c │ ├── Next_Fit_Fixed_Memory.c │ ├── README.md │ └── Worst_Fit_Fixed_memory_blocks.c ├── Optimization │ ├── Hill_climbing.c │ ├── README.md │ └── greywolf.c ├── Pattern-Matching │ ├── AhoCorasickAlgorithm.c │ ├── Boyer-moore.c │ ├── KMP Algorithm.c │ ├── README.md │ ├── RabinKarpAlgo.c │ └── Z_Algorithm.c ├── README.md ├── Recursion │ ├── Permutations.c │ ├── README.md │ └── TowerOfHanoi.c ├── Searching │ ├── Exponential_search.c │ ├── Jump_Search.c │ ├── Order_Agnostic_Binary_Search.c │ ├── README.md │ ├── Ternary-Search.c │ ├── binary_search.c │ ├── binary_search_in_a_2D_matrix.c │ ├── fibonacci_search.c │ ├── interpolation_search.c │ ├── linear_search.c │ ├── quick_select.c │ └── sublist_search.c ├── Signal Processing │ └── README.md ├── Sliding Window │ └── README.md ├── Sorting │ ├── Bead_sort.c │ ├── Bidirectional_Selection_Sort.c │ ├── BogoSortIn_C.c │ ├── Bucket_Sort.c │ ├── CocktailSort.c │ ├── Counting_Sort.c │ ├── Cyclic_Sort.c │ ├── Gnome_Sort.c │ ├── Odd_Even_Sort.c │ ├── Pigeonhole sort.c │ ├── Quick_Sort.c │ ├── README.md │ ├── StoogeSort.c │ ├── Tim_sort.c │ ├── Triplet.c │ ├── bingo_sort.c │ ├── bubble_sort.c │ ├── comb_sort.c │ ├── dnf_sort.c │ ├── heap_sort.c │ ├── insertion_sort.c │ ├── intro_sort.c │ ├── merge_sort.c │ ├── pancake_sort.c │ ├── radix_sort.c │ ├── randomised_quicksort.c │ ├── selection_sort.c │ └── shell_sort.c ├── Stack │ ├── Max_Area_Histogram.c │ ├── README.md │ ├── Rain_Water.c │ ├── Stack_using_Priority_Queue.c │ └── nextgreaterelement.c ├── String │ ├── Beautiful_string.c │ ├── Camel_Case.c │ └── Manachers_Algorithm.c ├── Sub-Arrays │ ├── BoyerMooreAlgo.c │ ├── Kadane_Algorithm.c │ ├── README.md │ └── Unsorted_Array.c ├── Trees │ ├── AVL_Tree.c │ ├── BPlusTree.c │ ├── BinaryTreeToBST.c │ ├── Binary_Tree_View.c │ ├── Fenwick Tree.c │ ├── MinIndexedDHeap.c │ ├── README.md │ ├── SegmentTree.c │ ├── SplayTree.c │ ├── Symmetric_Tree.c │ ├── TernarySearchTree.c │ ├── Van Emde Boas.c │ ├── binary_search_tree.c │ ├── morris_transversal.c │ ├── treap_algo.c │ ├── tree_traversals.c │ └── trie.c └── arrays │ ├── Diagonal_Printing_in_2D_arrays.c │ ├── Fisher_Yates_Shuffle_Algorithm.c │ ├── README.md │ ├── monotonic_array.c │ └── spiralprint.c ├── CODE_OF_CONDUCT.md ├── Java ├── Artificial Intelligence │ ├── A_star.java │ └── README.md ├── Automata │ └── README.md ├── Backtracking │ └── README.md ├── Binary │ ├── BinarytoGray.java │ ├── Brain_Keringham.java │ ├── hammingCode.java │ └── hammingDistance.java ├── Caching │ └── README.md ├── Concurrent Algorithms │ ├── README.md │ └── peterson_algo.java ├── Cryptography │ ├── Affine_Cipher.java │ ├── CaesarCipher.java │ ├── ColumnarTranspositionCipher.java │ ├── Elgamal.java │ ├── HillCipher.java │ ├── HomophonicSubstitution.java │ ├── MorseCodeConverter.java │ ├── PlayfairCipher.java │ ├── README.md │ ├── RSA_Algorithm.java │ ├── Rail_Fence_Cipher.java │ ├── SHA256.java │ ├── VernamCipher.java │ └── VigenèreCipher.java ├── Data-Compression │ ├── Huffman.java │ ├── LZ78.java │ ├── README.md │ └── burrow_wheeler_transform.java ├── Deadlock Avoidance │ ├── Bankers_Algorithm.java │ └── README.md ├── Divide-and-Conquer │ ├── KaratsubaAlgo.java │ ├── N_Queen.java │ ├── README.md │ └── strassen_matrix_multiplication.java ├── Dynamic-Programming │ ├── Fibonacci_Number.java │ ├── LongestCommonSubsequence.java │ ├── MatrixChainMultiplication.java │ ├── Min_Jump.java │ ├── PartitionProblem.java │ ├── README.md │ ├── Shortest_Common_Supersequence_Problem.java │ ├── TravellingSalesMan.java │ ├── _01_knapsack.java │ ├── coinchange.java │ ├── edit_distance.java │ └── minimum_sum_partition.java ├── Game Theory │ ├── Grundy_Numbers.java │ └── README.md ├── Graphics │ └── README.md ├── Graphs │ ├── Bellman_Form_Algorithm.java │ ├── BreadthFirstSearch.java │ ├── Dijkstra.java │ ├── Dinic.java │ ├── DisjointSetUnion.java │ ├── EdmondsKarpAlgorithm.java │ ├── Fleury_Algorithm.java │ ├── FordFulkerson.java │ ├── Hamiltonian_path.java │ ├── Hierholzer_Algorithm.java │ ├── Hopcroft-Karp.java │ ├── Kosaraju_Algorithm.java │ ├── Kruskals.java │ ├── Multisource_Shortest_Path.java │ ├── MultistageGraph.java │ ├── Prims.java │ ├── README.md │ ├── StoerWagner.java │ ├── Tarjan.java │ ├── dial_algorithm.java │ └── floyd_warshall.java ├── Greedy-Algorithm │ ├── ActivitySelection.java │ ├── Egyptian_Fraction.java │ ├── FractionalKnapsack.java │ ├── Job_Sequencing.java │ ├── Merge_Interval.java │ ├── Optimal_Merge_Pattern.java │ └── README.md ├── Hashing │ ├── BloomFilter.java │ ├── README.md │ └── cuckoo_hashing.java ├── Image Processing │ ├── Canny Edge Detection Algorithm │ │ ├── cannyEdgeDetection.java │ │ └── dog.jpeg │ └── README.md ├── Job Scheduling │ ├── First_Come_First_Serve.java │ ├── Highest_Response_Ratio_Next.java │ ├── README.md │ ├── RoundRobin.java │ ├── Shortest_Job_First.java │ └── priority_scheduling.java ├── Linked-List │ ├── AdditionOfTwoNumbersinLL.java │ ├── CircularLinkedList.java │ ├── DoubleLinkedList.java │ ├── LRUCacheDesign.java │ ├── LinkedListIntersection.java │ ├── LinkedListPalindrome.java │ ├── MergeKSortedLists.java │ ├── MergeSortLinkedList.java │ ├── Reverse_in_group_of_K.java │ ├── Reverselinkedlist.java │ ├── Singly Linked List.java │ ├── linklist_length.java │ └── mergeTwoSortedLL.java ├── Machine Learning │ ├── KNN.java │ ├── Linear_Regression.java │ ├── LogisticRegression.java │ └── README.md ├── Maths │ ├── Bentley_Ottmann_Algorithm.java │ ├── Binary_Exponentiation.java │ ├── Binet_Formula.java │ ├── BinomialTheroem.java │ ├── Chinese_remainder.java │ ├── Chudnovsky.java │ ├── CongruencyOfTriangles.java │ ├── DeciToBinary_BinaryToDeci.java │ ├── Extended_Euclidean_Algorithm.java │ ├── Factors.java │ ├── GCD_and_LCM.java │ ├── Interquartile_range.java │ ├── KadaneAlgo.java │ ├── Lucas_Lehmer_Series.java │ ├── LuhnAlgorithm.java │ ├── Manhattan_distance.java │ ├── ModularExponentiation.java │ ├── Pascal_Triangle.java │ ├── PerfectNumber.java │ ├── PowerSet.java │ ├── QuickHull_Algorithm.java │ ├── ReverseInteger.java │ ├── Roman_to_Decimal.java │ ├── RussianPeasant.java │ ├── ShriDharacharya.java │ ├── Sieve_erastonetihis.java │ ├── SimilarTriangles.java │ ├── Strong_Number.java │ ├── armstrong.java │ ├── bisection.java │ ├── closures.java │ ├── factorial.java │ ├── freivald.java │ ├── heronsFormula.java │ ├── is_prime.java │ ├── josephus.java │ ├── segmentUnionLength.java │ ├── sudoku.java │ └── zeller.java ├── Memory Allocation │ ├── BestFitFixedMemory.java │ ├── NextFitFixedMemory.java │ ├── README.md │ └── WorstFitFixedMemory.java ├── Optimization │ ├── Hill_climbing.java │ ├── README.md │ └── grey_wolf.java ├── Pattern-Matching │ ├── AhoCorasickAlgorithm.java │ ├── Boyer_moore.java │ ├── KmpAlgo.java │ ├── README.md │ ├── RabinKarp.java │ └── Z_Algorithm.java ├── README.md ├── Recursion │ ├── Permutations.java │ ├── README.md │ └── TowerOfHanoi.java ├── Searching │ ├── Exponential_search.java │ ├── JumpSearch.java │ ├── Order_Agnostic_Binary_Search.java │ ├── QuickSelect.java │ ├── README.md │ ├── SublistSearch.java │ ├── Ternary_Search.java │ ├── binary_search.java │ ├── binary_search_2d.java │ ├── fibonacci_search.java │ ├── interpolation_search.java │ └── linear_search.java ├── Signal Processing │ └── README.md ├── Sliding Window │ └── README.md ├── Sorting │ ├── BeadSort.java │ ├── Bidirectional_Selection_Sort.java │ ├── BogoSortIn_java.java │ ├── BucketSort.java │ ├── CocktailSort.java │ ├── Depth_first_search.java │ ├── Dutch_National_Flag.java │ ├── Gnome_Sorting.java │ ├── Odd_Even_Sort.java │ ├── PanCakeSort.java │ ├── QuickSort.java │ ├── README.md │ ├── StoogeSort.java │ ├── Tim_sort.java │ ├── Triplet.java │ ├── bingo_Sort.java │ ├── bubble_sort.java │ ├── comb_sort.java │ ├── counting_sort.java │ ├── cyclic_sort.java │ ├── heap_sort.java │ ├── insertion_sort.java │ ├── intro_sort.java │ ├── merge_sort.java │ ├── pigeonhole sort.java │ ├── radix_sort.java │ ├── randomized_quicksort.java │ ├── selection_sort.java │ ├── shell_sort.java │ └── wave_sort.java ├── Stack │ ├── Max_Area_Histogram.java │ ├── NextGreater.java │ ├── README.md │ ├── Rain_Water.java │ └── Stack_using_priorityqueue.java ├── String │ ├── Beautiful_Sorting.java │ ├── Camel_Case.java │ └── Manachers_Algorithm.java ├── Sub-Arrays │ ├── BoyerMoore.java │ ├── KadanesAlgorithm.java │ ├── README.md │ └── Unsorted_Array.java ├── Trees │ ├── BPlusTree.java │ ├── BinaryTreeToBST.java │ ├── Binary_Tree_View.java │ ├── Fenwick_Tree.java │ ├── MinIndexedDHeap.java │ ├── README.md │ ├── RedBlackTree.java │ ├── SegmentTree.java │ ├── Symmetric_Tree.java │ ├── TernarySearchTree.java │ ├── Trie.java │ ├── Van Emde Boas.java │ ├── aostar.java │ ├── avlTree.java │ ├── splayTree.java │ ├── treap_algo.java │ └── tree_traversals.java └── arrays │ ├── Diagonal_Printing_in_2D_arrays.java │ ├── FisherYatesShuffle.java │ ├── README.md │ ├── monotonic_array.java │ └── spiralPrint.java ├── LICENSE ├── Python ├── Artificial Intelligence │ ├── A_star.py │ └── README.md ├── Automata │ └── README.md ├── Binary │ ├── Binary to Gray conversion.py │ ├── Brain_Keringham.py │ ├── hammingCode.py │ └── hammingDistance.py ├── Caching │ └── README.md ├── Concurrent Algorithms │ ├── README.md │ └── peterson_algo.py ├── Cryptography │ ├── Affine_Cipher.py │ ├── CaesarCipher.py │ ├── ColumnarTranspositionCipher.py │ ├── DiffieHellman.py │ ├── Elgamal.py │ ├── HillCipher.py │ ├── MorseCodeConvortor.py │ ├── README.md │ ├── SHA256.py │ ├── VernamCipher.py │ ├── homophonic_Substitution.py │ ├── playfair_cipher.py │ ├── railfenceCipher.py │ ├── rsa.py │ └── vigenere_cipher.py ├── Data Compression │ ├── LZ78.py │ ├── README.md │ ├── burrow_wheeler_transform.py │ └── huffmanCoding.py ├── Deadlock Avoidance │ ├── README.md │ └── bankers_algorithm.py ├── Deep learning │ └── perceptron_learning.py ├── Divide-and-Conquer │ ├── Karatsuba.py │ ├── N_Queen.py │ ├── README.md │ └── strassen_matrix_multiplication.py ├── Dynamic-Programming │ ├── 01_knapsack.py │ ├── Fibonacci_Number.py │ ├── Longest_Common_Subsequence.py │ ├── MatrixChainMultiplication.py │ ├── Min_Jump.py │ ├── README.md │ ├── Shortest Common Supersequence Problem.py │ ├── TravellingSalesMan.py │ ├── coinchange.py │ ├── edit_distance.py │ └── minimum_sum_partition.py ├── Game Theory │ ├── Grundy_Numbers.py │ └── README.md ├── Graphics │ ├── README.md │ └── Scanline-algo.py ├── Graphs │ ├── BreadthFirstSearch.py │ ├── Dijkstra.py │ ├── Dinic.py │ ├── DisjointSetUnion.py │ ├── Edmonds-Karp_Algorithm.py │ ├── Fleury_Algorithm.py │ ├── Ford-Fulkerson_Algorithm.py │ ├── Hierholzer_Algorithm.py │ ├── Hopcroft-Karp algorithm.py │ ├── Kruskals.py │ ├── MultiSource_Shortest_Path.py │ ├── README.md │ ├── StoerWagner.py │ ├── Tarjan.py │ ├── dial_algorithm.py │ ├── floyd_warshall.py │ ├── multistage_graph.py │ └── prims_algorithm.py ├── Greedy-Algorithm │ ├── Activity_Selection.py │ ├── Egyptian_Fraction.py │ ├── Merge_Interval.py │ ├── Optimal_Merge_pattern.py │ ├── README.md │ ├── fractional_knapsack.py │ └── job_sequencing.py ├── Hashing │ ├── README.md │ ├── bloom_filter.py │ └── cuckoo_hashing.py ├── Image Processing │ ├── Canny Edge Detection Algorithm │ │ ├── cannyEdgeDetection.py │ │ └── dog.jpeg │ └── README.md ├── Job Scheduling │ ├── First_Come_First_Serve.py │ ├── Highest_Response_Ratio_Next.py │ ├── README.md │ ├── RoundRobin.py │ ├── Shortest_Job_First.py │ └── priority_scheduling.py ├── Linked-List │ ├── AdditionOfTwoNumbersLL.py │ ├── IntersectionOfLinkedList.py │ ├── LRUCacheDesign.py │ ├── MergeKSortedLists.py │ ├── MergeSortLinkedList.py │ ├── Reverse a singly linked list.py │ ├── Reverse in group of K.py │ ├── SinglyLinkedList.py │ ├── circular_linked_list.py │ ├── doubly_linked_list.py │ ├── linklist_length.py │ ├── mergeTwoSortedLL.py │ └── palindrome.py ├── Machine Learning │ ├── Decision Tree │ │ ├── PlayTennis.csv │ │ └── decision_tree.py │ ├── K Means Clustering │ │ └── kMeansClustering.py │ ├── Linear-Regression │ │ ├── README.md │ │ ├── demo_datasets │ │ │ ├── demo2d.csv │ │ │ └── demo3d.csv │ │ ├── linear_regression.py │ │ ├── plots │ │ │ ├── demo2d_plot.png │ │ │ └── demo3d_plot.png │ │ └── requirements.txt │ ├── Logistic-Regression │ │ ├── README.md │ │ ├── datasets │ │ │ ├── demo_1.csv │ │ │ └── demo_2.csv │ │ ├── logistic_regression.py │ │ └── requirements.txt │ ├── Naive Bayes Classifier │ │ └── NaiveBayesClassifier.py │ └── knn.py ├── Maths │ ├── Bentley_Ottmann_Algorithm.py │ ├── Binary_expo.py │ ├── Binet-Formula-for-Fibonacci-Sequence.py │ ├── BinomialTheorem.py │ ├── Chinese_remainder.py │ ├── Chudnovsky.py │ ├── CongruencyOfTriangles.py │ ├── Extended_Euclidean_Algorithm.py │ ├── GCD and LCM.py │ ├── Interquartile_range.py │ ├── Lucas_Lehmer_Series.py │ ├── Luhn algorithm.py │ ├── Manhattan_distance.py │ ├── ModularExponentiation.py │ ├── Pascal_Triangle.py │ ├── Power_Set.py │ ├── QuickHull Algorithm.py │ ├── README.md │ ├── Roman_to_Decimal.py │ ├── RussianPeasant.py │ ├── ShriDharacharya.py │ ├── SimilarTriangles.py │ ├── Strong_Numbers.py │ ├── armstrong.py │ ├── bin_to_dec_&_dec_to_bin.py │ ├── bisection.py │ ├── closures.py │ ├── factorial.py │ ├── factors.py │ ├── freivald.py │ ├── heronsFormula.py │ ├── is_prime.py │ ├── josephus.py │ ├── perfect.py │ ├── prefix_infix_postfix_interconversion.py │ ├── reverse_integer.py │ ├── segmentUnionLength.py │ ├── sieve_of_eratosthenes.py │ ├── sudoku.py │ └── zeller.py ├── Memory Allocation │ ├── BestFitFixedMemory.py │ ├── NextFitFixedMemory.py │ ├── README.md │ └── WorstFitFixedMemory.py ├── Optimization │ ├── Gradient_Descent.py │ ├── Hill_climbing.py │ ├── README.md │ ├── aco_optimization_py.py │ ├── genetic.py │ ├── greywolf.py │ ├── particle_swarm_optimization.py │ ├── shuffled_frog_leaping_optimization.py │ └── simulated_annealing.py ├── Pattern-Matching │ ├── AhoCorasickAlgorithm.py │ ├── Boyer-moore.py │ ├── README.md │ ├── Z_Algorithm.py │ ├── kmp_string_matching.py │ └── rabin_karp.py ├── README.md ├── Recursion │ ├── Permutations.py │ ├── README.md │ └── TowerOfHanoi.py ├── Searching │ ├── Binary_search_2D_matrix.py │ ├── Exponential_search.py │ ├── Jump_Search.py │ ├── Order_Agnostic_Binary_Search.py │ ├── README.md │ ├── Ternary-Search.py │ ├── binary_search.py │ ├── fibonacci_search.py │ ├── interpolation_search.py │ ├── linear_search.py │ ├── quick_select.py │ └── sublist_search.py ├── Signal Processing │ └── README.md ├── Sliding Window │ └── README.md ├── Sorting │ ├── Bead_sort.py │ ├── Bidirectional_Selection_Sort.py │ ├── BogoSortIn_python.py │ ├── Bucket_Sort.py │ ├── CocktailSort.py │ ├── Gnome_Sorting.py │ ├── Odd_Even_Sort.py │ ├── README.md │ ├── StoogeSort.py │ ├── Tim_sort.py │ ├── Triplet.py │ ├── bingo_sort.py │ ├── bubble_sort.py │ ├── cocktail_sort.py │ ├── comb_sort.py │ ├── counting_sort.py │ ├── cyclic_sort.py │ ├── dutch_national_flag_algorithm.py │ ├── heap_sort.py │ ├── insertion_sort.py │ ├── intro_sort.py │ ├── merge_sort.py │ ├── pancake_sort.py │ ├── pigeonhole sort.py │ ├── quick_sort.py │ ├── radixSort.py │ ├── randomized_quicksort.py │ ├── selection_sort.py │ ├── shell_sort.py │ └── wave_sort.py ├── Stack │ ├── Max_Area_Histogram.py │ ├── NGE_Using_stack.py │ ├── README.md │ ├── Rain_Water.py │ └── stack_using_priority_queue.py ├── String │ ├── Beautiful_string.py │ ├── Camel_Case.py │ └── Manachers_Algorithm.py ├── Sub-Arrays │ ├── Kadane_Algorithm.py │ ├── README.md │ ├── Unsortd_Array.py │ └── boyer_moore_majority_vote.py ├── Trees │ ├── AVLTree.py │ ├── BPlusTree.py │ ├── BinaryTreeToBST.py │ ├── Binary_Tree_View.py │ ├── Fenwick Tree.py │ ├── MinIndexedDHeap.py │ ├── MorrisTraversal.py │ ├── README.md │ ├── SegmentTree.py │ ├── Symmetric_Tree.py │ ├── TernarySearchTree.py │ ├── Van Emde Boas.py │ ├── aostar.py │ ├── red_black.py │ ├── splay_tree.py │ ├── treap_algo.py │ ├── tree_traversals.py │ └── trie.py └── arrays │ ├── Diagonal_Printing_in_2D_arrays.py │ ├── Fisher_Yates_Shuffle_Algorithm.py │ ├── README.md │ ├── Spiral_Print_Algorithm.py │ └── monotonic_array.py └── README.md /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: If you want to include a new Algorithm 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## 🛠️ Issue (Number) 4 | 5 | 6 | Issue no # 7 | 8 | ## 👨‍💻 Changes proposed 9 | 10 | 11 | 12 | ## ✔️ Check List (Check all the applicable boxes) 13 | 14 | - [ ] My code follows the code style of this project. 15 | - [ ] This PR does not contain plagiarized content. 16 | - [ ] The title of my pull request is a short description of the requested changes. 17 | 18 | ## 📄 Note to reviewers 19 | 20 | 21 | 22 | ## 📷 Screenshots -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Basic set up for three package managers 2 | 3 | version: 2 4 | updates: 5 | 6 | # Maintain dependencies for GitHub Actions 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | 12 | # Maintain dependencies for npm 13 | - package-ecosystem: "npm" 14 | directory: "/" 15 | schedule: 16 | interval: "weekly" 17 | 18 | # Maintain dependencies for Composer 19 | - package-ecosystem: "composer" 20 | directory: "/" 21 | schedule: 22 | interval: "weekly" 23 | 24 | -------------------------------------------------------------------------------- /.github/workflows/Auto_message_on_pr_open.yml: -------------------------------------------------------------------------------- 1 | name: Auto message on pr opened 2 | 3 | on: 4 | pull_request_target: 5 | types: [opened] 6 | 7 | jobs: 8 | auto-response: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: derekprior/add-autoresponse@master 13 | env: 14 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 15 | with: 16 | respondableId: ${{ github.event.pull_request.node_id }} 17 | response: "Thanks for your contribution we will soon check your PR @${{ github.event.pull_request.user.login }} :)" 18 | author: ${{ github.event.pull_request.user.login }} 19 | exemptedAuthors: "Kumar-laxmi" 20 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: configure 17 | run: ./configure 18 | - name: make 19 | run: make 20 | - name: make check 21 | run: make check 22 | - name: make distcheck 23 | run: make distcheck 24 | -------------------------------------------------------------------------------- /.github/workflows/codesee-arch-diagram.yml: -------------------------------------------------------------------------------- 1 | # This workflow was added by CodeSee. Learn more at https://codesee.io/ 2 | # This is v2.0 of this workflow file 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request_target: 8 | types: [opened, synchronize, reopened] 9 | 10 | name: CodeSee 11 | 12 | permissions: read-all 13 | 14 | jobs: 15 | codesee: 16 | runs-on: ubuntu-latest 17 | continue-on-error: true 18 | name: Analyze the repo with CodeSee 19 | steps: 20 | - uses: Codesee-io/codesee-action@v2 21 | with: 22 | codesee-token: ${{ secrets.CODESEE_ARCH_DIAG_API_TOKEN }} 23 | -------------------------------------------------------------------------------- /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request_target, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | permissions: 9 | issues: write 10 | pull-requests: write 11 | steps: 12 | - uses: actions/first-interaction@v1 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | issue-message: "We appreciate you pointing us to the problem, and as soon as time allows, we will look into it. Thank you!" 16 | pr-message: "We appreciate you creating your first pull request and look forward to reviewing your future contributions." 17 | -------------------------------------------------------------------------------- /.github/workflows/label.yml: -------------------------------------------------------------------------------- 1 | # This workflow will triage pull requests and apply a label based on the 2 | # paths that are modified in the pull request. 3 | # 4 | # To use this workflow, you will need to set up a .github/labeler.yml 5 | # file with configuration. For more information, see: 6 | # https://github.com/actions/labeler 7 | 8 | name: Labeler 9 | on: [pull_request_target] 10 | 11 | jobs: 12 | label: 13 | 14 | runs-on: ubuntu-latest 15 | permissions: 16 | contents: read 17 | pull-requests: write 18 | 19 | steps: 20 | - uses: actions/labeler@v4 21 | with: 22 | repo-token: "${{ secrets.GITHUB_TOKEN }}" 23 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | # This workflow warns and then closes issues and PRs that have had no activity for a specified amount of time. 2 | # 3 | # You can adjust the behavior by modifying this file. 4 | # For more information, see: 5 | # https://github.com/actions/stale 6 | name: Mark stale issues and pull requests 7 | 8 | on: 9 | schedule: 10 | - cron: '42 16 * * *' 11 | 12 | jobs: 13 | stale: 14 | 15 | runs-on: ubuntu-latest 16 | permissions: 17 | issues: write 18 | pull-requests: write 19 | 20 | steps: 21 | - uses: actions/stale@v5 22 | with: 23 | repo-token: ${{ secrets.GITHUB_TOKEN }} 24 | stale-issue-message: 'This issue has been automatically closed because it has been inactive for many days. Please reopen if you still intend on working on this problem' 25 | stale-pr-message: 'This pull request has been automatically closed because it has been inactive for many days. Please reopen if you still intend to submit this pull request.' 26 | stale-issue-label: 'no-issue-activity' 27 | stale-pr-label: 'no-pr-activity' 28 | -------------------------------------------------------------------------------- /C++/Artificial Intelligence/README.md: -------------------------------------------------------------------------------- 1 | # Artificial Intelligence -------------------------------------------------------------------------------- /C++/Automata/README.md: -------------------------------------------------------------------------------- 1 | # Automata Theory -------------------------------------------------------------------------------- /C++/Binary/Brain_Keringham.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Brian Kerningham's Algorithm in CPP 4 | int setBits(int n) 5 | { 6 | int count = 0; 7 | while (n) { 8 | n = n & (n - 1); 9 | count++; 10 | } 11 | return count; 12 | } 13 | 14 | int main() { 15 | std::cout << setBits(15); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /C++/Binary/hammingDistance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | /* 5 | XOR TABLE 6 | X Y Z 7 | 0 0 0 8 | 0 1 1 9 | 1 0 1 10 | 1 1 0 11 | */ 12 | int x = 0, y =0, count=0; // taking two values as test cases 13 | cout << "Enter value of X: "; 14 | cin >> x; 15 | cout << "Enter value of Y: "; 16 | cin >> y; 17 | int res = x ^ y ; // taking xor of the two values to get different bits 18 | 19 | // counting number of 1 present in the res 20 | while(res>0){ 21 | count += res & 1; 22 | res >>= 1; 23 | } 24 | cout << "The result is: " << count; // printing the value of bits 25 | return 0; 26 | } -------------------------------------------------------------------------------- /C++/Caching/README.md: -------------------------------------------------------------------------------- 1 | # Caching -------------------------------------------------------------------------------- /C++/Concurrent Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Concurrent Algorithms -------------------------------------------------------------------------------- /C++/Cryptography/README.md: -------------------------------------------------------------------------------- 1 | # Cryptography 2 | -------------------------------------------------------------------------------- /C++/Cryptography/RSA_Algorithm.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/C++/Cryptography/RSA_Algorithm.cpp -------------------------------------------------------------------------------- /C++/Data Compression/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/210194962-3fadb785-aa6d-4c17-8bbe-832d27fcd48a.png) 2 | -------------------------------------------------------------------------------- /C++/Deadlock Avoidance/README.md: -------------------------------------------------------------------------------- 1 | # Deadlock Avoidance -------------------------------------------------------------------------------- /C++/Divide-and-Conquer/README.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer -------------------------------------------------------------------------------- /C++/Dynamic-Programming/Fibonacci_Number.cpp: -------------------------------------------------------------------------------- 1 | // Author : K.om Senapati 2 | // Github username : KOMNOOB 3 | // Problem Statement: Fibonacci Number 4 | // Problem Description 5 | // The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. Given an integer n, your task is to find the nth Fibonacci number. 6 | 7 | // Write a function fibonacci(n) that takes in an integer n and returns the nth Fibonacci number. 8 | 9 | // Input : 10 | // An integer n representing the position of the Fibonacci number to be found. (0 <= n <= 30) 11 | // Output : 12 | // An integer representing the nth Fibonacci number. 13 | 14 | #include 15 | using std::cout, std::cin; 16 | 17 | int fibonacci(int n) 18 | { 19 | if (n <= 0) 20 | { 21 | cout << "n must be higher than 0\n"; 22 | return -1; 23 | } 24 | else if (n == 1 || n == 2) 25 | return 1; 26 | 27 | int memo[n + 1]; 28 | memo[0] = 0; 29 | memo[1] = 1; 30 | 31 | for (int i = 2; i <= n; i++) 32 | { 33 | memo[i] = memo[i - 1] + memo[i - 2]; 34 | } 35 | return memo[n]; 36 | } 37 | 38 | int main() 39 | { 40 | cout << "Enter the n value : "; 41 | int n; 42 | cin >> n; 43 | int fib = fibonacci(n); 44 | cout << "\nThe fibonacci number at position n = " << fib; 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /C++/Dynamic-Programming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming -------------------------------------------------------------------------------- /C++/Game Theory/README.md: -------------------------------------------------------------------------------- 1 | # Game Theory -------------------------------------------------------------------------------- /C++/Graphics/README.md: -------------------------------------------------------------------------------- 1 | # Computer Graphics -------------------------------------------------------------------------------- /C++/Graphs/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198796797-2d6831bf-8142-4a4c-af39-20e41877e16e.png) 2 | -------------------------------------------------------------------------------- /C++/Graphs/floyd_warshall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void floyd_warshall(int a[][7]) 5 | { 6 | int i, j, k; 7 | for (k = 0; k < 7; k++) 8 | { 9 | for (i = 0; i < 7; i++) 10 | { 11 | for (j = 0; j < 7; j++) 12 | { 13 | if ((a[i][k] * a[k][j] != 0) && (i != j)) 14 | { 15 | if ((a[i][k] + a[k][j] < a[i][j]) || (a[i][j] == 0)) 16 | { 17 | a[i][j] = a[i][k] + a[k][j]; 18 | } 19 | } 20 | } 21 | } 22 | } 23 | for (i = 0; i < 7; i++) 24 | { 25 | cout << "\nMINIMUM COST WITH RESPECT TO NODE:" << i << endl; 26 | for (j = 0; j < 7; j++) 27 | { 28 | cout << a[i][j] << "\t"; 29 | } 30 | } 31 | } 32 | int main() 33 | { 34 | int a[7][7]; 35 | cout << "ENTER VALUES OF ADJACENCY MATRIX\n\n"; 36 | for (int i = 0; i < 7; i++) 37 | { 38 | cout << "ENTER THE VALUES FOR " << (i + 1) << " ROW" << endl; 39 | for (int j = 0; j < 7; j++) 40 | { 41 | cin >> a[i][j]; 42 | } 43 | } 44 | floyd_warshall(a); 45 | getch(); 46 | } -------------------------------------------------------------------------------- /C++/Greedy-Algorithm/Activity_process.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int s,e; 10 | vector>activity;//Pair of end time,start time is inserted into vector 11 | for(int i=0;i>s>>e;//Input start time and end time 14 | activity.push_back(make_pair(e,s)); 15 | } 16 | //sort the activities according to their finishing time so that we always consider the next activity as the minimum finishing time activity 17 | sort(activity.begin(),activity.end());//Sorting the activity according to end time (Finishing time) 18 | int current_end=-1; 19 | int count=0; 20 | for(int i=0;icurrent_end) 24 | { 25 | count++; 26 | current_end=activity[i].first; 27 | } 28 | } 29 | cout< 3 | using namespace std; 4 | void egyptian_Fraction(int n, int d) 5 | { 6 | // When Both Numerator and denominator becomes zero then we simply return; 7 | if (d == 0 || n == 0) 8 | return; 9 | if (d % n == 0) 10 | { 11 | cout << "1/" << d / n; 12 | return; 13 | } 14 | if (n % d == 0) 15 | { 16 | cout << n / d; 17 | return; 18 | } 19 | if (n > d) 20 | { 21 | cout << n / d << " + "; 22 | egyptian_Fraction(n % d, d); 23 | return; 24 | } 25 | int x = d / n + 1; 26 | cout << "1/" << x << " + "; 27 | egyptian_Fraction(n * x - d, d * x); 28 | } 29 | int main() 30 | { 31 | int num, den; 32 | cout << "Enter Numerator:"; 33 | cin >> num; 34 | cout << "\nEnter Denominator:"; 35 | cin >> den; 36 | cout << "Egyptian Fraction : " << num << "/" << den << " is" << endl; 37 | egyptian_Fraction(num, den); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /C++/Greedy-Algorithm/README.md: -------------------------------------------------------------------------------- 1 | # Greedy Algorithm -------------------------------------------------------------------------------- /C++/Hashing/README.md: -------------------------------------------------------------------------------- 1 | # Hashing -------------------------------------------------------------------------------- /C++/Image Processing/Canny Edge Detection Algorithm/dog.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/C++/Image Processing/Canny Edge Detection Algorithm/dog.jpeg -------------------------------------------------------------------------------- /C++/Image Processing/README.md: -------------------------------------------------------------------------------- 1 | # Image Processing -------------------------------------------------------------------------------- /C++/Job Scheduling/README.md: -------------------------------------------------------------------------------- 1 | # Job Scheduling Algorithms -------------------------------------------------------------------------------- /C++/Maths/Binary Exponentiation Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | long long int mod = 1e18+7; 4 | //Large Exponentiation 5 | //a^b --> a,b,mod < 10^18 6 | //T.C ->O(log^2(N)) --> O(log(N)*log(N)) 7 | 8 | int binExp(int a , int b){ 9 | int ans=1; 10 | while(b>0){ 11 | //see if set-bit 12 | if(b&1){ 13 | ans=binMultiply(ans,a) ; 14 | } 15 | a = binMultiply(a,a); 16 | b>>=1 ; //right-shift the bit 17 | } 18 | return ans; 19 | } 20 | 21 | // add krte jayenge a ko and mod lete jayenge step by step -> O(n) but for O(logn) use binary technique -> 22 | // a+a+a+a+..... b times 23 | int binMultiply(long long int a , long long int b){ 24 | int ans=0; // as ans get added 25 | while(b>0){ 26 | //see if set-bit than add in ans 27 | if(b&1){ 28 | ans=(ans+a)%mod; 29 | } 30 | a = (a+a)%mod; //go to next power 31 | b>>=1 ; //right-shift the bit 32 | } 33 | return ans; 34 | } 35 | 36 | 37 | int main(){ 38 | int a = 2 , b=13 ; 39 | cout< 2 | #include 3 | 4 | int fact(int n) { 5 | int factn = 1; 6 | for (int i = 1; i < n + 1; i++) { 7 | factn = factn * i; 8 | } 9 | return factn; 10 | } 11 | 12 | int main() { 13 | std::cout << "Enter in (1+x)^n the values of x and n respectively" << std::endl; 14 | int x, n; 15 | std::cin >> x >> n; 16 | int nf = fact(n); 17 | int arr[n + 1]; 18 | 19 | for (int i = 0; i <= n; i++) { 20 | int ncr = nf / (fact(i) * fact(n - i)); 21 | arr[i] = ncr * pow(x, i); 22 | arr[n - i] = ncr * pow(x, n - i); 23 | } 24 | 25 | for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { 26 | std::cout << arr[i] << " "; 27 | } 28 | std::cout << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /C++/Maths/Extended_Eucledian_Algorithm.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/C++/Maths/Extended_Eucledian_Algorithm.cpp -------------------------------------------------------------------------------- /C++/Maths/Is_prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mod=1e9+7; 5 | 6 | void prime_checker1(int number){ 7 | /* 8 | What we will do here is we will iterate from 2 till number -1 9 | and check if the number is divisible by any of this. If it is then it is not 10 | a prime number 11 | */ 12 | 13 | for(int i=2;i>number; 42 | 43 | //CHECK PRIME 44 | 45 | // 1) O(N) complexity 46 | 47 | prime_checker1(number); 48 | 49 | // 2) O(sqrt(N)) complexity 50 | 51 | prime_checker2(number); 52 | } -------------------------------------------------------------------------------- /C++/Maths/Modular_exponentiation.cpp: -------------------------------------------------------------------------------- 1 | /* C++ Program for Modular Exponentiation */ 2 | #include 3 | using namespace std; 4 | 5 | // Function to calculate (x^y)%p 6 | int power(int x, int y, int p) 7 | { 8 | // Initialize answer 9 | int ans = 1; 10 | 11 | // Check till the number becomes zero 12 | while (y > 0) 13 | { 14 | // If y is odd, multiply x with answer 15 | if (y % 2 == 1) 16 | { 17 | ans = ans * x; 18 | } 19 | 20 | // Change y to half 21 | y = y / 2; 22 | 23 | // Change x to x^2 24 | x = x * x; 25 | } 26 | return ans % p; 27 | } 28 | 29 | int main() 30 | { 31 | int x, y, p; 32 | cin >> x >> y >> p; // Taking input of x, y and p 33 | 34 | // Function Call 35 | cout << "Power is " << power(x, y, p); 36 | return 0; 37 | } 38 | 39 | // Time Complexity:- O(log y) 40 | // Space Complexity:- O(1) -------------------------------------------------------------------------------- /C++/Maths/Pascal_Triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 1 4 | 1 2 1 5 | 1 3 3 1 6 | 1 4 6 4 1 7 | 8 | Each number in the triangle is the sum of the two numbers directly above it. 9 | 10 | we take number of rows as input 11 | And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr. 12 | nCr=factorial(n)/(factorial(r)*factorial(n-r)) 13 | */ 14 | #include 15 | using namespace std; 16 | 17 | //function to find the factorial. 18 | long factorial(int n) 19 | { 20 | int c; 21 | long result = 1; 22 | 23 | for (c = 1; c <= n; c++) 24 | result = result*c; 25 | 26 | return result; 27 | } 28 | 29 | int main() 30 | { 31 | int i, n, c; 32 | 33 | cout<<"Enter the number of rows you wish to see in pascal triangle:\n"; 34 | cin>>n; 35 | 36 | for (i = 0; i < n; i++) 37 | { 38 | for (c = 0; c <= (n - i - 2); c++) 39 | cout<<" "; 40 | for (c = 0 ; c <= i; c++) 41 | cout<< factorial(i)/(factorial(c)*factorial(i-c)); 42 | 43 | cout< 2 | using namespace std; 3 | // Function to multiply two numbers using Russian Peasant method 4 | int russianPeasant(int a, int b) { 5 | // initialize result 6 | int res = 0; 7 | 8 | // While second number doesn't become 1 9 | while (b > 0) { 10 | // If second number becomes odd, 11 | // add the first number to result 12 | if ((b & 1) != 0) 13 | res = res + a; 14 | 15 | // Double the first number and halve the second number 16 | a = a << 1; 17 | b = b >> 1; 18 | } 19 | return res; 20 | } 21 | // Driver program 22 | int main() { 23 | int a, b; 24 | cout << "Enter two numbers: "; 25 | cin >> a >> b; 26 | cout << russianPeasant(a, b) << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /C++/Maths/Sieve of Eratosthenes Algorithm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Time complexity of Sieve of Eratosthenes is n*log(log(n)) 3 | It is one of the most efficient ways to find all prime numbers. 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | #define int long long 9 | 10 | //declared globally 11 | int N = 1e5; 12 | //sieve for prime numbers. 13 | vector sieve; 14 | vectorisprime(N,1); 15 | 16 | void sieveOfEratosthenes() { 17 | for(int p=2; p*p<=N; p++){ 18 | if(isprime[p]==1) { 19 | for(int i=p*p; i<=N; i+=p){ 20 | isprime[i]=0; 21 | } 22 | } 23 | } 24 | for(int p=2; p<=N; p++) if(isprime[p]) { 25 | sieve.push_back(p); 26 | } 27 | } 28 | signed main(){ 29 | int n ; 30 | cin>>n; 31 | cout<<"First "< 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | // SSS property 11 | int a1 = 3, b1 = 4, c1 = 5; 12 | int a2 = 6, b2 = 8, c2 = 10; 13 | if (a1 * b2 == a2 * b1 && b1 * c2 == b2 * c1 && a1 * c2 == a2 * c1) { 14 | cout << "The triangles are similar" << endl; 15 | } else { 16 | cout << "The triangles are not similar" << endl; 17 | } 18 | 19 | // AA property 20 | int x1 = 40, x2 = 70; 21 | int y1 = 40, y2 = 70; 22 | if (x1 == y1 && x2 == y2) { 23 | cout << "The triangles are similar" << endl; 24 | } else { 25 | cout << "The triangles are not similar" << endl; 26 | } 27 | 28 | // SAS property 29 | int a3 = 3, m = 50, b3 = 4; 30 | int a4 = 6, n = 50, b4 = 8; 31 | if (a3 * b4 == a4 * b3 && m == n) { 32 | cout << "The triangles are similar" << endl; 33 | } else { 34 | cout << "The triangles are not similar" << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /C++/Maths/Strong_Number.cpp: -------------------------------------------------------------------------------- 1 | /*Name : Hardik Goyal 2 | Github username : HardikGoyal2003 3 | Repositary name : Algorithms 4 | */ 5 | /*What is a strong number? 6 | A strong number is a positive integer whose sum of factorials of its digits equals the number itself. In other words, if we take the 7 | digits of a number and calculate the factorial of each digit, then sum those factorials, and the result is the same as the original number, 8 | then that number is called a strong number. 9 | 10 | Problem Statement : To Check Given number is Strong Number or not. 11 | EXAMPLE : 12 | 1) 13 | Input: 145 14 | Output: "It is a Strong Number" 15 | */ 16 | #include 17 | using namespace std; 18 | int main(){ 19 | int n,factorial=1,sum=0; 20 | cout<<"Enter Number"; 21 | cin>>n; 22 | int n1= n; 23 | while (n>0) 24 | { 25 | int rem = n%10; 26 | n/=10; 27 | for (int i=1; i <=rem; i++) 28 | { 29 | factorial*=i; 30 | } 31 | sum+=factorial; 32 | factorial=1; 33 | } 34 | if (n1 == sum) 35 | { 36 | cout<<"It is Strong Number"; 37 | } 38 | else 39 | { 40 | cout<<"It is not a Strong Number"; 41 | } 42 | } -------------------------------------------------------------------------------- /C++/Maths/bisection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //x^{2}+x-1 5 | float f(float x)//function 6 | { 7 | return x*x*x - 4*x - 9; 8 | } 9 | int main() 10 | { 11 | float L = 2; 12 | float R = 3;//range 13 | float M = (L+R)/2; 14 | int iterations = 20; 15 | float threshold = .00001; 16 | 17 | while(iterations--) 18 | { 19 | M = (L+R)/2; 20 | if(f(L) * f(M) < 0)//root is b/w (L,M) 21 | { 22 | R = M; 23 | cout<<"iteration "<<20-iterations<<": "< 12 | using namespace std; 13 | 14 | int factorial(int num){ 15 | if(num == 0 || num == 1) // factorial of 0 and 1 is 1 16 | return 1; 17 | else // if num > 1 18 | return (num * factorial(num - 1)); 19 | } 20 | 21 | int main() 22 | { 23 | int num; 24 | long int res = 1; 25 | 26 | cout << "Enter a number: "; 27 | cin >> num; // factorial of this no will be calculated 28 | 29 | // calling the function 30 | res = factorial(num); 31 | 32 | cout << "Factorial of " << num << " is " << res; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /C++/Maths/heronsFormula.cpp: -------------------------------------------------------------------------------- 1 | //This code takes input of three sides of a traingle from the user and returns it's are which is calculated using Heron's Formula. In symbols, if a, b, and c are the lengths of the sides: Area = Square root of√s(s - a)(s - b)(s - c) where s is half the perimeter, or (a + b + c)/2. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | double s1, s2, s3, area, S; 9 | cout << "Enter Three Sides of a Triangle" << endl; 10 | cin >> s1 >> s2 >> s3; 11 | 12 | S = (s1 + s2 + s3) / 2; 13 | area = sqrt(S * (S - s1) * (S - s2) * (S - s3)); 14 | 15 | cout << "The Area of triangle = " << area << endl; 16 | } 17 | -------------------------------------------------------------------------------- /C++/Maths/perfect.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to check if the number is a Perfect number or not 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main () 7 | { 8 | // initialize variable for loop, input number and to store sum 9 | int i, num, sum=0; 10 | 11 | // take user input 12 | cout << "Enter the number: "; 13 | cin >> num; 14 | 15 | // iterate from 1 to N and if the number is a factor of N then add it to sum 16 | for (i=1; i < num; i++) 17 | { 18 | int div = num % i; 19 | if (div == 0) 20 | sum = sum + i; 21 | } 22 | 23 | // if sum of factors of N is equal to N then it is a perfect number 24 | if (sum == num) 25 | cout << "\n" << num <<" is a perfect number"; 26 | else 27 | cout << "\n" << num <<" is not a perfect number"; 28 | return 0; 29 | } 30 | 31 | /******************************************************* 32 | Case 1: 33 | Enter the number: 153 34 | 153 is not a perfect number 35 | 36 | Case 2: 37 | Enter the number: 496 38 | 496 is a perfect number 39 | *******************************************************/ 40 | -------------------------------------------------------------------------------- /C++/Maths/shriDharacharya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void ShriDharacharyaFormulae(double a, double b, double c) { 5 | double compValue = pow(b, 2) - 4 * a * c; 6 | double val1 = -b / (2 * a); 7 | 8 | if (compValue >= 0) { 9 | double val2 = sqrt(compValue) / (2 * a); 10 | std::cout << val1 + val2 << " and " << val1 - val2 << std::endl; 11 | } else { 12 | std::cout << "Imaginary roots" << std::endl; 13 | } 14 | } 15 | 16 | int main() { 17 | double a, b, c; 18 | std::cin >> a; 19 | std::cin >> b; 20 | std::cin >> c; 21 | ShriDharacharyaFormulae(a, b, c); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /C++/Maths/zeller.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program to Find the Day 3 | // for a Date 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int Zellercongruence(int day, int month, int year) { 10 | if (month == 1) { 11 | month = 13; 12 | year--; 13 | } if (month == 2) { 14 | month = 14; 15 | year--; 16 | } 17 | int q = day; 18 | int m = month; 19 | int k = year % 100; 20 | int j = year / 100; 21 | int h = q + 13 * (m + 1) / 5 + k + k / 4 + 22 | j / 4 + 5 * j; 23 | h = h % 7; 24 | switch (h) { 25 | case 0: 26 | cout << "Saturday \n"; 27 | break; 28 | case 1: 29 | cout << "Sunday \n"; 30 | break; 31 | case 2: 32 | cout << "Monday \n"; 33 | break; 34 | case 3: 35 | cout << "Tuesday \n"; 36 | break; 37 | case 4: 38 | cout << "Wednesday \n"; 39 | break; 40 | case 5: 41 | cout << "Thursday \n"; 42 | break; 43 | case 6: 44 | cout << "Friday \n"; 45 | break; 46 | } 47 | return 0; 48 | } 49 | 50 | // Driver code 51 | int main() { 52 | Zellercongruence(11, 11, 2022); // date (dd/mm/yyyy) 53 | return 0; 54 | } -------------------------------------------------------------------------------- /C++/Memory Allocation/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation -------------------------------------------------------------------------------- /C++/Optimization/README.md: -------------------------------------------------------------------------------- 1 | # Optimization 2 | Grey Wolf Optimisation -------------------------------------------------------------------------------- /C++/Pattern-Matching/README.md: -------------------------------------------------------------------------------- 1 | # Pattern Matching -------------------------------------------------------------------------------- /C++/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198798410-934fdb17-c72e-43e6-acec-cec63dc8d2ea.png) 2 | -------------------------------------------------------------------------------- /C++/Recursion/Permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void permutations(string p, string up) { 6 | // Base case: If the remaining characters to be permuted is empty, print the permutation 7 | if (up.empty()) { 8 | cout << p << endl; 9 | return; 10 | } 11 | 12 | char ch = up[0]; 13 | 14 | for (int i = 0; i <= p.length(); i++) { 15 | // Split the permutation string into two parts 16 | string f = p.substr(0, i); // First part 17 | string s = p.substr(i, p.length()); // Second part 18 | 19 | // Recursive call: Generate permutations by inserting the current character at each possible position 20 | permutations(f + ch + s, up.substr(1)); 21 | } 22 | } 23 | 24 | int main() { 25 | permutations("", "abc"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C++/Recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion -------------------------------------------------------------------------------- /C++/Searching/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198502266-b90e47d9-4f27-46ec-8572-7fa5f53125e9.png) 2 | -------------------------------------------------------------------------------- /C++/Searching/linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | <----------------------------------PROBLEM------------------------------------> 7 | Given an array arr[] of n elements, write a function to search a given element el in arr[]. 8 | 9 | Variables used: 10 | n = total number of elements in array 11 | v = vector (dynamic array initialized with the input size) 12 | el = element to be searched in the array. 13 | */ 14 | 15 | int main() 16 | { 17 | int n; 18 | cin >> n; 19 | vector v(n); 20 | for(int i=0; i> v[i]; 22 | } 23 | 24 | int el; 25 | cin >> el; 26 | 27 | // Flag variable to keep a check if the element is found till then. 28 | bool flag=false; 29 | for(int i=0; i 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int sizeOfArray = 10; 7 | 8 | void RandomShuffle(int *arr, int sizeOfArray) { 9 | for (int i = 0; i < sizeOfArray; i++) { 10 | int j = rand() % sizeOfArray; 11 | int t = arr[i]; 12 | arr[i] = arr[j]; 13 | arr[j] = t; 14 | } 15 | } 16 | 17 | bool is_sorted(int *arr, int sizeOfArray) { 18 | for (int i = 1; i < sizeOfArray; i++) { 19 | if (arr[i] < arr[i - 1]) { 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | 26 | void bogosort(int *arr, int sizeOfArray) { 27 | while (!is_sorted(arr, sizeOfArray)) { 28 | RandomShuffle(arr, sizeOfArray); 29 | } 30 | } 31 | 32 | int main() { 33 | int arr[sizeOfArray]; 34 | for (int i = 0; i < sizeOfArray; i++) { 35 | arr[i] = rand() % 100; 36 | } 37 | 38 | cout << "The unsorted arr is:\t"; 39 | for (int i = 0; i < sizeOfArray; i++) { 40 | cout << arr[i] << " "; 41 | } 42 | cout << endl; 43 | 44 | bogosort(arr, sizeOfArray); 45 | 46 | cout << "The sorted arr is:\t"; 47 | for (int i = 0; i < sizeOfArray; i++) { 48 | cout << arr[i] << " "; 49 | } 50 | cout << endl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /C++/Sorting/Odd_Even_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A function to sort the algorithm using Odd Even sort 5 | void oddEvenSort(int arr[], int n) 6 | { 7 | bool isSorted = false; 8 | 9 | while (!isSorted) 10 | { 11 | isSorted = true; 12 | 13 | // Perform Bubble sort on odd indexed element 14 | for (int i=1; i<=n-2; i=i+2) 15 | { 16 | if (arr[i] > arr[i+1]) 17 | { 18 | swap(arr[i], arr[i+1]); 19 | isSorted = false; 20 | } 21 | } 22 | 23 | // Perform Bubble sort on even indexed element 24 | for (int i=0; i<=n-2; i=i+2) 25 | { 26 | if (arr[i] > arr[i+1]) 27 | { 28 | swap(arr[i], arr[i+1]); 29 | isSorted = false; 30 | } 31 | } 32 | } 33 | 34 | return; 35 | } 36 | 37 | // A utility function to print an array of size n 38 | void printArray(int arr[], int n) 39 | { 40 | for (int i=0; i < n; i++) 41 | cout << arr[i] << " "; 42 | cout << "\n"; 43 | } 44 | 45 | // Driver program to test above functions. 46 | int main() 47 | { 48 | int arr[] = {50, 7, 3, 1, 0, -2, -5}; 49 | int n = sizeof(arr)/sizeof(arr[0]); 50 | 51 | oddEvenSort(arr, n); 52 | printArray(arr, n); 53 | 54 | return (0); 55 | } -------------------------------------------------------------------------------- /C++/Sorting/README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | -------------------------------------------------------------------------------- /C++/Sorting/Wave_sort.cpp: -------------------------------------------------------------------------------- 1 | /*Name : Atul Kumar 2 | Github username : atul1510 3 | Repositary name : Algorithms 4 | */ 5 | #include 6 | using namespace std; 7 | void swap(int arr[], int a, int b) // A utility method to swap two numbers. 8 | 9 | { 10 | int temp = arr[a]; 11 | arr[a] = arr[b]; 12 | arr[b] = temp; 13 | } 14 | void waveSort(int arr[], int n) 15 | { 16 | for (int i = 1; i < n; i+=2) // Traverse all even elements 17 | 18 | { 19 | if(arr[i]>arr[i-1]) swap(arr, i, i-1); // If current even element is smaller than previous 20 | if(arr[i]>arr[i+1] && i<=n-2) swap(arr, i, i+1); // If current even element is smaller than next 21 | } 22 | } 23 | int main() // Driver program to test above function 24 | 25 | { 26 | int arr[5] = {5, 4, 3, 2, 1}; 27 | waveSort(arr, 5); 28 | for (int i = 0; i < 5; i++) { 29 | cout< 2 | #include 3 | using namespace std; 4 | void bingoSort(vector &arr) 5 | { 6 | int min = arr[0], max = arr[0]; 7 | 8 | for (int i = 1; i < arr.size(); i++) 9 | { 10 | if (arr[i] < min) 11 | min = arr[i]; 12 | else if (arr[i] > max) 13 | max = arr[i]; 14 | } 15 | 16 | vector flags(max - min + 1, 0); 17 | 18 | for (int i = 0; i < arr.size(); i++) 19 | flags[arr[i] - min] = 1; 20 | 21 | int index = 0; 22 | for (int i = 0; i < max - min + 1; i++) 23 | { 24 | if (flags[i]) 25 | { 26 | arr[index] = i + min; 27 | index++; 28 | } 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | cout << "Enter the length of the array: "; 35 | int n; 36 | cin >> n; 37 | 38 | vector a(n); 39 | 40 | cout << "Enter the elements of the array:" << endl; 41 | for (int i = 0; i < n; i++) 42 | cin >> a[i]; 43 | 44 | cout << "Original array:" << endl; 45 | for (int i = 0; i < n; i++) 46 | cout << a[i] << " "; 47 | cout << endl; 48 | 49 | bingoSort(a); 50 | 51 | cout << "Sorted array:" << endl; 52 | for (int i = 0; i < n; i++) 53 | cout << a[i] << " "; 54 | cout << endl; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /C++/Sorting/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cout << "ENTER THE SIZE OF ARRAY: : "; 7 | cin >> n; 8 | 9 | int array[n]; 10 | cout << "ENTER THE ELEMENTS: " << endl; 11 | for (int i = 0; i < n; i++) 12 | { 13 | cin >> array[i]; 14 | } 15 | 16 | cout << "UNSORTED ARRAY: " << endl; 17 | for (int j = 0; j < n; j++) 18 | { 19 | cout << array[j] << " "; 20 | } 21 | cout << endl; 22 | 23 | int temp; 24 | for (int i = 0; i <= n - 1; i++) 25 | { 26 | for (int j = 0; j < n - i - 1; j++) 27 | { 28 | 29 | if (array[j] > array[j + 1]) 30 | { 31 | temp = array[j]; 32 | array[j] = array[j + 1]; 33 | array[j + 1] = temp; 34 | } 35 | } 36 | } 37 | 38 | cout << "SORTED ARRAY: " << endl; 39 | for (int i = 0; i < n; i++) 40 | { 41 | cout << array[i] << " "; 42 | } 43 | cout << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C++/Sorting/comb_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int getNextGap(int gap) 4 | { 5 | gap = (gap*10)/13; 6 | if (gap < 1) 7 | return 1; 8 | return gap; 9 | } 10 | void combSort(int a[], int n) 11 | { 12 | int gap = n; 13 | bool swapped = true; 14 | while (gap != 1 || swapped == true) 15 | { 16 | gap = getNextGap(gap); 17 | swapped = false; 18 | for (int i=0; i a[i+gap]) 21 | { 22 | swap(a[i], a[i+gap]); 23 | swapped = true; 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | cout << "Enter the length of array" << endl; 32 | int n; 33 | cin >> n; 34 | int *a = new int(n); 35 | // Getting elements of array 36 | cout << "Enter the elements of array" << endl; 37 | for (int i = 0; i < n; i++) 38 | cin >> a[i]; 39 | cout << "Original array: \n"; 40 | for (int i = 0; i < n; i++) 41 | cout << a[i] << " "; 42 | combSort(a, n); 43 | 44 | cout << "\nSorted array: \n"; 45 | for (int i = 0; i < n; i++) 46 | cout << a[i] << " "; 47 | delete (a); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /C++/Sorting/counting_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | // complexity time - O(n+ k), space - O(n + k) 5 | // used as subroutine in radix sort 6 | //Not a comparison based algorithm 7 | void countSort(int a[],int n,int k){ 8 | int count[k]; 9 | for(int i = 0;i =0;i--){ 19 | output[count[a[i]] - 1] = a[i]; 20 | count[a[i]]--; 21 | } 22 | for(int i = 0;i < n ;i++) 23 | a[i] = output[i]; 24 | } 25 | 26 | 27 | 28 | 29 | int main() 30 | { 31 | int a[]={50,10,20,80,5,22,22},k = INT_MIN; 32 | for(int i : a) 33 | k = max(k,i); 34 | int n = sizeof(a)/sizeof(int); 35 | countSort(a,n,k+1); 36 | for(int e:a) 37 | cout< 2 | 3 | using namespace std; 4 | 5 | void cycleSort(vector a) 6 | { 7 | for (int i = 0; i < a.size() - 1; i++) 8 | { 9 | int item = a[i]; 10 | int pos = i; 11 | for (int j = i + 1; j < a.size(); j++) 12 | if (a[j] < item) 13 | pos++; 14 | 15 | swap(item, a[pos]); 16 | 17 | while (pos != i) 18 | { 19 | pos = i; 20 | for (int j = i + 1; j < a.size(); j++) 21 | if (a[j] < item) 22 | pos++; 23 | 24 | swap(item, a[pos]); 25 | } 26 | } 27 | 28 | for (int e : a) 29 | cout << e << " "; 30 | } 31 | 32 | int main() 33 | { 34 | vector a = {20, 10, 50, 16, 30}; 35 | cycleSort(a); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /C++/Sorting/depth_first_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Graph { 6 | int numVertices; 7 | list *adjLists; 8 | bool *visited; 9 | 10 | public: 11 | Graph(int V); 12 | void addEdge(int src, int dest); 13 | void DFS(int vertex); 14 | }; 15 | 16 | // graph 17 | Graph::Graph(int vertices) { 18 | numVertices = vertices; 19 | adjLists = new list[vertices]; 20 | visited = new bool[vertices]; 21 | } 22 | 23 | // edges 24 | void Graph::addEdge(int src, int dest) { 25 | adjLists[src].push_front(dest); 26 | } 27 | 28 | // algorithm 29 | void Graph::DFS(int vertex) { 30 | visited[vertex] = true; 31 | list adjList = adjLists[vertex]; 32 | 33 | cout << vertex << " "; 34 | 35 | list::iterator i; 36 | for (i = adjList.begin(); i != adjList.end(); ++i) 37 | if (!visited[*i]) 38 | DFS(*i); 39 | } 40 | 41 | int main() { 42 | Graph g(4); 43 | g.addEdge(0, 1); 44 | g.addEdge(0, 2); 45 | g.addEdge(1, 2); 46 | g.addEdge(2, 3); 47 | 48 | g.DFS(2); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /C++/Sorting/dutch_national_flag_algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void sort012(vector &nums, int n) 4 | { 5 | int l = 0, m = 0, h = n-1; 6 | while(m <= h) 7 | { 8 | if(nums[m] == 0) 9 | { 10 | swap(nums[l], nums[m]); 11 | l++; 12 | m++; 13 | } 14 | else if(nums[m] == 1) 15 | { 16 | m++; 17 | } 18 | else 19 | { 20 | swap(nums[h], nums[m]); 21 | h--; 22 | } 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | vector nums(n); 31 | for(int i = 0; i < n; i++) 32 | { 33 | cin>>nums[i]; 34 | } 35 | sort012(nums, n); 36 | for(int i = 0; i < n; i++) 37 | { 38 | cout< 2 | using namespace std; 3 | 4 | void heapify(int *ar, int n, int i) { 5 | int max = i, leftChild = 2 * i + 1, rightChild = 2 * i + 2; 6 | // left child greater than root 7 | if(leftChild < n && ar[leftChild] > ar[max]) max = leftChild; 8 | // right child greater than root 9 | if(rightChild < n && ar[rightChild] > ar[max]) max = rightChild; 10 | 11 | // swap and heapify until max ele not found 12 | if(max != i) { 13 | swap(ar[i], ar[max]); 14 | heapify(ar, n, max); 15 | } 16 | } 17 | 18 | void heapSort(int *ar, int n) { 19 | // max heapify 20 | for(int i = n / 2 - 1; i >= 0; i--) heapify(ar, n, i); 21 | // heap sort 22 | for(int i = n - 1; i >= 0; i--) { 23 | swap(ar[0], ar[i]); 24 | // heapify root element 25 | heapify(ar, i, 0); 26 | } 27 | } 28 | 29 | int main() { 30 | // input code 31 | int n; 32 | printf("HEAP SORT\nEnter array size : "); 33 | cin >> n; 34 | int ar[n]; 35 | printf("\nEnter array : "); 36 | for(int i = 0; i < n; i++) scanf("%d", &ar[i]); 37 | 38 | // function call 39 | heapSort(ar, n); 40 | // display sorted array 41 | printf("\nSORTED ARRAY : "); 42 | for(int i : ar) cout << i << " "; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /C++/Sorting/insertion_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void Sort(int a[], int n) 7 | { 8 | int i, key, j; 9 | for (i = 1; i < n; i++) 10 | { 11 | key = a[i]; 12 | j = i - 1; 13 | 14 | while (j >= 0 && a[j] > key) 15 | { 16 | a[j + 1] = a[j]; 17 | j = j - 1; 18 | } 19 | a[j + 1] = key; 20 | } 21 | 22 | for (int i = 0; i < n; i++) 23 | cout< 2 | #include 3 | #include 4 | 5 | void pigeonholeSort(std::vector& arr) { 6 | int min = arr[0], max = arr[0]; 7 | for (int i = 1; i < arr.size(); i++) { 8 | if (arr[i] < min) { 9 | min = arr[i]; 10 | } 11 | if (arr[i] > max) { 12 | max = arr[i]; 13 | } 14 | } 15 | 16 | int range = max - min + 1; 17 | std::vector pigeonholes(range, 0); 18 | 19 | for (int i = 0; i < arr.size(); i++) { 20 | pigeonholes[arr[i] - min]++; 21 | } 22 | 23 | int index = 0; 24 | for (int i = 0; i < range; i++) { 25 | while (pigeonholes[i] > 0) { 26 | arr[index++] = i + min; 27 | pigeonholes[i]--; 28 | } 29 | } 30 | } 31 | 32 | int main() { 33 | int n; 34 | std::cout << "Enter the number of elements: "; 35 | std::cin >> n; 36 | 37 | std::vector arr(n); 38 | std::cout << "Enter the elements:" << std::endl; 39 | for (int i = 0; i < n; i++) { 40 | std::cin >> arr[i]; 41 | } 42 | 43 | pigeonholeSort(arr); 44 | 45 | std::cout << "Sorted array: "; 46 | for (int i = 0; i < n; i++) { 47 | std::cout << arr[i] << " "; 48 | } 49 | std::cout << std::endl; 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /C++/Sorting/quick_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int hPartition(int a[], int low, int high) 5 | { 6 | int pivot = a[low]; 7 | int i = low - 1; 8 | int j = high + 1; 9 | while (true) 10 | { 11 | do 12 | { 13 | i++; 14 | } while (a[i] < pivot); 15 | do 16 | { 17 | j--; 18 | } while (a[j] > pivot); 19 | 20 | if (i >= j) 21 | return j; 22 | 23 | swap(a[i], a[j]); 24 | } 25 | } 26 | 27 | void quickSort(int a[],int l,int h){ 28 | if(l 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int Partition(int arr[],int p,int r){ 9 | int x=arr[r]; //pivot element 10 | int i=p-1; 11 | int j; 12 | for(int j=p;j<=r-1;j++){ 13 | if(arr[j]<=x){ 14 | i++; 15 | swap(arr[i],arr[j]); 16 | } 17 | } 18 | swap(arr[i+1],arr[r]); 19 | 20 | return i+1; 21 | } 22 | int Randomized_partition(int arr[],int p,int r){ 23 | //rand()%(r-p+1) generate random number in range 0 to r-p 24 | // we want in range (p,r) so add p 25 | int i=p+rand_r(&seed)%(r-p+1); 26 | swap(arr[i],arr[r]); 27 | 28 | return(Partition(arr,p,r)); 29 | } 30 | void Randomized_quicksort(int arr[],int p,int r){ 31 | if(p 2 | using namespace std; 3 | 4 | //Display function 5 | void display(int array[], int n) 6 | { 7 | int i; 8 | for (i = 0; i < n; i++) 9 | cout << array[i] << "\t"; 10 | 11 | cout << "\n"; 12 | } 13 | 14 | //Driver code 15 | int main() 16 | { 17 | //Taking size of array as input 18 | int n; 19 | cout << "ENTER THE SIZE OF ARRAY: : "; 20 | cin >> n; 21 | 22 | //Taking elements of array as input 23 | int array[n]; 24 | cout << "ENTER THE ELEMENTS: " << endl; 25 | for (int i = 0; i < n; i++) 26 | { 27 | cin >> array[i]; 28 | } 29 | 30 | //Printing unsorted array 31 | cout << "UNSORTED ARRAY: " << endl; 32 | display(array, n); 33 | 34 | int i, j, smallest,temp; 35 | 36 | //Selection sort 37 | for (i = 0; i < n-1; i++) 38 | { 39 | smallest= i; 40 | for (j = i+1; j < n; j++){ 41 | if (array[j] < array[smallest]) 42 | smallest = j; 43 | } 44 | 45 | temp = array[smallest]; 46 | array[smallest] = array[i]; 47 | array[i] = temp; 48 | } 49 | 50 | //Printing sorted array 51 | cout << "SORTED ARRAY: \n"; 52 | display(array, n); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /C++/Stack/README.md: -------------------------------------------------------------------------------- 1 | # Stacks -------------------------------------------------------------------------------- /C++/Sub-Arrays/README.md: -------------------------------------------------------------------------------- 1 | # Algorithms on Sub-Arrays -------------------------------------------------------------------------------- /C++/Trees/README.md: -------------------------------------------------------------------------------- 1 | ![1_QwwrXC3c7YzndILdmZAAew](https://user-images.githubusercontent.com/76027425/210194849-d88e018b-b7b6-4542-9c2e-d15441797575.png) 2 | -------------------------------------------------------------------------------- /C++/arrays/README.md: -------------------------------------------------------------------------------- 1 | # Arrays -------------------------------------------------------------------------------- /C++/arrays/monotonic_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isMonotonic(vector& nums) { 6 | int n=nums.size(); 7 | int s=0,e=n-1; 8 | while(s>n; 29 | cout<<"enter the elements of array"; 30 | vector nums; 31 | for(int i=0;i>element; 34 | nums.push_back(element); 35 | } 36 | cout< 2 | 3 | //Brian Kerningham's Algorithm in C 4 | int setBits(int n) 5 | { 6 | int count = 0; 7 | while (n) { 8 | n = n & (n - 1); 9 | count++; 10 | } 11 | return count; 12 | } 13 | 14 | int main() { 15 | printf("%d",setBits(15)); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /C/Binary/hammingDistance.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | /* 5 | XOR TABLE 6 | X Y Z 7 | 0 0 0 8 | 0 1 1 9 | 1 0 1 10 | 1 1 0 11 | */ 12 | int x = 0, y =0, count=0; // taking two values as test cases 13 | printf("Enter the value of X: "); 14 | scanf("%d",&x); 15 | 16 | printf("Enter the value of Y: "); 17 | scanf("%d",&y); 18 | 19 | int res = x ^ y ; // taking xor of the two values to get different bits 20 | 21 | // counting number of 1 present in the res 22 | while(res>0){ 23 | count += res & 1; 24 | res >>= 1; 25 | } 26 | printf("The result is : %d",count);// printing the value of bits 27 | return 0; 28 | } -------------------------------------------------------------------------------- /C/Caching/README.md: -------------------------------------------------------------------------------- 1 | # Caching -------------------------------------------------------------------------------- /C/Concurrent Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Concurrent Algorithms -------------------------------------------------------------------------------- /C/Cryptography/Hill_Cipher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | unsigned int a[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } }; 5 | unsigned int b[3][3] = { { 8, 5, 10 }, { 21, 8, 21 }, { 21, 12, 8 } }; 6 | int i, j; 7 | unsigned int c[20], d[20]; 8 | char msg[20]; 9 | int determinant = 0, t = 0; 10 | ; 11 | printf("Enter plain text\n "); 12 | scanf("%s", msg); 13 | for (i = 0; i < 3; i++) { 14 | c[i] = msg[i] - 65; 15 | printf("%d ", c[i]); 16 | } 17 | for (i = 0; i < 3; i++) { 18 | t = 0; 19 | for (j = 0; j < 3; j++) { 20 | t = t + (a[i][j] * c[j]); 21 | } 22 | d[i] = t % 26; 23 | } 24 | printf("\nEncrypted Cipher Text :"); 25 | for (i = 0; i < 3; i++) 26 | printf(" %c", d[i] + 65); 27 | for (i = 0; i < 3; i++) { 28 | t = 0; 29 | for (j = 0; j < 3; j++) { 30 | t = t + (b[i][j] * d[j]); 31 | } 32 | c[i] = t % 26; 33 | } 34 | printf("\nDecrypted Cipher Text :"); 35 | for (i = 0; i < 3; i++) 36 | printf(" %c", c[i] + 65); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /C/Cryptography/README.md: -------------------------------------------------------------------------------- 1 | # Cryptography 2 | -------------------------------------------------------------------------------- /C/Data Compression/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/210194980-47be7a7f-fdf0-4131-a8ad-84adebafdda3.png) 2 | -------------------------------------------------------------------------------- /C/Deadlock Avoidance/README.md: -------------------------------------------------------------------------------- 1 | # Deadlock Avoidance -------------------------------------------------------------------------------- /C/Divide-and-Conquer/README.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer -------------------------------------------------------------------------------- /C/Dynamic-Programming/Fibonacci_Number.c: -------------------------------------------------------------------------------- 1 | // Author : K.om Senapati 2 | // Github username : KOMNOOB 3 | // Problem Statement: Fibonacci Number 4 | // Problem Description 5 | // The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. Given an integer n, your task is to find the nth Fibonacci number. 6 | 7 | // Write a function fibonacci(n) that takes in an integer n and returns the nth Fibonacci number. 8 | 9 | // Input : 10 | // An integer n representing the position of the Fibonacci number to be found. (0 <= n <= 30) 11 | // Output : 12 | // An integer representing the nth Fibonacci number. 13 | 14 | #include 15 | 16 | int fibonacci(int n) 17 | { 18 | if (n <= 0) 19 | { 20 | printf("n must be greater than 0\n"); 21 | return -1; 22 | } 23 | else if (n == 1 || n == 2) 24 | { 25 | return 1; 26 | } 27 | 28 | int memo[n + 1]; 29 | memo[0] = 0; 30 | memo[1] = 1; 31 | 32 | for (int i = 2; i <= n; i++) 33 | { 34 | memo[i] = memo[i - 1] + memo[i - 2]; 35 | } 36 | return memo[n]; 37 | } 38 | 39 | int main() 40 | { 41 | int n; 42 | printf("Enter the value of n: "); 43 | scanf("%d", &n); 44 | int result = fibonacci(n); 45 | printf("The Fibonacci number at position %d is %d\n", n, result); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /C/Dynamic-Programming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming -------------------------------------------------------------------------------- /C/Game Theory/README.md: -------------------------------------------------------------------------------- 1 | # Game Theory -------------------------------------------------------------------------------- /C/Graphics/README.md: -------------------------------------------------------------------------------- 1 | # Computer Graphics -------------------------------------------------------------------------------- /C/Graphs/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198797083-01523b30-bee8-48ca-bc65-d9320d41a251.png) 2 | -------------------------------------------------------------------------------- /C/Graphs/floyd_warshall.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void floyd_warshall(int a[][6]) 5 | { 6 | int i, j, k; 7 | for (k = 0; k < 6; k++) 8 | { 9 | for (i = 0; i < 6; i++) 10 | { 11 | for (j = 0; j < 6; j++) 12 | { 13 | if ((a[i][k] * a[k][j] != 0) && (i != j)) 14 | { 15 | if ((a[i][k] + a[k][j] < a[i][j]) || (a[i][j] == 0)) 16 | { 17 | a[i][j] = a[i][k] + a[k][j]; 18 | } 19 | } 20 | } 21 | } 22 | } 23 | for (i = 0; i < 6; i++) 24 | { 25 | printf("\nMINIMUM COST WITH RESPECT TO NODE: ",i,"\n"); 26 | for (j = 0; j < 6; j++) 27 | { 28 | printf(a[i][j],"\t"); 29 | } 30 | 31 | } 32 | } 33 | int main() 34 | { 35 | 36 | int a[6][6]; 37 | 38 | printf("---------ENTER VALUES OF ADJACENCY MATRIX------------\n\n"); 39 | for (int i = 0; i < 6; i++) 40 | { 41 | printf("ENTER THE VALUES FOR ",(i+1)," ROW: \n"); 42 | for (int j = 0; j < 6; j++) 43 | { 44 | scanf("%d",&a[i][j]); 45 | } 46 | } 47 | floyd_warshall(a); 48 | getch(); 49 | } -------------------------------------------------------------------------------- /C/Greedy-Algorithm/ActivitySelection.c: -------------------------------------------------------------------------------- 1 | //In this implementation user is required to enter the start and finish time already sorted according to finish time 2 | #include 3 | void printActivity(int start[],int finish[],int n){ 4 | int i,j; 5 | printf("Following activities are selected :"); 6 | i=0;//i reperesents the index of the activity 7 | printf("(%d %d) ,",start[i],finish[i]); 8 | for(j=1;j=finish[i]){ 12 | printf("(%d %d) ,",start[j],finish[j]); 13 | i=j; 14 | } 15 | } 16 | } 17 | int main() 18 | { 19 | int n;//n is the number of activities or processes 20 | printf("Enter the number of activities:\n"); 21 | scanf("%d",&n); 22 | printf("Enter the start and end time :\n"); 23 | int start[n],finish[n];//Input start time and end time 24 | for(int i=0;i 4 | 5 | void egyptian_Fraction(int n, int d) 6 | { 7 | // When Both Numerator and denominator becomes zero then we simply return; 8 | if (d == 0 || n == 0) 9 | return; 10 | if (d % n == 0) 11 | { 12 | printf("1/%d", d / n); 13 | return; 14 | } 15 | if (n % d == 0) 16 | { 17 | printf("%d", n / d); 18 | return; 19 | } 20 | if (n > d) 21 | { 22 | printf("%d + ", n / d); 23 | egyptian_Fraction(n % d, d); 24 | return; 25 | } 26 | int x = d / n + 1; 27 | printf("1/%d + ", x); 28 | egyptian_Fraction(n * x - d, d * x); 29 | } 30 | 31 | int main() 32 | { 33 | int num, den; 34 | printf("Enter Numerator: "); 35 | scanf("%d", &num); 36 | printf("Enter Denominator:"); 37 | scanf("%d", &den); 38 | printf("Egyptian Fraction representation of %d/%d is\n", num, den); 39 | egyptian_Fraction(num, den); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /C/Greedy-Algorithm/README.md: -------------------------------------------------------------------------------- 1 | # Greedy Algorithm -------------------------------------------------------------------------------- /C/Hashing/README.md: -------------------------------------------------------------------------------- 1 | # Hashing -------------------------------------------------------------------------------- /C/Image Processing/Canny Edge Detection Algorithm/dog.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/C/Image Processing/Canny Edge Detection Algorithm/dog.jpeg -------------------------------------------------------------------------------- /C/Image Processing/README.md: -------------------------------------------------------------------------------- 1 | # Image Processing -------------------------------------------------------------------------------- /C/Job Scheduling/README.md: -------------------------------------------------------------------------------- 1 | # Job Scheduling Algorithms -------------------------------------------------------------------------------- /C/Machine Learning/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/C/Machine Learning/README.md -------------------------------------------------------------------------------- /C/Maths/Binary_expo.c: -------------------------------------------------------------------------------- 1 | /*Binary exponentiation (also known as exponentiation by squaring)is a trick which allows to 2 | calculate a^n using only O(log n) multiplications (instead of 3 | O(n) multiplications required by the naive approach). 4 | 5 | The final complexity of this algorithm is O(log n): we have to compute log n powers of 6 | a, and then have to do at most log n multiplications to get the final answer from them. 7 | */ 8 | 9 | #include 10 | 11 | long long int binary(int Base, int Exponent){ 12 | if (Exponent==0) 13 | return 1 ; 14 | long long res = binary(Base , Exponent / 2); 15 | while (Exponent > 0 ){ 16 | if (Exponent %2 ==0) 17 | return res * res ; 18 | else 19 | return res * res * Base ; 20 | 21 | } 22 | 23 | } 24 | int main(){ 25 | int Base , Exponent ; 26 | printf("Enter the Base : "); 27 | scanf("%d" , &Base); 28 | printf("Enter the Exponent : "); 29 | scanf("%d", &Exponent); 30 | long long int Answer = binary(Base,Exponent); 31 | printf("\n%d^%d = %lld",Base , Exponent , Answer); 32 | return 0 ; 33 | 34 | } 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /C/Maths/BinomialTheorem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int fact(int n) { 5 | int factn = 1; 6 | for (int i = 1; i < n + 1; i++) { 7 | factn = factn * i; 8 | } 9 | return factn; 10 | } 11 | 12 | int main() { 13 | printf("Enter in (1+x)^n the values of x and n respectively\n"); 14 | int x, n; 15 | scanf("%d %d", &x, &n); 16 | int nf = fact(n); 17 | int arr[n + 1]; 18 | 19 | for (int i = 0; i <= n; i++) { 20 | int ncr = nf / (fact(i) * fact(n - i)); 21 | arr[i] = ncr * pow(x, i); 22 | arr[n - i] = ncr * pow(x, n - i); 23 | } 24 | 25 | for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { 26 | printf("%d ", arr[i]); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /C/Maths/ModularExponentiation.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // Declaring the variables. 5 | int base,power,modulo; 6 | // Reading the base,power,modulo values from the user. 7 | printf("Enter base value = "); 8 | scanf("%d",&base); 9 | printf("Enter power value = "); 10 | scanf("%d",&power); 11 | printf("Enter modulo value = "); 12 | scanf("%d",&modulo); 13 | // Update base if it is more than or equal to modulo 14 | base = base % modulo; 15 | // Initializing the result variable to store the modular exponentiation value. 16 | int result = 1; 17 | // In case base is divisible by modulo; 18 | if (base == 0) 19 | { 20 | printf("0"); 21 | } 22 | while (power > 0) 23 | { 24 | // If power is odd, multiply base with result 25 | if ((power & 1) != 0) 26 | { 27 | result = (result * base) % modulo; 28 | } 29 | // power must be even now 30 | // power = power/2 31 | power = power >> 1; 32 | base = (base * base) % modulo; 33 | } 34 | // Printing the modular exponentiation value. 35 | printf("Modular Exponentiation = %d ",result); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /C/Maths/Pascal_Triangle.c: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 1 4 | 1 2 1 5 | 1 3 3 1 6 | 1 4 6 4 1 7 | 8 | Each number in the triangle is the sum of the two numbers directly above it. 9 | 10 | we take number of rows as input 11 | And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr. 12 | nCr=factorial(n)/(factorial(r)*factorial(n-r)) 13 | */ 14 | #include 15 | 16 | long factorial(int n) 17 | { 18 | int c; 19 | long result = 1; 20 | 21 | for (c = 1; c <= n; c++) 22 | result = result*c; 23 | 24 | return result; 25 | } 26 | 27 | int main() 28 | { 29 | int i, n, c; 30 | 31 | printf("Enter the number of rows you wish to see in pascal triangle:\n"); 32 | scanf("%d",&n); 33 | 34 | for (i = 0; i < n; i++) 35 | { 36 | for (c = 0; c <= (n - i - 2); c++) 37 | printf(" "); 38 | 39 | for (c = 0 ; c <= i; c++) 40 | printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); 41 | 42 | printf("\n"); 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /C/Maths/Power_Set.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void generatePowerSet(int originalSet[], int numElements) { 6 | int numSubsets = pow(2, numElements); 7 | 8 | for (int i = 0; i < numSubsets; i++) { 9 | printf("{ "); 10 | for (int j = 0; j < numElements; j++) { 11 | if ((i & (1 << j)) != 0) { 12 | printf("%d ", originalSet[j]); 13 | } 14 | } 15 | printf("}\n"); 16 | } 17 | } 18 | 19 | int main() { 20 | int numElements; 21 | printf("Enter the number of elements in the original set: "); 22 | scanf("%d", &numElements); 23 | 24 | int originalSet[numElements]; 25 | printf("Enter the elements of the original set: "); 26 | for (int i = 0; i < numElements; i++) { 27 | scanf("%d", &originalSet[i]); 28 | } 29 | 30 | printf("Power Set:\n"); 31 | generatePowerSet(originalSet, numElements); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /C/Maths/RussianPeasant.c: -------------------------------------------------------------------------------- 1 | #include 2 | // Function to multiply two numbers using Russian Peasant method 3 | int russianPeasant(int a, int b) { 4 | // Initialize the result 5 | int res = 0; 6 | 7 | // While the second number doesn't become 1 8 | while (b > 0) { 9 | // If the second number becomes odd, add the first number to the result 10 | if (b & 1) { 11 | res = res + a; 12 | } 13 | 14 | // Double the first number and halve the second number 15 | a = a << 1; 16 | b = b >> 1; 17 | } 18 | return res; 19 | } 20 | // Driver program 21 | int main() { 22 | int a, b; 23 | printf("Enter two integers: "); 24 | scanf("%d %d", &a, &b); 25 | int result = russianPeasant(a, b); 26 | printf("The result of multiplication is: %d\n", result); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /C/Maths/SimilarTriangles.c: -------------------------------------------------------------------------------- 1 | // Similar Triangles Program 2 | // It can be used to check whether the two traingles are similar or not. 3 | // Author: @swastik-akhil 4 | 5 | 6 | #include 7 | int main(void) { 8 | 9 | // SSS property 10 | int a1 = 3, b1 = 4, c1 = 5; 11 | int a2 = 6, b2 = 8, c2 = 10; 12 | if (a1/a2 == b1/b2 && b1/b2 == c1/c2) { 13 | printf("The triangles are similar\n"); 14 | } else { 15 | printf("The triangles are not similar\n"); 16 | } 17 | 18 | 19 | // AA property 20 | 21 | int x1 = 40, x2 = 70; 22 | int y1 = 40, y2 = 70; 23 | if(x1==y1 && x2==y2){ 24 | printf("The triangles are similar\n"); 25 | } else { 26 | printf("The triangles are not similar\n"); 27 | } 28 | 29 | 30 | //SAS property 31 | 32 | 33 | int a3= 3,m = 50, b3 = 4 ; 34 | int a4 = 6, n = 50, b4 = 8; 35 | if (a3/a4 == b3/b4 && m == n) { 36 | printf("The triangles are similar\n"); 37 | } else { 38 | printf("The triangles are not similar\n"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /C/Maths/Strong_Number.c: -------------------------------------------------------------------------------- 1 | /*Name : Hardik Goyal 2 | Github username : HardikGoyal2003 3 | Repositary name : Algorithms 4 | */ 5 | /*What is a strong number? 6 | A strong number is a positive integer whose sum of factorials of its digits equals the number itself. In other words, if we take the 7 | digits of a number and calculate the factorial of each digit, then sum those factorials, and the result is the same as the original number, 8 | then that number is called a strong number. 9 | 10 | Problem Statement : To Check Given number is Strong Number or not. 11 | EXAMPLE : 12 | 1) 13 | Input: 145 14 | Output: "It is a Strong Number" 15 | */ 16 | 17 | #include 18 | int main(){ 19 | int n,factorial=1,sum=0; 20 | printf("Enter Number"); 21 | scanf("%d",&n); 22 | int new=n; 23 | while (n>0) 24 | { 25 | int rem = n%10; 26 | n/=10; 27 | for (int i=1; i <=rem; i++) 28 | { 29 | factorial*=i; 30 | } 31 | sum+=factorial; 32 | factorial=1; 33 | } 34 | if (new==sum) 35 | { 36 | printf("It is Strong Number"); 37 | } 38 | else 39 | { 40 | printf("It is not a Strong Number"); 41 | } 42 | } -------------------------------------------------------------------------------- /C/Maths/factorial.c: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | 3 | FACTORIAL PROGRAM 4 | Factorial is represented by !. It is calculated by mutiplying natural 5 | numbers upto that number, i.e., n! = 1 x 2 x 3... x n. 6 | (n should be positive) 7 | Note: 0! = 1 and 1! = 1 8 | 9 | **********************************************************************/ 10 | 11 | #include 12 | 13 | int factorial(int num){ 14 | if(num == 0 || num == 1) // factorial of 0 and 1 is 1 15 | return 1; 16 | else // if num > 1 17 | return (num * factorial(num - 1)); 18 | } 19 | 20 | int main() 21 | { 22 | int num; 23 | int res = 1; 24 | 25 | printf("Enter a number: "); 26 | scanf("%d", &num); // factorial of this no will be calculated 27 | 28 | // calling the function 29 | res = factorial(num); 30 | 31 | printf("Factorial of %d is %d", num, res); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C/Maths/factors.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void factors(int n) { 4 | printf("Factors of %d are: ", n); 5 | for (int i = 1; i <= n; ++i) { 6 | if (n % i == 0) { 7 | printf("%d ", i); 8 | } 9 | } 10 | printf("\n"); 11 | } 12 | 13 | int main() { 14 | factors(24); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /C/Maths/heronsFormula.c: -------------------------------------------------------------------------------- 1 | //This code takes input of three sides of a traingle from the user and returns it's are which is calculated using Heron's Formula. In symbols, if a, b, and c are the lengths of the sides: Area = Square root of√s(s - a)(s - b)(s - c) where s is half the perimeter, or (a + b + c)/2. 2 | 3 | 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | double s1, s2, s3, area, S; 10 | 11 | // Take input from user 12 | printf("Enter Three Sides of a Triangle\n"); 13 | scanf("%lf %lf %lf", &s1, &s2, &s3); 14 | 15 | S = (s1 + s2 + s3) / 2; 16 | area = sqrt(S * (S - s1) * (S - s2) * (S - s3)); 17 | 18 | printf("The Area of triangle = %.2f\n", area); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C/Maths/is_Prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n,i,m=0,flag=0; 4 | printf("Enter the number to check prime:"); 5 | scanf("%d",&n); 6 | m=n/2; 7 | for(i=2;i*i<=m;i++) 8 | { 9 | if(n%i==0) 10 | { 11 | printf("Number is not prime"); 12 | flag=1; 13 | break; 14 | } 15 | } 16 | if(flag==0) 17 | printf("Number is prime"); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C/Maths/perfect.c: -------------------------------------------------------------------------------- 1 | // C program to check if the number is a Perfect number or not 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | // initialize variable for loop, input number and to store sum 8 | int i, num, sum = 0; 9 | 10 | // take user input 11 | printf("\nEnter a number : "); 12 | scanf("%d", &num); 13 | 14 | // iterate from 1 to N and if the number is a factor of N then add it to sum 15 | for(i=1; i 2 | #include 3 | 4 | void ShriDharacharyaFormulae(double a, double b, double c) { 5 | double compValue = pow(b, 2) - 4 * a * c; 6 | double val1 = -b / (2 * a); 7 | 8 | if (compValue >= 0) { 9 | double val2 = sqrt(compValue) / (2 * a); 10 | printf("%lf and %lf\n", val1 + val2, val1 - val2); 11 | } else { 12 | printf("Imaginary roots\n"); 13 | } 14 | } 15 | 16 | int main() { 17 | double a, b, c; 18 | scanf("%lf", &a); 19 | scanf("%lf", &b); 20 | scanf("%lf", &c); 21 | ShriDharacharyaFormulae(a, b, c); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /C/Maths/sieveOfEratosthenes.c: -------------------------------------------------------------------------------- 1 | /*Sieve of Eratosthenes 2 | In this method first we create an array of n number and then we start traversing it and the numbers 3 | which encounters its multiple is marked and the left out numbers are prime numbers*/ 4 | 5 | #include 6 | 7 | int main(){ 8 | int n; 9 | printf("Enter the number till where prime is required: "); 10 | scanf("%d",&n); 11 | 12 | int arr[10000]={0}; 13 | for(int i=2;i<=n;i++){ 14 | if(arr[i]==0){ //Markiong the numbers 15 | for(int j=i*i;j<=n;j+=i){ 16 | arr[j]=1; 17 | } 18 | } 19 | } 20 | 21 | for(int i=2;i<=n;i++){ 22 | if(arr[i]==0){ 23 | printf("%d\n",i); 24 | } 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /C/Maths/zeller.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *switchDay(int h) { 4 | char *days[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 5 | return days[h]; 6 | } 7 | 8 | void zellerCongruence(int day, int month, int year) { 9 | if (month == 1) { 10 | month = 13; 11 | year--; 12 | } if (month == 2) { 13 | month = 14; 14 | year--; 15 | } 16 | int q = day; 17 | int m = month; 18 | int k = year % 100; 19 | int j = year / 100; 20 | int h = q + 13 * (m + 1) / 5 + k + k / 4 + j / 4 + 5 * j; 21 | h = h % 7; 22 | printf("%s\n", switchDay(h)); 23 | } 24 | 25 | // Driver Code 26 | int main() { 27 | zellerCongruence(11, 11, 2022); // date (dd/mm/yyyy) 28 | return 0; 29 | } -------------------------------------------------------------------------------- /C/Memory Allocation/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation -------------------------------------------------------------------------------- /C/Optimization/README.md: -------------------------------------------------------------------------------- 1 | # Optimization 2 | Grey Wolf Optimisation -------------------------------------------------------------------------------- /C/Pattern-Matching/README.md: -------------------------------------------------------------------------------- 1 | # Pattern Matching -------------------------------------------------------------------------------- /C/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198798759-287eafc9-353c-4be7-b698-061ebac57e41.png) 2 | 3 | -------------------------------------------------------------------------------- /C/Recursion/Permutations.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void permutations(char p[], char up[]) { 5 | // Base case: If the remaining characters to be permuted is empty, print the permutation 6 | if (up[0] == '\0') { 7 | printf("%s\n", p); 8 | return; 9 | } 10 | 11 | char ch = up[0]; 12 | int len = strlen(p); 13 | 14 | for (int i = 0; i <= len; i++) { 15 | // Split the permutation string into two parts 16 | char f[100], s[100]; 17 | strncpy(f, p, i); // First part 18 | f[i] = '\0'; 19 | strcpy(s, p + i); // Second part 20 | 21 | // Recursive call: Generate permutations by inserting the current character at each possible position 22 | permutations(strcat(strcat(f, &ch), s), up + 1); 23 | } 24 | } 25 | 26 | int main() { 27 | char str[] = "abc"; 28 | permutations("", str); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /C/Recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion -------------------------------------------------------------------------------- /C/Searching/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198502317-119683ce-122d-46f6-9888-b7329aadf267.png) 2 | -------------------------------------------------------------------------------- /C/Searching/linear_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | <----------------------------------PROBLEM------------------------------------> 5 | Given an array arr[] of n elements, write a function to search a given element el in arr[]. 6 | 7 | Variables used: 8 | n = total number of elements in array 9 | arr = array initialized with the input size 10 | el = element to be searched in the array. 11 | */ 12 | 13 | int main() 14 | { 15 | int n=0; 16 | printf("Enter total no. of elements in the array: "); 17 | scanf("%d", &n); 18 | int arr[n]; 19 | for(int i=0; i 2 | #include 3 | 4 | #define SIZE 10 5 | 6 | void RandomShuffle(int *arr, int sizeOfArray) { 7 | int i, j; 8 | for (i = 0; i < sizeOfArray; i++) { 9 | j = rand() % sizeOfArray; 10 | int t = arr[i]; 11 | arr[i] = arr[j]; 12 | arr[j] = t; 13 | } 14 | } 15 | 16 | int is_sorted(int *arr, int sizeOfArray) { 17 | int i; 18 | for (i = 1; i < sizeOfArray; i++) { 19 | if (arr[i] < arr[i - 1]) { 20 | return 0; 21 | } 22 | } 23 | return 1; 24 | } 25 | 26 | void bogosort(int *arr, int sizeOfArray) { 27 | while (!is_sorted(arr, sizeOfArray)) { 28 | RandomShuffle(arr, sizeOfArray); 29 | } 30 | } 31 | 32 | int main() { 33 | int arr[SIZE]; 34 | int i; 35 | 36 | for (i = 0; i < SIZE; i++) { 37 | arr[i] = rand() % 100; 38 | } 39 | 40 | printf("The unsorted arr is:\t"); 41 | for (i = 0; i < SIZE; i++) { 42 | printf("%d ", arr[i]); 43 | } 44 | printf("\n"); 45 | 46 | bogosort(arr, SIZE); 47 | 48 | printf("The sorted arr is:\t"); 49 | for (i = 0; i < SIZE; i++) { 50 | printf("%d ", arr[i]); 51 | } 52 | printf("\n"); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C/Sorting/Cyclic_Sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * C Program to Implement Cyclic Sort 3 | */ 4 | #include 5 | 6 | #define MAX 8 7 | 8 | void cyclic_sort(int *); 9 | 10 | void main() 11 | { 12 | int a[MAX],i; 13 | 14 | printf("enter the elements into array :"); 15 | for (i = 0;i < MAX; i++) 16 | { 17 | scanf("%d", &a[i]); 18 | } 19 | cyclic_sort(a); 20 | printf("sorted elements are :\n"); 21 | for (i = 0;i < MAX; i++) 22 | { 23 | printf("%d ", a[i]); 24 | } 25 | } 26 | 27 | /* sorts elements using cyclic sort algorithm */ 28 | void cyclic_sort(int * a) 29 | { 30 | int temp, item, pos, i, j, k; 31 | 32 | for (i = 0;i < MAX; i++) 33 | { 34 | item = a[i]; 35 | pos = i; 36 | do 37 | { 38 | k = 0; 39 | for (j = 0;j < MAX;j++) 40 | { 41 | if (pos != j && a[j] < item) 42 | { 43 | k++; 44 | } 45 | } 46 | if (pos != k) 47 | { 48 | while (pos != k && item == a[k]) 49 | { 50 | k++; 51 | } 52 | temp = a[k]; 53 | a[k] = item; 54 | item = temp; 55 | pos = k; 56 | } 57 | }while (pos != i); 58 | } 59 | } -------------------------------------------------------------------------------- /C/Sorting/Odd_Even_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 7 3 | 4 | void swap(int *,int *); 5 | void oddeven_sort(int *); 6 | 7 | void main() 8 | { 9 | int a[MAX], i; 10 | 11 | printf("enter the elements in to the matrix :"); 12 | for (i = 0;i < MAX;i++) 13 | { 14 | scanf("%d", &a[i]); 15 | } 16 | printf("sorted elements are :\n"); 17 | oddeven_sort(a); 18 | for (i = 0;i < MAX;i++) 19 | { 20 | printf(" %d", a[i]); 21 | } 22 | } 23 | 24 | /* swaps the elements */ 25 | void swap(int * x, int * y) 26 | { 27 | int temp; 28 | 29 | temp = *x; 30 | *x = *y; 31 | *y = temp; 32 | } 33 | 34 | /* sorts the array using oddeven algorithm */ 35 | void oddeven_sort(int * x) 36 | { 37 | int sort = 0, i; 38 | 39 | while (!sort) 40 | { 41 | sort = 1; 42 | for (i = 1;i < MAX;i += 2) 43 | { 44 | if (x[i] > x[i+1]) 45 | { 46 | swap(&x[i], &x[i+1]); 47 | sort = 0; 48 | } 49 | } 50 | for (i = 0;i < MAX - 1;i += 2) 51 | { 52 | if (x[i] > x[i + 1]) 53 | { 54 | swap(&x[i], &x[i + 1]); 55 | sort = 0; 56 | } 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /C/Sorting/Pigeonhole sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pigeonholeSort(int arr[], int n) { 4 | int min = arr[0], max = arr[0]; 5 | for (int i = 1; i < n; i++) { 6 | if (arr[i] < min) { 7 | min = arr[i]; 8 | } 9 | if (arr[i] > max) { 10 | max = arr[i]; 11 | } 12 | } 13 | 14 | int range = max - min + 1; 15 | int* pigeonholes = (int*)malloc(range * sizeof(int)); 16 | memset(pigeonholes, 0, range * sizeof(int)); 17 | 18 | for (int i = 0; i < n; i++) { 19 | pigeonholes[arr[i] - min]++; 20 | } 21 | 22 | int index = 0; 23 | for (int i = 0; i < range; i++) { 24 | while (pigeonholes[i] > 0) { 25 | arr[index++] = i + min; 26 | pigeonholes[i]--; 27 | } 28 | } 29 | 30 | free(pigeonholes); 31 | } 32 | 33 | int main() { 34 | int n; 35 | printf("Enter the number of elements: "); 36 | scanf("%d", &n); 37 | 38 | int arr[n]; 39 | printf("Enter the elements:\n"); 40 | for (int i = 0; i < n; i++) { 41 | scanf("%d", &arr[i]); 42 | } 43 | 44 | pigeonholeSort(arr, n); 45 | 46 | printf("Sorted array: "); 47 | for (int i = 0; i < n; i++) { 48 | printf("%d ", arr[i]); 49 | } 50 | printf("\n"); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /C/Sorting/README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | -------------------------------------------------------------------------------- /C/Sorting/StoogeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | // Function to swap two elements 3 | void swap(int* a,int* b){ 4 | int temp=*a; 5 | *a=*b; 6 | *b=temp; 7 | } 8 | // Stooge Sort function 9 | void stoogeSort(int arr[],int low,int high){ 10 | if(low>=high) 11 | return; 12 | // If the first element is greater than the last element, swap them 13 | if(arr[low]>arr[high]) 14 | swap(&arr[low],&arr[high]); 15 | // If there are more than 2 elements in the array 16 | if(high-low+1>2) { 17 | int t=(high-low+1)/3; 18 | // Sort the first two-thirds of the array 19 | stoogeSort(arr,low,high-t); 20 | // Sort the last two-thirds of the array 21 | stoogeSort(arr,low+t,high); 22 | // Sort the first two-thirds of the array again 23 | stoogeSort(arr,low,high-t); 24 | } 25 | } 26 | // Function to print the array 27 | void printArray(int arr[],int size){ 28 | for(int i=0;i 2 | 3 | void bingoSort(int arr[], int n) 4 | { 5 | int min = arr[0], max = arr[0]; 6 | int i; 7 | for (i = 1; i < n; i++) 8 | { 9 | if (arr[i] < min) 10 | min = arr[i]; 11 | else if (arr[i] > max) 12 | max = arr[i]; 13 | } 14 | int *flags = (int *)calloc(max - min + 1, sizeof(int)); 15 | for (i = 0; i < n; i++) 16 | flags[arr[i] - min] = 1; 17 | int index = 0; 18 | for (i = 0; i < max - min + 1; i++) 19 | { 20 | if (flags[i]) 21 | { 22 | arr[index] = i + min; 23 | index++; 24 | } 25 | } 26 | free(flags); 27 | } 28 | 29 | int main() 30 | { 31 | printf("Enter the length of the array: "); 32 | int n; 33 | scanf("%d", &n); 34 | int *a = (int *)malloc(n * sizeof(int)); 35 | printf("Enter the elements of the array:\n"); 36 | for (int i = 0; i < n; i++) 37 | scanf("%d", &a[i]); 38 | printf("Original array:\n"); 39 | for (int i = 0; i < n; i++) 40 | printf("%d ", a[i]); 41 | bingoSort(a, n); 42 | printf("\nSorted array:\n"); 43 | for (int i = 0; i < n; i++) 44 | printf("%d ", a[i]); 45 | free(a); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C/Sorting/bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[20], n, i, j, temp, k, s; 6 | 7 | 8 | printf("ENTER THE SIZE OF ARRAY: "); 9 | scanf("%d", &n); 10 | 11 | for (i = 0; i < n; i++) 12 | { 13 | printf("ENTER THE ELEMENTS: "); 14 | scanf("%d", &a[i]); 15 | } 16 | 17 | for (s = 0; s < n - 1; s++) 18 | { 19 | 20 | for (k = 0; k < n - 1; k++) 21 | { 22 | if (a[k] > a[k + 1]) 23 | { 24 | temp = a[k]; 25 | a[k] = a[k + 1]; 26 | a[k + 1] = temp; 27 | } 28 | } 29 | } 30 | 31 | for (j = 0; j < n; j++) 32 | { 33 | printf("%d\t", a[j]); 34 | } 35 | } -------------------------------------------------------------------------------- /C/Sorting/comb_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int getNextGap(int gap) 5 | { 6 | gap = (gap * 10) / 13; 7 | if (gap < 1) 8 | return 1; 9 | return gap; 10 | } 11 | 12 | void combSort(int a[], int n) 13 | { 14 | int gap = n; 15 | bool swapped = true; 16 | while (gap != 1 || swapped == true) 17 | { 18 | gap = getNextGap(gap); 19 | swapped = false; 20 | for (int i = 0; i < n - gap; i++) 21 | { 22 | if (a[i] > a[i + gap]) 23 | { 24 | int temp = a[i]; 25 | a[i] = a[i + gap]; 26 | a[i + gap] = temp; 27 | swapped = true; 28 | } 29 | } 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | printf("Enter the length of the array: "); 36 | int n; 37 | scanf("%d", &n); 38 | int *a = (int *)malloc(n * sizeof(int)); 39 | printf("Enter the elements of the array:\n"); 40 | for (int i = 0; i < n; i++) 41 | scanf("%d", &a[i]); 42 | printf("Original array:\n"); 43 | for (int i = 0; i < n; i++) 44 | printf("%d ", a[i]); 45 | combSort(a, n); 46 | printf("\nSorted array:\n"); 47 | for (int i = 0; i < n; i++) 48 | printf("%d ", a[i]); 49 | free(a); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /C/Sorting/dnf_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void dnfSort(int arr[],int n){ 4 | int t1,t2; 5 | int s = 0; 6 | int mid = 0; 7 | int e = n-1; 8 | while(mid<=e){ 9 | // shifting all the zeros to the front 10 | if(arr[mid] == 0){ 11 | t1=arr[mid]; 12 | arr[mid]=arr[s]; 13 | arr[s]=t1; 14 | s++; 15 | mid++; 16 | } 17 | 18 | // when the array encounters 1, the position dosen't change 19 | else if(arr[mid] == 1){ 20 | mid++; 21 | } 22 | 23 | // shifting all 2s to the end 24 | else if(arr[mid] == 2){ 25 | t2=arr[mid]; 26 | arr[mid]=arr[e]; 27 | arr[e]=t2; 28 | e--; 29 | } 30 | } 31 | return; 32 | } 33 | int main(){ 34 | int n; 35 | scanf("%d",&n); 36 | int arr[1000]; 37 | for (int i = 0; i < n; ++i) 38 | { 39 | scanf("%d",&arr[i]); 40 | } 41 | dnfSort(arr, n); 42 | for (int i = 0; i < n; ++i) 43 | { 44 | printf("%d",arr[i]); 45 | printf(" "); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /C/Sorting/insertion_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void Sort(int a[], int n) 5 | { 6 | int i, key, j; 7 | for (i = 1; i < n; i++) 8 | { 9 | key = a[i]; 10 | j = i - 1; 11 | 12 | while (j >= 0 && a[j] > key) 13 | { 14 | a[j + 1] = a[j]; 15 | j = j - 1; 16 | } 17 | a[j + 1] = key; 18 | } 19 | 20 | for (int i = 0; i < n; i++) 21 | printf("%d ", a[i]); 22 | printf("\n"); 23 | } 24 | 25 | int main() 26 | { 27 | int arr[] = {45, 55, 20, 3, 5}; 28 | int n = sizeof(arr) / sizeof(arr[0]); 29 | Sort(arr, n); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /C/Sorting/randomised_quicksort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int Partition(int arr[], int p, int r) { 6 | int x = arr[r]; 7 | int i = p - 1; 8 | for (int j = p; j <= r - 1; j++) { 9 | if (arr[j] <= x) { 10 | i++; 11 | int temp = arr[i]; 12 | arr[i] = arr[j]; 13 | arr[j] = temp; 14 | } 15 | } 16 | int temp = arr[i + 1]; 17 | arr[i + 1] = arr[r]; 18 | arr[r] = temp; 19 | return i + 1; 20 | } 21 | 22 | int Randomized_partition(int arr[], int p, int r) { 23 | int i = p + rand() % (r - p + 1); 24 | int temp = arr[i]; 25 | arr[i] = arr[r]; 26 | arr[r] = temp; 27 | return Partition(arr, p, r); 28 | } 29 | 30 | void Randomized_quicksort(int arr[], int p, int r) { 31 | if (p < r) { 32 | int q = Randomized_partition(arr, p, r); 33 | Randomized_quicksort(arr, p, q - 1); 34 | Randomized_quicksort(arr, q + 1, r); 35 | } 36 | } 37 | 38 | int main() { 39 | int arr[] = {78, 9, 10, -1, 2, -45, 91}; 40 | int n = sizeof(arr) / sizeof(arr[0]); 41 | srand(time(0)); 42 | Randomized_quicksort(arr, 0, n - 1); 43 | printf("Sorted array using Randomized quicksort is:\n"); 44 | for (int i = 0; i < n; i++) { 45 | printf("%d ", arr[i]); 46 | } 47 | printf("\n"); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /C/Sorting/selection_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[20], n, i, j, temp, k, s; 6 | 7 | // Taking size of array as input 8 | printf("ENTER THE SIZE OF ARRAY: "); 9 | scanf("%d", &n); 10 | 11 | // Taking array elements as input 12 | for (i = 0; i < n; i++) 13 | { 14 | printf("ENTER THE ELEMENTS: "); 15 | scanf("%d", &a[i]); 16 | } 17 | 18 | // selection sort 19 | for (int i = 0; i < n - 1; i++) 20 | { 21 | int smallest = i; 22 | for (int j = i + 1; j < n; j++) 23 | { 24 | if (a[smallest] > a[j]) 25 | { 26 | smallest = j; 27 | } 28 | } 29 | 30 | // swapping 31 | int temp = a[smallest]; 32 | a[smallest] = a[i]; 33 | a[i] = temp; 34 | } 35 | 36 | // Printing sorted array 37 | printf("SORTED ARRAY: "); 38 | for (j = 0; j < n; j++) 39 | { 40 | printf("%d\t", a[j]); 41 | } 42 | } -------------------------------------------------------------------------------- /C/Stack/README.md: -------------------------------------------------------------------------------- 1 | # Stacks -------------------------------------------------------------------------------- /C/Sub-Arrays/Kadane_Algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // function for kadane's algorithm 4 | int kadane(int Array[], int n) { 5 | int max_sum = 0; 6 | int current_sum = 0; 7 | 8 | for(int i=0; i 2 | #include 3 | 4 | bool isMonotonic(int nums[], int n) { 5 | int s = 0, e = n - 1; 6 | while (s < e) { 7 | if (nums[s] <= nums[s + 1] && nums[s] <= nums[e] && e == n - 1) { 8 | s++; 9 | } 10 | else if (nums[e] <= nums[e - 1] && nums[e] <= nums[s] && s == 0) { 11 | e--; 12 | } 13 | else { 14 | break; 15 | } 16 | } 17 | if (s == n - 1 || e == 0) { 18 | return true; 19 | } 20 | return false; 21 | } 22 | 23 | int main() { 24 | int n; 25 | printf("enter no of elements in array: "); 26 | scanf("%d", &n); 27 | 28 | int nums[n]; 29 | printf("enter the elements of array: "); 30 | for (int i = 0; i < n; i++) { 31 | scanf("%d", &nums[i]); 32 | } 33 | 34 | if (isMonotonic(nums, n)) { 35 | printf("the array is monotonic\n"); 36 | } 37 | else { 38 | printf("the array is not monotonic\n"); 39 | } 40 | 41 | return 0; 42 | } 43 | // MONOTONIC ARRAY IN C 44 | // This Program Tells if an array is monotonic or not. 45 | -------------------------------------------------------------------------------- /Java/Artificial Intelligence/README.md: -------------------------------------------------------------------------------- 1 | # Artificial Intelligence -------------------------------------------------------------------------------- /Java/Automata/README.md: -------------------------------------------------------------------------------- 1 | # Automata Theory -------------------------------------------------------------------------------- /Java/Backtracking/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Java/Backtracking/README.md -------------------------------------------------------------------------------- /Java/Binary/Brain_Keringham.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Brain_Keringham{ 4 | 5 | //Brian Kerningham's Algorithm in Java 6 | static int setBits(int n) 7 | { 8 | int count = 0; 9 | while (n > 0) { 10 | n = n & (n - 1); 11 | count++; 12 | } 13 | return count; 14 | } 15 | 16 | public static void main(String[] args) { 17 | System.out.println(setBits(15)); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Java/Binary/hammingDistance.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class hammingDistance { 3 | public static void main(String[] args) { 4 | Scanner sc = new Scanner(System.in); 5 | System.out.println("Enter the value of X: "); 6 | int x = sc.nextInt(); 7 | System.out.println("Enter the value of Y: "); 8 | int y= sc.nextInt(); 9 | int count=0; 10 | int res = x ^ y; 11 | while(res>0){ 12 | count+= res &1; 13 | res >>=1; 14 | } 15 | System.out.println("The result is: " + count); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Java/Caching/README.md: -------------------------------------------------------------------------------- 1 | # Caching -------------------------------------------------------------------------------- /Java/Concurrent Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Concurrent Algorithms -------------------------------------------------------------------------------- /Java/Cryptography/README.md: -------------------------------------------------------------------------------- 1 | # Cryptography 2 | -------------------------------------------------------------------------------- /Java/Data-Compression/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/210194999-150ddc8f-c7a1-4751-aa43-a7cca7befc5f.png) 2 | -------------------------------------------------------------------------------- /Java/Deadlock Avoidance/README.md: -------------------------------------------------------------------------------- 1 | # Deadlock Avoidance -------------------------------------------------------------------------------- /Java/Divide-and-Conquer/README.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer -------------------------------------------------------------------------------- /Java/Dynamic-Programming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming -------------------------------------------------------------------------------- /Java/Game Theory/README.md: -------------------------------------------------------------------------------- 1 | # Game Theory -------------------------------------------------------------------------------- /Java/Graphics/README.md: -------------------------------------------------------------------------------- 1 | # Computer Graphics -------------------------------------------------------------------------------- /Java/Graphs/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198797268-63f38396-03ba-47ee-b0ee-e6f70e4ad46c.png) 2 | -------------------------------------------------------------------------------- /Java/Greedy-Algorithm/README.md: -------------------------------------------------------------------------------- 1 | # Greedy Algorithm -------------------------------------------------------------------------------- /Java/Hashing/README.md: -------------------------------------------------------------------------------- 1 | # Hashing -------------------------------------------------------------------------------- /Java/Image Processing/Canny Edge Detection Algorithm/dog.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Java/Image Processing/Canny Edge Detection Algorithm/dog.jpeg -------------------------------------------------------------------------------- /Java/Image Processing/README.md: -------------------------------------------------------------------------------- 1 | # Image Processing -------------------------------------------------------------------------------- /Java/Job Scheduling/README.md: -------------------------------------------------------------------------------- 1 | # Job Scheduling Algorithms -------------------------------------------------------------------------------- /Java/Machine Learning/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Java/Machine Learning/README.md -------------------------------------------------------------------------------- /Java/Maths/Binary_Exponentiation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import javax.lang.model.util.ElementScanner14; 3 | 4 | import java.lang.Math.*; 5 | public class Binary_Exponentiation { 6 | 7 | public static long exp(long number, long pow) { 8 | if (pow == (long) 1) 9 | return (long)number; 10 | else if (pow == (long) 0) 11 | return (long) 1; 12 | else { 13 | { 14 | if (pow % (long) 2 == 0) { 15 | return exp(number, pow/2) * exp(number, pow/2); 16 | } else{ 17 | return exp(number, (pow - 1) / 2) * exp(number, (pow - 1) / 2) * number; 18 | } 19 | } 20 | } 21 | 22 | } 23 | 24 | public static void main(String args[]){ 25 | 26 | Scanner scan = new Scanner(System.in); 27 | int num = scan.nextInt(); 28 | int pow = scan.nextInt(); 29 | 30 | System.out.println(exp((long) num, (long) pow)); 31 | 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Java/Maths/BinomialTheroem.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.lang.Math; 3 | 4 | public class BinomialTheroem{ 5 | public static int fact(int n){ 6 | int factn =1; 7 | for(int i=1; i factors(int n) { 13 | ArrayList ans = new ArrayList(); 14 | if (n < 1) return ans; 15 | ans.add(Long.valueOf(1)); 16 | long d = 0; 17 | if (n == 1) return ans; 18 | for (long i = 2; i <= Math.sqrt(n);i++) { 19 | if (n % i == 0) { 20 | ans.add(i); 21 | d = n / i; 22 | if (d != i) { 23 | ans.add(d); 24 | } 25 | } 26 | } 27 | 28 | ans.add(Long.valueOf(n)); 29 | Collections.sort(ans); 30 | return ans; 31 | } 32 | 33 | 34 | public static void main(String[] args) { 35 | System.out.println("Factors of 24 are:"); 36 | System.out.println(factors(24)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Java/Maths/KadaneAlgo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class KadaneAlgo { 5 | // function implementing Kadane's Algorithm 6 | static int maxSum(int ar[], int n) { 7 | int maxSum = Integer.MIN_VALUE, maxSumCurrIdx = 0; 8 | for(int i = 0; i < n; i++) { 9 | maxSumCurrIdx += ar[i]; 10 | if(maxSum < maxSumCurrIdx) maxSum = maxSumCurrIdx; 11 | if(maxSumCurrIdx < 0) maxSumCurrIdx = 0; 12 | } 13 | return maxSum; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // input code 18 | Scanner s = new Scanner(System.in); 19 | System.out.print("Enter array size : "); 20 | int n = s.nextInt(); 21 | int[] ar = new int[n]; 22 | System.out.print("Enter array elements : "); 23 | for(int i = 0; i < n; i++) ar[i] = s.nextInt(); 24 | 25 | // function call & output 26 | System.out.println("Max Sum : " + maxSum(ar, n)); 27 | } 28 | } -------------------------------------------------------------------------------- /Java/Maths/Pascal_Triangle.java: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 1 4 | 1 2 1 5 | 1 3 3 1 6 | 1 4 6 4 1 7 | 8 | Each number in the triangle is the sum of the two numbers directly above it. 9 | 10 | we take number of rows as input 11 | And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr. 12 | nCr=factorial(n)/(factorial(r)*factorial(n-r)) 13 | */ 14 | import java.util.Scanner; 15 | 16 | public class Pascal_Triangle { 17 | public static long factorial(int n) { 18 | long result = 1; 19 | for (int c = 1; c <= n; c++) { 20 | result *= c; 21 | } 22 | return result; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner scanner = new Scanner(System.in); 27 | 28 | System.out.println("Enter the number of rows you wish to see in Pascal's Triangle:"); 29 | int n = scanner.nextInt(); 30 | 31 | for (int i = 0; i < n; i++) { 32 | for (int c = 0; c <= (n - i - 2); c++) { 33 | System.out.print(" "); 34 | } 35 | 36 | for (int c = 0; c <= i; c++) { 37 | long coeff = factorial(i) / (factorial(c) * factorial(i - c)); 38 | System.out.print(coeff + " "); 39 | } 40 | 41 | System.out.println(); 42 | } 43 | scanner.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Java/Maths/PerfectNumber.java: -------------------------------------------------------------------------------- 1 | // Java program to check if the number is an Perfect number or not 2 | 3 | import java.util.Scanner; 4 | 5 | public class PerfectNumber { 6 | public static void main(String[] args) { 7 | // take user input 8 | Scanner sc = new Scanner(System.in); 9 | System.out.print("Enter a number : "); 10 | int N = sc.nextInt(); 11 | 12 | // initialize variable for loop and to store sum 13 | int i, sum = 0; 14 | 15 | // iterate from 1 to N and if the number is a factor of N then add it to sum 16 | for (i = 1; i < N; i++) { 17 | if (N % i == 0) { 18 | sum += i; 19 | } 20 | } 21 | 22 | // if sum of factors of N is equal to N then it is a perfect number 23 | if (sum == N) { 24 | System.out.print(N + " is a Perfect Number\n"); 25 | } else { 26 | System.out.print(N + " is not a Perfect Number\n"); 27 | } 28 | } 29 | } 30 | 31 | 32 | /******************************************************* 33 | Case 1: 34 | Enter a number: 153 35 | 153 is not a Perfect number 36 | 37 | Case 2: 38 | Enter a number : 496 39 | 496 is a Perfect Number 40 | *******************************************************/ 41 | -------------------------------------------------------------------------------- /Java/Maths/RussianPeasant.java: -------------------------------------------------------------------------------- 1 | //function to multiply them without using multiplication operator 2 | import java.util.*; 3 | import java.lang.*; 4 | import java.io.*; 5 | class RussianPeasant { 6 | // Function to multiply two 7 | // numbers using Russian Peasant method 8 | static int russianPeasant(int a, int b) 9 | { 10 | // initialize result 11 | int res = 0; 12 | 13 | // While second number doesn't become 1 14 | while (b > 0) 15 | { 16 | // If second number becomes odd, 17 | // add the first number to result 18 | if ((b & 1) != 0) 19 | res = res + a; 20 | // Double the first number 21 | // and halve the second number 22 | a = a << 1; 23 | b = b >> 1; 24 | } 25 | return res; 26 | } 27 | // driver program 28 | public static void main (String[] args) 29 | { 30 | Scanner sc = new Scanner(System.in); 31 | int a = sc.nextInt(); 32 | int b = sc.nextInt(); 33 | System.out.println(russianPeasant(a,b)); 34 | 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Java/Maths/ShriDharacharya.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.lang.Math; 3 | public class ShriDharacharya { 4 | public static void ShriDharacharyaFormulae(double a, double b, double c){ 5 | double compValue = (Math.pow(b,2) - 4*a*c); 6 | double val1 = -b/(2*a); 7 | double val2 = Math.pow(compValue,.5)/(2*a); 8 | if(compValue>=0){ 9 | System.out.println((val1+val2) + "and " + (val1-val2)); 10 | } 11 | else{ 12 | System.out.println("Imaginary roots"); 13 | } 14 | } 15 | public static void main(String[] args) { 16 | Scanner scan = new Scanner(System.in); 17 | double a = scan.nextDouble(); 18 | double b = scan.nextDouble(); 19 | double c = scan.nextDouble(); 20 | ShriDharacharyaFormulae(a,b,c); 21 | scan.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java/Maths/Sieve_erastonetihis.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Sieve_erastonetihis { 4 | public static void main(String[] args) { 5 | // If you have to find prime numbers till n 6 | Scanner sc = new Scanner(System.in); 7 | System.out.print("Enter a number: "); 8 | int n = sc.nextInt(); 9 | //creating boolean array to mark multiples of primes as true 10 | //So all true element will be prime numbers 11 | if(n<=1){ 12 | System.out.println("There is no prime numbers"); 13 | } 14 | boolean [] primes = new boolean[n+1]; 15 | sieve(n, primes); 16 | } 17 | public static void sieve(int n, boolean [] primes){ 18 | for(int i=2;i*i<=n;i++){ 19 | if(!primes[i]){ 20 | for(int j=2*i;j<=n;j+=i){ 21 | primes[j]= true; 22 | } 23 | } 24 | } 25 | for(int i=2;i<=n;i++){ 26 | if(!primes[i]){ 27 | System.out.print(i + " "); 28 | } 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Java/Maths/SimilarTriangles.java: -------------------------------------------------------------------------------- 1 | // Similar Triangles Program 2 | // It can be used to check whether the two traingles are similar or not. 3 | // Author: @swastik-akhil 4 | 5 | 6 | public class SimilarTriangles { 7 | public static void main(String[] args) { 8 | 9 | // SSS property 10 | 11 | 12 | int a1 = 3, b1 = 4, c1 = 5; 13 | int a2 = 6, b2 = 8, c2 = 10; 14 | if (a1/a2 == b1/b2 && b1/b2 == c1/c2) { 15 | System.out.println("The triangles are similar"); 16 | } else { 17 | System.out.println("The triangles are not similar"); 18 | } 19 | 20 | 21 | // AA property 22 | 23 | int x1 = 40, x2 = 70; 24 | int y1 = 40, y2 = 70; 25 | if(x1==y1 && x2==y2){ 26 | System.out.println("The triangles are similar"); 27 | } else { 28 | System.out.println("The triangles are not similar"); 29 | } 30 | 31 | 32 | //SAS property 33 | 34 | 35 | int a3= 3,m = 50, b3 = 4 ; 36 | int a4 = 6, n = 50, b4 = 8; 37 | if (a3/a4 == b3/b4 && m == n) { 38 | System.out.println("The triangles are similar"); 39 | } else { 40 | System.out.println("The triangles are not similar"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Java/Maths/bisection.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Bisection Method for solving equations 2 | class bisection{ 3 | static final float EPSILON = (float)0.01; 4 | // An example function whose solution is determined using 5 | // Bisection Method. The function is x^3 - x^2 + 2 6 | static double func(double x){ 7 | return x*x*x - x*x + 2; 8 | } 9 | // Prints root of func(x) with error of EPSILON 10 | static void bisection(double a, double b){ 11 | if (func(a) * func(b) >= 0){ 12 | System.out.println("You have not assumed" 13 | + " right a and b"); 14 | return; 15 | } 16 | double c = a; 17 | while ((b-a) >= EPSILON){ 18 | // Find middle point 19 | c = (a+b)/2; 20 | // Check if middle point is root 21 | if (func(c) == 0.0) 22 | break; 23 | // Decide the side to repeat the steps 24 | else if (func(c)*func(a) < 0){ 25 | b = c; 26 | System.out.printf("Approx value of root is : %.4f\n",c); 27 | } 28 | else{ 29 | a = c; 30 | System.out.printf("Approx value of root is : %.4f\n",c); 31 | } 32 | } 33 | //prints value of c upto 4 decimal places 34 | System.out.printf("The value of root is : %.4f",c); 35 | } 36 | // Driver program to test above function 37 | public static void main(String[] args){ 38 | // Initial values assumed 39 | double a =-200, b = 300; 40 | bisection(a, b); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Java/Maths/factorial.java: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | 3 | FACTORIAL PROGRAM 4 | Factorial is represented by !. It is calculated by mutiplying natural 5 | numbers upto that number, i.e., n! = 1 x 2 x 3... x n. 6 | (n should be positive) 7 | Note: 0! = 1 and 1! = 1 8 | 9 | **********************************************************************/ 10 | 11 | import java.util.Scanner; // scanner class 12 | 13 | public class factorial 14 | { 15 | static int factorialNum(int num){ 16 | if(num == 0 || num == 1) // factorial of 0 and 1 is 1 17 | return 1; 18 | else // if num > 1 19 | return (num * factorialNum(num - 1)); 20 | } 21 | 22 | public static void main(String[] args) { 23 | 24 | Scanner scan = new Scanner(System.in); 25 | System.out.print("Enter a number:"); 26 | int num = scan.nextInt(); //factorial of this no will be calculated 27 | System.out.println("Factorial of " + num + " is " + factorialNum(num)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Java/Maths/heronsFormula.java: -------------------------------------------------------------------------------- 1 | //This code takes input of three sides of a traingle from the user and returns it's are which is calculated using Heron's Formula. In symbols, if a, b, and c are the lengths of the sides: Area = Square root of√s(s - a)(s - b)(s - c) where s is half the perimeter, or (a + b + c)/2. 2 | 3 | import java.util.Scanner; 4 | 5 | public class heronsFormula { 6 | 7 | public static void main(String[] args) { 8 | double s1, s2, s3, area, S; 9 | // Scanner scanner; 10 | Scanner sc = new Scanner(System.in); 11 | // Take input from user 12 | System.out.println("Enter Three Sides of a Triangle"); 13 | s1 = sc.nextDouble(); 14 | s2 = sc.nextDouble(); 15 | s3 = sc.nextDouble(); 16 | 17 | S = (s1 + s2 + s3) / 2; 18 | area = Math.sqrt(S * (S - s1) * (S - s2) * (S - s3)); 19 | 20 | System.out.format("The Area of triangle = %.2f\n", area); 21 | sc.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java/Maths/is_prime.java: -------------------------------------------------------------------------------- 1 | public class PrimeExample{ 2 | public static void main(String args[]){ 3 | int i,m=0,flag=0; 4 | int n=3;//it is the number to be checked 5 | m=n/2; 6 | if(n==0||n==1){ 7 | System.out.println(n+" is not prime number"); 8 | }else{ 9 | for(i=2;i<=m;i++){ 10 | if(n%i==0){ 11 | System.out.println(n+" is not prime number"); 12 | flag=1; 13 | break; 14 | } 15 | } 16 | if(flag==0) { System.out.println(n+" is prime number"); } 17 | }//end of else 18 | } 19 | } -------------------------------------------------------------------------------- /Java/Memory Allocation/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation -------------------------------------------------------------------------------- /Java/Optimization/README.md: -------------------------------------------------------------------------------- 1 | # Optimization -------------------------------------------------------------------------------- /Java/Pattern-Matching/README.md: -------------------------------------------------------------------------------- 1 | # Pattern Matching -------------------------------------------------------------------------------- /Java/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198799154-7942a4f7-0cd8-4863-9b9d-81b32e0706d7.png) 2 | -------------------------------------------------------------------------------- /Java/Recursion/Permutations.java: -------------------------------------------------------------------------------- 1 | public class Permutations { 2 | 3 | public static void main(String[] args) { 4 | permutations("", "abc"); 5 | } 6 | 7 | static void permutations(String p, String up) { 8 | // Base case: If the remaining characters to be permuted is empty, print the permutation 9 | if (up.isEmpty()) { 10 | System.out.println(p); 11 | return; 12 | } 13 | 14 | char ch = up.charAt(0); 15 | 16 | for (int i = 0; i <= p.length(); i++) { 17 | // Split the permutation string into two parts 18 | String f = p.substring(0, i); // First part 19 | String s = p.substring(i, p.length()); // Second part 20 | 21 | // Recursive call: Generate permutations by inserting the current character at each possible position 22 | permutations(f + ch + s, up.substring(1)); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Java/Recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion -------------------------------------------------------------------------------- /Java/Searching/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198502361-93fadfda-9766-4b3f-9d53-d642a328766a.png) 2 | -------------------------------------------------------------------------------- /Java/Searching/binary_search_2d.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class binary_search_2d{ 4 | 5 | public static int[] search(int[][] matrix, int target){ 6 | int row = 0; 7 | int col = matrix.length - 1; 8 | 9 | while(row < matrix.length && col >= 0){ 10 | if(matrix[row][col] == target){ 11 | return new int[]{row, col}; 12 | } 13 | if(matrix[row][col] < target){ 14 | row++; 15 | } 16 | else{ 17 | col--; 18 | } 19 | } 20 | //if not found in the matrix, return [-1, -1] 21 | return new int[]{-1, -1}; 22 | } 23 | public static void main(String[] args){ 24 | 25 | Scanner sc = new Scanner(System.in); 26 | int [][] matrix= { 27 | {10, 20, 30, 40}, 28 | {15, 25, 35, 45}, 29 | {28, 29, 37, 49}, 30 | {33, 34, 38, 50} 31 | }; 32 | System.out.print("Enter the number to be searched in the 2D Matrix : "); 33 | int n = sc.nextInt(); 34 | System.out.println(Arrays.toString(search(matrix, n))); 35 | sc.close(); 36 | } 37 | } -------------------------------------------------------------------------------- /Java/Signal Processing/README.md: -------------------------------------------------------------------------------- 1 | # Signal Processing -------------------------------------------------------------------------------- /Java/Sliding Window/README.md: -------------------------------------------------------------------------------- 1 | # Sliding Window Algorithm -------------------------------------------------------------------------------- /Java/Sorting/Odd_Even_Sort.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Odd_Even_Sort { 4 | public static void oddEvenSort(int arr[], int n) 5 | { 6 | boolean isSorted = false; // Initially array is unsorted 7 | 8 | while (!isSorted) { 9 | isSorted = true; 10 | int temp = 0; 11 | 12 | // Perform Bubble sort on odd indexed element 13 | for (int i = 1; i <= n - 2; i = i + 2) { 14 | if (arr[i] > arr[i + 1]) { 15 | temp = arr[i]; 16 | arr[i] = arr[i + 1]; 17 | arr[i + 1] = temp; 18 | isSorted = false; 19 | } 20 | } 21 | 22 | // Perform Bubble sort on even indexed element 23 | for (int i = 0; i <= n - 2; i = i + 2) { 24 | if (arr[i] > arr[i + 1]) { 25 | temp = arr[i]; 26 | arr[i] = arr[i + 1]; 27 | arr[i + 1] = temp; 28 | isSorted = false; 29 | } 30 | } 31 | } 32 | 33 | return; 34 | } 35 | public static void main(String[] args) 36 | { 37 | int arr[] = {50, 10, 2, -4, 0}; 38 | int n = arr.length; 39 | 40 | oddEvenSort(arr, n); 41 | for (int i = 0; i < n; i++) 42 | System.out.print(arr[i] + " "); 43 | 44 | System.out.println(" "); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Java/Sorting/README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | -------------------------------------------------------------------------------- /Java/Sorting/bubble_sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class bubble_sort { 4 | public static void main(String args[]) { 5 | int num; 6 | int temp = 0; 7 | try (Scanner s = new Scanner(System.in)) { 8 | System.out.print("ENTER THE NUMBER OF ELEMENTS: "); 9 | 10 | num = s.nextInt(); 11 | 12 | int[] array = new int[num]; 13 | System.out.println("ENTER THE ELEMENTS OF ARRAY: "); 14 | for (int i = 0; i < num; i++) { 15 | array[i] = s.nextInt(); 16 | } 17 | System.out.println("UNSORTED ARRAY: "); 18 | 19 | for (int i = 0; i < num; i++) { 20 | System.out.println(array[i]); 21 | } 22 | 23 | for (int i = 0; i < num; i++) { 24 | for (int j = 1; j < (num - i); j++) { 25 | if (array[j - 1] > array[j]) { 26 | 27 | temp = array[j - 1]; 28 | array[j - 1] = array[j]; 29 | array[j] = temp; 30 | } 31 | 32 | } 33 | } 34 | System.out.println("SORTED ARRAY: "); 35 | for (int i = 0; i < num; i++) { 36 | System.out.println(array[i]); 37 | } 38 | } 39 | 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Java/Sorting/cyclic_sort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Cylic Sort is a sorting algorithm ideally used if the question 3 | reads "given range of numbers from 0 to N or 1 to N" 4 | */ 5 | 6 | /* 7 | Approach used : 8 | Check if the number is in the correct index, if not swap, and then continue iterating 9 | */ 10 | import java.util.*; 11 | public class cyclic_sort{ 12 | 13 | //swapping 14 | public static void swap(int[] arr, int first, int second){ 15 | int temp = arr[first]; 16 | arr[first] = arr[second]; 17 | arr[second] = temp; 18 | } 19 | 20 | public static void cycleSort(int[] arr){ 21 | int i = 0; 22 | while(i < arr.length){ 23 | int correctIndex = arr[i] - 1; 24 | /*if the question reads 1 to N, correctIndex = arr[i] - 1; 25 | if the question reads from 0 to N, correctIndex = arr[i]; */ 26 | 27 | if(arr[i] != arr[correctIndex]){ 28 | swap(arr, i, correctIndex); 29 | } 30 | else{ 31 | i++; 32 | } 33 | } 34 | } 35 | public static void main(String[] args){ 36 | int[] arr = {3, 5, 2, 1, 4}; 37 | cycleSort(arr); 38 | System.out.println(Arrays.toString(arr)); 39 | } 40 | } -------------------------------------------------------------------------------- /Java/Sorting/insertion_sort.java: -------------------------------------------------------------------------------- 1 | public class insertion_sort 2 | { 3 | public static void sort(int a[],int n) { 4 | for (int j = 1; j < n; j++) { 5 | int k = a[j]; 6 | int i = j-1; 7 | while ( (i > -1) && ( a [i] > k ) ) { 8 | a [i+1] = a [i]; 9 | i--; 10 | } 11 | a[i+1] = k; 12 | } 13 | for(int i:a){ 14 | System.out.print(i+" "); 15 | } 16 | System.out.println(); 17 | } 18 | 19 | public static void main(String[] args) { 20 | int[] arr = {45, 3, 2, 89,77}; 21 | int n = arr.length; 22 | sort(arr, n); 23 | } 24 | } -------------------------------------------------------------------------------- /Java/Sorting/shell_sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class shell_sort { 4 | public static void shellSort(int[] arr) { 5 | int n = arr.length; 6 | 7 | // Start with a big gap, then reduce the gap 8 | for (int gap = n / 2; gap > 0; gap /= 2) { 9 | // Do a gapped insertion sort for this gap size. 10 | // The first gap elements a[0..gap-1] are already in gapped order. 11 | // Keep adding one more element until the entire array is gap sorted. 12 | for (int i = gap; i < n; i++) { 13 | int temp = arr[i]; 14 | int j = i; 15 | while (j >= gap && arr[j - gap] > temp) { 16 | // Shift earlier gap-sorted elements up until the correct location for a[i] is found. 17 | arr[j] = arr[j - gap]; 18 | j -= gap; 19 | } 20 | // Put temp (the original a[i]) in its correct location. 21 | arr[j] = temp; 22 | } 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | int[] arr = {9, 8, 1, 7, 4, 3}; 28 | System.out.println("Array before sorting:"); 29 | System.out.println(Arrays.toString(arr)); 30 | shellSort(arr); 31 | System.out.println("Array after sorting:"); 32 | System.out.println(Arrays.toString(arr)); 33 | } 34 | } -------------------------------------------------------------------------------- /Java/Sorting/wave_sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class wave_sort { 4 | public static void wave_sort(int[] arr) { 5 | // Sorting the array in ascending order 6 | Arrays.sort(arr); 7 | 8 | // Swapping the elements in pairs 9 | for (int i = 0; i < arr.length - 1; i += 2) { 10 | int temp = arr[i]; 11 | arr[i] = arr[i + 1]; 12 | arr[i + 1] = temp; 13 | } 14 | } 15 | 16 | public static void main(String[] args) { 17 | int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 18 | 19 | waveSort(arr); 20 | 21 | System.out.println(Arrays.toString(arr)); // [2, 1, 4, 3, 6, 5, 8, 7, 9] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Java/Stack/README.md: -------------------------------------------------------------------------------- 1 | # Stacks -------------------------------------------------------------------------------- /Java/Sub-Arrays/BoyerMoore.java: -------------------------------------------------------------------------------- 1 | // The Boyer-Moore Majority Voting Algorithm is an efficient algorithm used to find the majority element in an array, if it exists. A majority element is an element that appears more than n/2 times in an array of length n. 2 | // The algorithm works by maintaining a candidate variable and a count variable. It iterates through the array, updating the candidate and count based on certain conditions 3 | 4 | public class BoyerMoore { 5 | public static void main(String[] args) { 6 | int arr [] = {2,1,2,2,2,1,1,3,2}; 7 | int x=1, count =1, element = arr[0]; 8 | while(x0: 17 | count += res & 1 18 | res >>= 1 19 | 20 | print("THe result is: ", count) 21 | #printing the value of bits -------------------------------------------------------------------------------- /Python/Caching/README.md: -------------------------------------------------------------------------------- 1 | # Caching -------------------------------------------------------------------------------- /Python/Concurrent Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Concurrent Algorithms -------------------------------------------------------------------------------- /Python/Cryptography/MorseCodeConvortor.py: -------------------------------------------------------------------------------- 1 | morse_code_map = { 2 | 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 3 | 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 4 | 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..', 5 | '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', 6 | ' ': '/' 7 | } 8 | 9 | def convert_to_morse_code(text): 10 | morse_code = [] 11 | text = text.upper() 12 | 13 | for char in text: 14 | if char in morse_code_map: 15 | morse_code.append(morse_code_map[char]) 16 | morse_code.append(' ') # Add space between characters 17 | 18 | return ''.join(morse_code) 19 | 20 | def main(): 21 | text = input("Enter a text string: ") 22 | morse_code = convert_to_morse_code(text) 23 | 24 | print("Text:", text) 25 | print("Morse Code:", morse_code) 26 | 27 | if __name__ == "__main__": 28 | main() 29 | -------------------------------------------------------------------------------- /Python/Cryptography/README.md: -------------------------------------------------------------------------------- 1 | # Cryptography 2 | -------------------------------------------------------------------------------- /Python/Data Compression/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/210195009-d3d0f6df-8530-4c47-90b6-c00534c6edce.png) 2 | -------------------------------------------------------------------------------- /Python/Deadlock Avoidance/README.md: -------------------------------------------------------------------------------- 1 | # Deadlock Avoidance -------------------------------------------------------------------------------- /Python/Divide-and-Conquer/README.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer -------------------------------------------------------------------------------- /Python/Dynamic-Programming/01_knapsack.py: -------------------------------------------------------------------------------- 1 | #0-1 Knapsack Problem in best possible Time & Space Complexity 2 | #Time complexity = O(N*N) 3 | #Space complexity = O(N) 4 | 5 | def knapSack(W, wt, val, n): 6 | 7 | dp = [0 for i in range(W+1)] 8 | 9 | for i in range(n): 10 | for j in range(W, 0, -1): 11 | if j-wt[i]>=0: 12 | dp[j] = max(dp[j], val[i]+dp[j-wt[i]]) 13 | 14 | return dp[-1] 15 | 16 | print('Enter the maximum capacity of Knapsack:') 17 | W = int(input()) 18 | 19 | print('Enter the number of items available:') 20 | n = int(input()) 21 | 22 | print('Enter the weights of items (in space separated format):') 23 | wt = [int(i) for i in input().split(' ')] 24 | 25 | print('Enter the values of items (in space separated format):') 26 | val = [int(i) for i in input().split(' ')] 27 | 28 | res = knapSack(W, wt, val, n) 29 | 30 | print('Maximum value that can be attained: {}'.format(res)) 31 | -------------------------------------------------------------------------------- /Python/Dynamic-Programming/Fibonacci_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author : K.om Senapati 3 | Github username : KOMNOOB 4 | Problem Statement: Fibonacci Number 5 | Problem Description 6 | The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. Given an integer n, your task is to find the nth Fibonacci number. 7 | 8 | Write a function fibonacci(n) that takes in an integer n and returns the nth Fibonacci number. 9 | 10 | Input : 11 | An integer n representing the position of the Fibonacci number to be found. (0 <= n <= 30) 12 | Output : 13 | An integer representing the nth Fibonacci number. 14 | ''' 15 | def fibonacci(n: int) -> int: 16 | """ 17 | Returns nth fibonacci number 18 | """ 19 | if n <= 0: 20 | raise Exception("n must be higher than 0") 21 | if n in (1,2): # Base case for handling errors 22 | return 1 23 | memo = [0] * (n+1) 24 | 25 | memo[0] = 0 26 | memo[1] = 1 27 | 28 | for i in range(2,n+1): 29 | memo[i] = memo[i-1] + memo[i-2] 30 | 31 | return memo[n] 32 | 33 | if __name__ == "__main__": 34 | n = int(input("Enter the n value : ")) 35 | print(f"The fibonacci number at position n = {fibonacci(n)}") -------------------------------------------------------------------------------- /Python/Dynamic-Programming/Longest_Common_Subsequence.py: -------------------------------------------------------------------------------- 1 | string1 = input("Enter first string:") 2 | string2 = input("Enter second string:") 3 | 4 | n, m = len(string1), len(string2) 5 | lcs = [] 6 | for i in range(n+1): 7 | lst = [] 8 | for j in range(m+1): 9 | lst.append(0) 10 | lcs.append(lst) 11 | print(lcs) 12 | 13 | for i in range(n+1): 14 | for j in range(m+1): 15 | if i == 0 or j == 0: 16 | lcs[i][j] = 0 17 | elif string1[i-1] == string2[j-1]: 18 | lcs[i][j] = lcs[i-1][j-1]+1 19 | else: 20 | lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1]) 21 | print(lcs) 22 | 23 | print("Longest Common Subsequence: ", lcs[m][n]) 24 | 25 | i = n 26 | j = m 27 | temp = "" 28 | while i >= 1 and j >= 1: 29 | if string1[i-1] == string2[j-1]: 30 | temp += string1[i-1] 31 | i -= 1 32 | j -= 1 33 | elif lcs[i-1][j] > lcs[i][j-1]: 34 | i -= 1 35 | else: 36 | j -= 1 37 | print("LCS is: ",temp[::-1]) 38 | -------------------------------------------------------------------------------- /Python/Dynamic-Programming/Min_Jump.py: -------------------------------------------------------------------------------- 1 | #Name : Atul Kumar 2 | #Github username : atul1510 3 | #Repositary name : Algorithms 4 | # Problem Statement: C++ program to find find the minimum number of jumps to reach the other end of the array starting from the 0th index. And if the end isn’t reachable, return -1 as the output. 5 | #Example: 6 | #1] INPUT: int arr[] = { 1, 9, 4, 7, 3, 7, 2, 1, 8, 9, 5 }; 7 | #OUTPUT: Minimum number of jumps to reach the other end is 2 */ 8 | # Python3 program to find Minimum number of jumps to reach end 9 | # Returns minimum number of jumps to reach arr[n-1] from arr[0] 10 | def minJump(arr, x): 11 | jump = [0 for i in range(x)] 12 | if (x == 0) or (arr[0] == 0): 13 | return float('inf') 14 | jump[0] = 0 15 | # To Find the minimum number of jumps to reach arr[i] from 16 | for i in range(1, x): 17 | jump[i] = float('inf') 18 | for j in range(i): 19 | if (i <= j + arr[j]) and (jump[j] != float('inf')): # arr[0] and assign this value to jumps[i] 20 | jump[i] = min(jump[i], jump[j] + 1) 21 | break 22 | return jump[x-1] 23 | # Main Function 24 | arr = [ 1, 9, 4, 7, 3, 7, 2, 1, 8, 9, 5 ] 25 | size = len(arr) 26 | print('Minimum number of jumps to reach', 27 | 'end is', minJump(arr, size)) -------------------------------------------------------------------------------- /Python/Dynamic-Programming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming -------------------------------------------------------------------------------- /Python/Dynamic-Programming/Shortest Common Supersequence Problem.py: -------------------------------------------------------------------------------- 1 | # Function to find the length of the shortest common supersequence of 2 | # sequences `X[0…m-1]` and `Y[0…n-1]` 3 | def SCSLength(X, Y, m, n): 4 | 5 | # if the end of either sequence is reached, return 6 | # the length of another sequence 7 | if m == 0 or n == 0: 8 | return n + m 9 | 10 | # if the last character of `X` and `Y` matches 11 | if X[m - 1] == Y[n - 1]: 12 | return SCSLength(X, Y, m - 1, n - 1) + 1 13 | 14 | # last character of `X` and `Y` don't match 15 | return min(SCSLength(X, Y, m, n - 1) + 1, SCSLength(X, Y, m - 1, n) + 1) 16 | 17 | 18 | if __name__ == '__main__': 19 | 20 | X = 'ABCBDAB' 21 | Y = 'BDCABA' 22 | 23 | m = len(X) 24 | n = len(Y) 25 | 26 | print('The length of the shortest common supersequence is', SCSLength(X, Y, m, n)) -------------------------------------------------------------------------------- /Python/Dynamic-Programming/coinchange.py: -------------------------------------------------------------------------------- 1 | def numberOfWays(coins, n, sum): 2 | dp = [[0 for i in range(sum+1)] for j in range(n+1)] #declaring a dp 3 | 4 | for i in range(n+1): 5 | dp[i][0] = 1 6 | 7 | for i in range(1, n+1): 8 | for j in range(1, sum+1): 9 | if coins[i-1]<=j: 10 | dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j] 11 | 12 | else: 13 | dp[i][j] = dp[i-1][j] 14 | return dp[n][sum] 15 | 16 | n = int(input()) 17 | sum = int(input()) 18 | 19 | coins = list(map(int,input().strip().split())) 20 | 21 | print(numberOfWays(coins,n,sum)) -------------------------------------------------------------------------------- /Python/Dynamic-Programming/edit_distance.py: -------------------------------------------------------------------------------- 1 | def editDistRec(s1, s2, m, n): 2 | # If first string is empty, the only option is to 3 | # insert all characters of second string into first 4 | if m == 0: 5 | return n 6 | 7 | # If second string is empty, the only option is to 8 | # remove all characters of first string 9 | if n == 0: 10 | return m 11 | 12 | # If last characters of two strings are same, nothing 13 | # much to do. Get the count for 14 | # remaining strings. 15 | if s1[m - 1] == s2[n - 1]: 16 | return editDistRec(s1, s2, m - 1, n - 1) 17 | 18 | # If last characters are not same, consider all three 19 | # operations on last character of first string, 20 | # recursively compute minimum cost for all three 21 | # operations and take minimum of three values. 22 | return 1 + min(editDistRec(s1, s2, m, n - 1), # Insert 23 | editDistRec(s1, s2, m - 1, n), # Remove 24 | editDistRec(s1, s2, m - 1, n - 1) # Replace 25 | ) 26 | 27 | def editDist(s1, s2): 28 | return editDistRec(s1, s2, len(s1), len(s2)) 29 | 30 | # Driver code 31 | s1 = "GEEXSFRGEEKKS" 32 | s2 = "GEEKSFORGEEKS" 33 | print(editDist(s1, s2)) -------------------------------------------------------------------------------- /Python/Game Theory/README.md: -------------------------------------------------------------------------------- 1 | # Game Theory -------------------------------------------------------------------------------- /Python/Graphics/README.md: -------------------------------------------------------------------------------- 1 | # Computer Graphics -------------------------------------------------------------------------------- /Python/Graphs/MultiSource_Shortest_Path.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | print("Enter the number of vertices, edges, and sources") 4 | n, m, s = map(int, input().split()) 5 | adj = [[] for _ in range(n+1)] 6 | sources = input("Enter the sources ").split() 7 | 8 | print("Enter the edges:") 9 | 10 | while m > 0: 11 | u, v = map(int, input().split()) 12 | 13 | adj[u].append(v) 14 | adj[v].append(u) 15 | 16 | m -= 1 17 | 18 | dist = [1e9] * (n + 1) 19 | vis = [0] * (n + 1) 20 | q = deque() 21 | 22 | for it in sources: 23 | it = int(it) 24 | vis[it] = 1 25 | q.append(it) 26 | 27 | lvl = 0 28 | 29 | while q: 30 | sz = len(q) 31 | while sz > 0: 32 | node = q.popleft() 33 | 34 | dist[node] = lvl 35 | 36 | for it in adj[node]: 37 | if vis[it]: 38 | continue 39 | 40 | vis[it] = 1 41 | q.append(it) 42 | 43 | sz -= 1 44 | 45 | lvl += 1 46 | 47 | for i in range(1, n+1): 48 | print(i, ":", dist[i]) -------------------------------------------------------------------------------- /Python/Graphs/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198797501-bd7d085a-062f-4475-9cf8-8ddfc85357c7.png) 2 | -------------------------------------------------------------------------------- /Python/Graphs/floyd_warshall.py: -------------------------------------------------------------------------------- 1 | INF = float('inf') 2 | 3 | def printmatrix(m): 4 | r, c = len(m), len(m[0]) 5 | for i in range(r): 6 | for j in range(c): 7 | print(m[i][j], end=" ") 8 | print() 9 | 10 | v, e = map(int, input().split()) 11 | m = [[None]*v for i in range(v)] 12 | for i in range(v): 13 | for j in range(v): 14 | # when source = destination 15 | if i == j: 16 | m[i][j] = 0 17 | # when edge doesn't exist 18 | else: 19 | m[i][j] = INF 20 | 21 | # Taking input from user 22 | for i in range(e): 23 | src, dst, wt = map(int, input().split()) 24 | m[src][dst] = wt 25 | 26 | print("MATRIX BEFORE USING FLOYD WARSHALL: \n") 27 | printmatrix(m) 28 | print("\n\n") 29 | for k in range(v): 30 | for i in range(v): 31 | for j in range(v): 32 | if m[i][j] + m[k][j] < m[i][j]: 33 | m[i][j] = m[i][j] + m[k][j] 34 | print("MATRIX AFTER USING FLOYD WARSHALL: \n") 35 | printmatrix(m) 36 | -------------------------------------------------------------------------------- /Python/Greedy-Algorithm/Activity_Selection.py: -------------------------------------------------------------------------------- 1 | def MaxActivities(arr, n): 2 | selected = [] 3 | # Sort jobs according to finish time 4 | Activity.sort(key=lambda x: x[1]) 5 | # The first activity always gets selected 6 | i = 0 7 | selected.append(arr[i]) 8 | for j in range(1, n): 9 | '''If this activity has start time greater than or 10 | equal to the finish time of previously selected 11 | activity, then select it''' 12 | if arr[j][0] >= arr[i][1]: 13 | selected.append(arr[j]) 14 | i = j 15 | return selected 16 | 17 | # Driver code 18 | if __name__ == '__main__': 19 | print("Enter no. of elements") 20 | n=int(input()) 21 | print("Enter the starting and ending time as integer elements") 22 | Activity=[] 23 | for i in range(n): 24 | a=[] 25 | for j in range(2): 26 | a.append(int(input())) 27 | Activity.append(a) 28 | 29 | # Function call 30 | selected = MaxActivities(Activity, n) 31 | print("Following activities are selected :") 32 | print(selected[0], end = "") 33 | for i in range (1, len(selected)): 34 | print(",", end = " ") 35 | print(selected[i], end = "") 36 | -------------------------------------------------------------------------------- /Python/Greedy-Algorithm/Egyptian_Fraction.py: -------------------------------------------------------------------------------- 1 | # Python program to print a fraction in Egyptian Form using Greedy Algorithm 2 | 3 | def egyptianFraction(n, d): 4 | # When Both Numerator and denominator becomes zero then we simply return; 5 | if d == 0 or n == 0: 6 | return 7 | if d % n == 0: 8 | print("1/", d // n, sep='', end='') 9 | return 10 | if n % d == 0: 11 | print(n // d, end='') 12 | return 13 | if n > d: 14 | print(n // d, " + ", end='') 15 | egyptianFraction(n % d, d) 16 | return 17 | x = d // n + 1 18 | print("1/", x, " + ", end='', sep='') 19 | egyptianFraction(n * x - d, d * x) 20 | 21 | # Main Function 22 | numerator = int(input("Enter Numerator: ")) 23 | denominator = int(input("Enter Denominator: ")) 24 | print("Egyptian Fraction representation of", numerator, "/", denominator, "is") 25 | egyptianFraction(numerator, denominator) -------------------------------------------------------------------------------- /Python/Greedy-Algorithm/Merge_Interval.py: -------------------------------------------------------------------------------- 1 | #Name : Atul Kumar 2 | #Github username : atul1510 3 | #Repositary name : Algorithms 4 | #Problem Statement : Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, 5 | #and return an array of the non-overlapping intervals that cover all the intervals in the input. 6 | def MI(arr): 7 | # Sorting based on the increasing order 8 | # of the start intervals 9 | arr.sort(key=lambda x: x[0]) 10 | # Stores index of last element 11 | # in output array (modified arr[]) 12 | index = 0 13 | # Traverse all input Intervals starting from 14 | # second interval 15 | for i in range(1, len(arr)): 16 | # If this is not first Interval and overlaps 17 | # with the previous one, Merge previous and 18 | # current Intervals 19 | if (arr[index][1] >= arr[i][0]): 20 | arr[index][1] = max(arr[index][1], arr[i][1]) 21 | else: 22 | index = index + 1 23 | arr[index] = arr[i] 24 | print("The Merged Intervals are :", end=" ") 25 | for i in range(index+1): 26 | print(arr[i], end=" ") 27 | # Main Finction 28 | arr = [[1, 3], [2, 6], [8, 10], [15, 18]] 29 | MI(arr) -------------------------------------------------------------------------------- /Python/Hashing/README.md: -------------------------------------------------------------------------------- 1 | # Hashing -------------------------------------------------------------------------------- /Python/Image Processing/Canny Edge Detection Algorithm/dog.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Python/Image Processing/Canny Edge Detection Algorithm/dog.jpeg -------------------------------------------------------------------------------- /Python/Image Processing/README.md: -------------------------------------------------------------------------------- 1 | # Image Processing -------------------------------------------------------------------------------- /Python/Job Scheduling/README.md: -------------------------------------------------------------------------------- 1 | # Job Scheduling Algorithms -------------------------------------------------------------------------------- /Python/Linked-List/linklist_length.py: -------------------------------------------------------------------------------- 1 | #Name : Atul Kumar 2 | #Github username : atul1510 3 | #Repositary name : Algorithms 4 | # Node class 5 | class Node: 6 | def __init__(self, data): 7 | self.data = data 8 | self.next = None 9 | # Function to find the length of a singly linked list 10 | def find_length(head): 11 | count = 0 12 | current = head 13 | while current != None: 14 | count += 1 15 | current = current.next 16 | return count 17 | # Creating an empty linked list 18 | head = None 19 | # Reading input from the user to create the linked list 20 | n = int(input("Enter the number of nodes in the linked list: ")) 21 | if n > 0: 22 | prev = None 23 | for i in range(n): 24 | data = int(input("Enter the data for node " + str(i+1) + ": ")) 25 | current = Node(data) 26 | if prev == None: 27 | head = current 28 | else: 29 | prev.next = current 30 | prev = current 31 | # Finding the length of the linked list 32 | length = find_length(head) 33 | # Printing the length of the linked list 34 | print("Length of the linked list is:", length) -------------------------------------------------------------------------------- /Python/Machine Learning/Decision Tree/PlayTennis.csv: -------------------------------------------------------------------------------- 1 | outlook,temp,humidity,windy,play 2 | sunny,hot,high,false,no 3 | sunny,hot,high,true,no 4 | overcast,hot,high,false,yes 5 | rainy,mild,high,false,yes 6 | rainy,cool,normal,false,yes 7 | rainy,cool,normal,true,no 8 | overcast,cool,normal,true,yes 9 | sunny,mild,high,false,no 10 | sunny,cool,normal,false,yes 11 | rainy,mild,normal,false,yes 12 | sunny,mild,normal,true,yes 13 | overcast,mild,high,true,yes 14 | overcast,hot,normal,false,yes 15 | rainy,mild,high,true,no 16 | -------------------------------------------------------------------------------- /Python/Machine Learning/Linear-Regression/plots/demo2d_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Python/Machine Learning/Linear-Regression/plots/demo2d_plot.png -------------------------------------------------------------------------------- /Python/Machine Learning/Linear-Regression/plots/demo3d_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kumar-laxmi/Algorithms/ddbfcef1895f59a3c7086bb0204a6d4500d98db8/Python/Machine Learning/Linear-Regression/plots/demo3d_plot.png -------------------------------------------------------------------------------- /Python/Machine Learning/Linear-Regression/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.23.5 2 | pandas==1.5.2 3 | matplotlib==3.6.2 4 | -------------------------------------------------------------------------------- /Python/Machine Learning/Logistic-Regression/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.23.5 2 | pandas==1.5.2 3 | -------------------------------------------------------------------------------- /Python/Maths/Binary_expo.py: -------------------------------------------------------------------------------- 1 | """ 2 | Binary exponentiation (also known as exponentiation by squaring) 3 | is a trick which allows to calculate a^n using only O(log n) multiplications (instead of 4 | O(n) multiplications required by the naive approach). 5 | 6 | The final complexity of this algorithm is 7 | O(log n): we have to compute log n powers of 8 | a, and then have to do at most log n multiplications to get the final answer from them. 9 | """ 10 | 11 | def binary(Base, Exponent): 12 | if Exponent == 0: 13 | return 1 14 | res = binary(Base, Exponent // 2) 15 | if Exponent % 2 == 0: 16 | return res * res 17 | else: 18 | return res * res * Base 19 | 20 | Base = int(input("Enter the Base: ")) 21 | Exponent = int(input("Enter the Exponent: ")) 22 | Answer = binary(Base, Exponent) 23 | print("Answer is", Answer) 24 | 25 | 26 | -------------------------------------------------------------------------------- /Python/Maths/BinomialTheorem.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def fact(n): 4 | factn = 1 5 | for i in range(1, n + 1): 6 | factn *= i 7 | return factn 8 | 9 | print("Enter in (1+x)^n the values of x and n respectively") 10 | x = int(input()) 11 | n = int(input()) 12 | nf = fact(n) 13 | arr = [0] * (n + 1) 14 | 15 | for i in range(n + 1): 16 | ncr = nf // (fact(i) * fact(n - i)) 17 | arr[i] = ncr * pow(x, i) 18 | arr[n - i] = ncr * pow(x, n - i) 19 | 20 | for i in arr: 21 | print(i, end=" ") 22 | print() 23 | -------------------------------------------------------------------------------- /Python/Maths/CongruencyOfTriangles.py: -------------------------------------------------------------------------------- 1 | """Congruency of Triangles Program 2 | It can be used to check whether the two traingles are congruent or not. 3 | Author: @swastik-akhil 4 | """ 5 | 6 | # SSS property 7 | a1, b1, c1 = 3, 4, 5 8 | a2, b2, c2 = 3, 4, 5 9 | 10 | if a1 == a2 and b1 == b2 and c1 == c2: 11 | print("The triangles are congruent") 12 | else: 13 | print("The triangles are not congruent") 14 | 15 | # SAS property 16 | a3, m, b3 = 3, 50, 4 17 | a4, n, b4 = 3, 50, 4 18 | 19 | if a3 == a4 and b3 == b4 and m == n: 20 | print("The triangles are congruent") 21 | else: 22 | print("The triangles are not congruent") 23 | 24 | # ASA property 25 | m1, a5, m2 = 40, 3, 60 26 | m3, a6, m4 = 40, 3, 60 27 | 28 | if m1 == m3 and m2 == m4 and a5 == a6: 29 | print("The triangles are congruent") 30 | else: 31 | print("The triangles are not congruent") 32 | 33 | # AAS property 34 | m5, m6, a7 = 40, 60, 6 35 | m7, m8, a8 = 40, 60, 6 36 | 37 | if m5 == m7 and m6 == m8 and a7 == a8: 38 | print("The triangles are congruent") 39 | else: 40 | print("The triangles are not congruent") 41 | 42 | # RHS property 43 | a9, b5 = 3, 4 44 | a10, b6 = 3, 4 45 | 46 | if a9 == a10 and b5 == b6: 47 | print("The triangles are congruent") 48 | else: 49 | print("The triangles are not congruent") 50 | -------------------------------------------------------------------------------- /Python/Maths/Interquartile_range.py: -------------------------------------------------------------------------------- 1 | #Problem statement :- Calculate Interquartile range from given number series 2 | 3 | n = int(input("Enter the size")) 4 | 5 | arr = [] 6 | for i in range (0 , n): 7 | element = int(input()) 8 | arr.append(element) 9 | 10 | arr.sort() 11 | 12 | start = 0 13 | last = n-1 14 | 15 | n = last - start + 1 16 | n = (n + 1) // 2 - 1 17 | 18 | mid=n 19 | 20 | print("Median of series as Quartile2 : ", arr[mid]) 21 | 22 | q1=int(mid/2) 23 | 24 | Quartile1 = arr[q1] 25 | 26 | print("The Median of first half as Quartile1",Quartile1) 27 | 28 | q2 = int(mid+1+(last-mid)/2) 29 | Quartile3 = arr[q2] 30 | 31 | print("The Median of Second half as Quartile3 : ",Quartile3) 32 | 33 | print("The IQR value is ",Quartile3 - Quartile1) 34 | 35 | # Case 1: INPUt :- 1 3 5 2 4 6 36 | 37 | # OUTPUT:- Median of series as Quartile2 : 3 38 | # The Median of first half as Quartile1 : 2 39 | # The Median of second half as Quartile2 : 5 40 | # The IQR value is : 3 41 | 42 | -------------------------------------------------------------------------------- /Python/Maths/ModularExponentiation.py: -------------------------------------------------------------------------------- 1 | def modulorExponentation(base,power,modulo): 2 | # Update base if it is more than or equal to modulo 3 | base = base % modulo 4 | # Initializing the result variable to store the modular exponentiation value. 5 | result = 1 6 | while power>0: 7 | # If power is odd, multiply base with result 8 | if (power & 1) != 0: 9 | result = (result * base) % modulo 10 | # Power must be even now 11 | # power = power/2 12 | power = power >> 1 13 | base = (base * base) % modulo 14 | # Printing the modular exponentiation value. 15 | print("Modular Exponentiation = ", result) 16 | 17 | # Reading the base,power,modulo values from the user. 18 | base = int(input("Enter the base value = ")) 19 | power = int(input("Enter the power value = ")) 20 | modulo = int(input("Enter the modulo value = ")) 21 | # In case base is divisible by modulo 22 | if base == 0: 23 | print("0") 24 | # Calling the function to perform the modular exponentiation. 25 | modulorExponentation(base,power,modulo) 26 | -------------------------------------------------------------------------------- /Python/Maths/Pascal_Triangle.py: -------------------------------------------------------------------------------- 1 | # 2 | # 1 3 | # 1 1 4 | # 1 2 1 5 | # 1 3 3 1 6 | # 1 4 6 4 1 7 | 8 | # Each number in the triangle is the sum of the two numbers directly above it. 9 | 10 | # we take number of rows as input 11 | # And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr. 12 | # nCr=factorial(n)/(factorial(r)*factorial(n-r)) 13 | 14 | def factorial(n): 15 | result = 1 16 | for c in range(1, n+1): 17 | result *= c 18 | return result 19 | 20 | n = int(input("Enter the number of rows you wish to see in Pascal's Triangle:\n")) 21 | 22 | for i in range(n): 23 | for c in range(n - i - 1): 24 | print(" ", end="") 25 | 26 | for c in range(i + 1): 27 | coeff = factorial(i) // (factorial(c) * factorial(i - c)) 28 | print(coeff, end=" ") 29 | 30 | print() 31 | -------------------------------------------------------------------------------- /Python/Maths/Power_Set.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | def generate_power_set(original_set): 4 | n = len(original_set) 5 | power_set = [] 6 | 7 | for i in range(2**n): 8 | subset = [original_set[j] for j in range(n) if (i & (1 << j)) != 0] 9 | power_set.append(subset) 10 | 11 | return power_set 12 | 13 | def print_power_set(power_set): 14 | for subset in power_set: 15 | print("{", end=" ") 16 | print(*subset, end=" ") 17 | print("}") 18 | 19 | def remove_duplicates(original_set): 20 | return list(set(original_set)) 21 | 22 | num_elements = int(input("Enter the number of elements in the original set: ")) 23 | original_set = [] 24 | print("Enter the elements of the original set: ") 25 | for _ in range(num_elements): 26 | original_set.append(input()) 27 | 28 | # Remove duplicates from the original set 29 | original_set = remove_duplicates(original_set) 30 | 31 | # Generate the power set 32 | power_set = generate_power_set(original_set) 33 | 34 | # Print the power set 35 | print("Power Set:") 36 | print_power_set(power_set) -------------------------------------------------------------------------------- /Python/Maths/README.md: -------------------------------------------------------------------------------- 1 | # Algorithms to compute mathematical functions 2 | -------------------------------------------------------------------------------- /Python/Maths/RussianPeasant.py: -------------------------------------------------------------------------------- 1 | def russian_peasant(a, b): 2 | res = 0 3 | while b > 0: 4 | if b & 1: 5 | res += a 6 | a <<= 1 7 | b >>= 1 8 | return res 9 | def main(): 10 | a = int(input("Enter the first number: ")) 11 | b = int(input("Enter the second number: ")) 12 | result = russian_peasant(a, b) 13 | print("Result:", result) 14 | if __name__ == "__main__": 15 | main() 16 | -------------------------------------------------------------------------------- /Python/Maths/ShriDharacharya.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def ShriDharacharyaFormulae(a, b, c): 4 | compValue = (b ** 2) - 4 * a * c 5 | val1 = -b / (2 * a) 6 | 7 | if compValue >= 0: 8 | val2 = math.sqrt(compValue) / (2 * a) 9 | print(val1 + val2, "and", val1 - val2) 10 | else: 11 | print("Imaginary roots") 12 | 13 | a = float(input()) 14 | b = float(input()) 15 | c = float(input()) 16 | ShriDharacharyaFormulae(a, b, c) 17 | -------------------------------------------------------------------------------- /Python/Maths/SimilarTriangles.py: -------------------------------------------------------------------------------- 1 | # Similar Triangles Program 2 | # It can be used to check whether the two triangles are similar or not. 3 | # Author: @swastik-akhil 4 | 5 | 6 | # SSS property 7 | 8 | a1, b1, c1 = 3, 4, 5 9 | a2, b2, c2 = 6, 8, 10 10 | if a1/a2 == b1/b2 == c1/c2: 11 | print("The triangles are similar") 12 | else: 13 | print("The triangles are not similar") 14 | 15 | 16 | # AA property 17 | 18 | x1, x2 = 40, 70 19 | y1, y2 = 40, 70 20 | if x1 == y1 and x2 == y2: 21 | print("The triangles are similar") 22 | else: 23 | print("The triangles are not similar") 24 | 25 | 26 | # SAS property 27 | 28 | a3, m, b3 = 3, 50, 4 29 | a4, n, b4 = 6, 50, 8 30 | if a3/a4 == b3/b4 and m == n: 31 | print("The triangles are similar") 32 | else: 33 | print("The triangles are not similar") 34 | -------------------------------------------------------------------------------- /Python/Maths/Strong_Numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | Name : Hardik Goyal 3 | Github username : HardikGoyal2003 4 | Repositary name : Algorithms 5 | 6 | What is a strong number? 7 | A strong number is a positive integer whose sum of factorials of its digits equals the number itself. In other words, if we take the 8 | digits of a number and calculate the factorial of each digit, then sum those factorials, and the result is the same as the original number, 9 | then that number is called a strong number. 10 | 11 | Problem Statement : To Check Given number is Strong Number or not. 12 | EXAMPLE : 13 | 1) 14 | Input: 145 15 | Output: "It is a Strong Number" 16 | """ 17 | n=int(input("Enter Number")) 18 | factorial=1 19 | sum=0 20 | n1= n 21 | while (n>0): 22 | rem = int(n%10) 23 | n=n//10 24 | for i in range(1,rem+1): 25 | factorial*=i 26 | sum+=factorial 27 | factorial=1 28 | if (n1 == sum): 29 | print("It is Strong Number") 30 | else: 31 | print("It is not a Strong Number") 32 | 33 | -------------------------------------------------------------------------------- /Python/Maths/armstrong.py: -------------------------------------------------------------------------------- 1 | # Python program to check if the number is an Armstrong number or not 2 | 3 | number = int(input('Enter a number to check if it is armstrong or not: ')) 4 | 5 | length = len(str(number)) # to get the number of digits in the given number 6 | 7 | s = 0 # initializing sum for the number 8 | temp = number 9 | 10 | # finding the sum of digits with the power of length 11 | while temp > 0: 12 | last_digit = temp%10 13 | s = s + last_digit**length 14 | temp //= 10 15 | 16 | # checking if sum is equal to the given number 17 | if s == number: 18 | print(number, "is an Armstrong number") 19 | else: 20 | print(number, "is not an Armstrong number") 21 | 22 | """ 23 | Case 1: 24 | Enter a number to check if it is armstrong or not: 153 25 | 153 is an Armstrong number 26 | 27 | Case 2: 28 | Enter a number to check if it is armstrong or not: 233 29 | 233 is not an Armstrong number 30 | 31 | Case 3: 32 | Enter a number to check if it is armstrong or not: 1634 33 | 1634 is an Armstrong number 34 | """ -------------------------------------------------------------------------------- /Python/Maths/bin_to_dec_&_dec_to_bin.py: -------------------------------------------------------------------------------- 1 | def main (): 2 | while True : 3 | print("\n1) Binary to decimal \t2)Decimal to Binary \t3)Exit") #The list of choices user can enter 4 | Enter = input("\nEnter your choice : ") 5 | 6 | if Enter == '1': 7 | Binary = input("\nEnter a binary number: ") 8 | Binary_To_Decimal( Binary) #This function returns decimal equivalent of given binary number 9 | 10 | elif Enter =='2': 11 | Decimal = input("\nEnter a decimal number : ") 12 | Decimal_To_Binary(int(Decimal)) #This function returns binary equivalent of given decimal number 13 | 14 | elif Enter == '3': 15 | break 16 | 17 | else : 18 | print("\nEnter valid input") 19 | 20 | 21 | def Binary_To_Decimal(Binary): 22 | decimal = int(Binary, 2) #Built in "int()" function to convert binary to decimal , 2 is taken as the base of the number for binary 23 | print("\nThe Decimal equivalent of" " " + str(Binary) + " is: ", decimal) 24 | 25 | 26 | def Decimal_To_Binary(Decimal): 27 | binary = bin(Decimal)[2:] # "bin()" function to convert decimal to binary 28 | print("\nThe Binary equivalent of" " "+ str(Decimal) + " is: ", binary) 29 | 30 | 31 | 32 | if __name__ == "__main__": 33 | main() 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Python/Maths/bisection.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation 2 | # of Bisection Method for 3 | # solving equations 4 | 5 | 6 | # An example function whose 7 | # solution is determined using 8 | # Bisection Method. 9 | # The function is x^3 - x^2 + 2 10 | def func(x): 11 | return x*x*x - x*x + 2 12 | 13 | # Prints root of func(x) 14 | # with error of EPSILON 15 | def bisection(a,b): 16 | 17 | if (func(a) * func(b) >= 0): 18 | print("You have not assumed right a and b\n") 19 | return 20 | 21 | c = a 22 | while ((b-a) >= 0.01): 23 | 24 | # Find middle point 25 | c = (a+b)/2 26 | 27 | # Check if middle point is root 28 | if (func(c) == 0.0): 29 | break 30 | 31 | # Decide the side to repeat the steps 32 | if (func(c)*func(a) < 0): 33 | b = c 34 | else: 35 | a = c 36 | 37 | print("The value of root is : ","%.4f"%c) 38 | 39 | # Driver code 40 | # Initial values assumed 41 | a =-200 42 | b = 300 43 | bisection(a, b) 44 | -------------------------------------------------------------------------------- /Python/Maths/factorial.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | FACTORIAL PROGRAM 4 | Factorial is represented by !. It is calculated by mutiplying natural 5 | numbers upto that number, i.e., n! = 1 x 2 x 3... x n. 6 | (n should be positive) 7 | Note: 0! = 1 and 1! = 1 8 | 9 | ''' 10 | 11 | def factorial(num): 12 | if(num == 0 or num == 1): return 1 # factorial of 0 and 1 is 1 13 | else: return num * factorial(num-1) 14 | 15 | num = int(input("Enter a number: ")) # factorial of this no will be calculated 16 | res = factorial(num) 17 | print("Factorial of" , num, "is", factorial(num)) 18 | -------------------------------------------------------------------------------- /Python/Maths/factors.py: -------------------------------------------------------------------------------- 1 | # @author : Pradyumn Rahar 2 | 3 | from math import sqrt 4 | 5 | # Time Complexity: O(sqrt(n)*log(sqrt(n))) | Space Complexity: O(sqrt(n)) 6 | def factors_of_a_number(num: int) -> list: 7 | """ 8 | >>> factors_of_a_number(1) 9 | [1] 10 | >>> factors_of_a_number(5) 11 | [1, 5] 12 | >>> factors_of_a_number(24) 13 | [1, 2, 3, 4, 6, 8, 12, 24] 14 | >>> factors_of_a_number(-24) 15 | [] 16 | """ 17 | facs: list[int] = [] 18 | if num < 1: 19 | return facs 20 | facs.append(1) 21 | if num == 1: 22 | return facs 23 | facs.append(num) 24 | for i in range(2, int(sqrt(num)) + 1): 25 | if num % i == 0: # If i is a factor of num 26 | facs.append(i) 27 | d = num // i # num//i is the other factor of num 28 | if d != i: # If d and i are distinct 29 | facs.append(d) # we have found another factor 30 | facs.sort() 31 | return facs 32 | 33 | 34 | if __name__ == "__main__": 35 | print("Factors of 24:", factors_of_a_number(24)) 36 | -------------------------------------------------------------------------------- /Python/Maths/heronsFormula.py: -------------------------------------------------------------------------------- 1 | """This code takes input of three sides of a traingle from the user and returns it's are which is calculated using Heron's Formula. In symbols, if a, b, and c are the lengths of the sides: Area = Square root of√s(s - a)(s - b)(s - c) where s is half the perimeter, or (a + b + c)/2.""" 2 | 3 | 4 | import math 5 | 6 | s1 = float(input("Enter side 1 of triangle: ")) 7 | s2 = float(input("Enter side 2 of triangle: ")) 8 | s3 = float(input("Enter side 3 of triangle: ")) 9 | 10 | S = (s1 + s2 + s3) / 2 11 | area = math.sqrt(S * (S - s1) * (S - s2) * (S - s3)) 12 | 13 | print(f"The area of the triangle is: {area:.2f}") 14 | 15 | -------------------------------------------------------------------------------- /Python/Maths/is_prime.py: -------------------------------------------------------------------------------- 1 | # import sqrt from math module 2 | from math import sqrt 3 | 4 | 5 | def isPrime(n): 6 | 7 | # Corner case 8 | if (n <= 1): 9 | return False 10 | 11 | # Check from 2 to sqrt(n) 12 | for i in range(2, int(sqrt(n))+1): 13 | if (n % i == 0): 14 | return False 15 | 16 | return True 17 | 18 | if isPrime(11): 19 | print("true") 20 | else: 21 | print("false") -------------------------------------------------------------------------------- /Python/Maths/josephus.py: -------------------------------------------------------------------------------- 1 | # Josephus Problem Algorithm :: 2 | # 1. Create a list to represent the circle of people, numbered from 1 to n. 3 | # 2. Start with an index variable pointing to the first person. 4 | # 3. Repeat the elimination process until only one person remains: 5 | # a. Move the index k-1 steps in a circular manner. 6 | # b. Remove the person at the current index. 7 | # c. Update the index to the next position after the elimination, wrapping around if necessary. 8 | # 4. Return the value of the last remaining person. 9 | 10 | def josephus(n, k): 11 | people = list(range(1, n + 1)) # Create a list with people numbered from 1 to n 12 | index = 0 13 | 14 | # Elimination process 15 | while len(people) > 1: 16 | # Move the index k-1 steps in a circular manner 17 | index = (index + k - 1) % len(people) 18 | 19 | # Remove the person at the current index 20 | people.pop(index) 21 | 22 | return people[0] # Return the value of the last remaining person 23 | 24 | # User input for n and k 25 | n = int(input("Enter the number of people in the circle: ")) 26 | k = int(input("Enter the elimination step size: ")) 27 | 28 | lastPerson = josephus(n, k) # Solve the Josephus Problem 29 | print("The last remaining person is at position:", lastPerson) 30 | -------------------------------------------------------------------------------- /Python/Maths/perfect.py: -------------------------------------------------------------------------------- 1 | # Python program to check if the number is a Perfect number or not 2 | 3 | # take user input 4 | num = int(input("Enter a number : ")) 5 | 6 | # initialize variable to store sum 7 | sum = 0 8 | 9 | # iterate from 1 to N and if the number is a factor of N then add it to sum 10 | for i in range (1,num): 11 | if(num % i == 0): 12 | sum = sum + i 13 | 14 | # if sum of factors of N is equal to N then it is a perfect number 15 | if(sum == num): 16 | print(num," is a perfect number") 17 | else: 18 | print(num," is not a perfect number") 19 | 20 | """ 21 | Case 1: 22 | Enter a number: 153 23 | 153 is not a perfect number 24 | 25 | Case 2: 26 | Enter a number: 496 27 | 496 is a perfect number 28 | """ 29 | -------------------------------------------------------------------------------- /Python/Maths/sieve_of_eratosthenes.py: -------------------------------------------------------------------------------- 1 | # Approach :- 2 | # we have to loop and find the non primes first 3 | # for ex:- lets say number is 10 4 | # prime number are - 2,3,5.... 5 | # sqrt(10) = around 3 we check multiples here like 6 | # starting with 2 -> we have 4,6,8,10 we mark these places as false 7 | # now next 3 -> we have 9 we mark it as false 8 | # hence we got all non-prime marked false 9 | def sieveOfErastosthenes(n, sieve): 10 | # creating a list of boolean act as sieve initially all places is true 11 | isPrime = [True]*(n+1) 12 | p = 2 13 | # iterating through the square root of number and finding all non-prime numbers 14 | while p*p <= n: 15 | if isPrime[p]: 16 | i = p * p 17 | # marking the place as false here 18 | while i <= n: 19 | isPrime[i] = False 20 | i += p 21 | p += 1 22 | # adding the result to list here 23 | for j in range(2, n + 1): 24 | if (isPrime[j]): 25 | sieve.append(j) 26 | return sieve 27 | 28 | def main(): 29 | n = int(input("Enter Number - ")) 30 | sieve = [] 31 | sieveOfErastosthenes(n, sieve) 32 | print(sieve) 33 | 34 | if __name__ == '__main__': 35 | main() 36 | -------------------------------------------------------------------------------- /Python/Maths/zeller.py: -------------------------------------------------------------------------------- 1 | # Python3 program to Find the Day 2 | # for a Date 3 | 4 | def switch(h) : 5 | return { 6 | 0 : "Saturday", 7 | 1 : "Sunday", 8 | 2 : "Monday", 9 | 3 : "Tuesday", 10 | 4 : "Wednesday", 11 | 5 : "Thursday", 12 | 6 : "Friday", 13 | }[h] 14 | 15 | def Zellercongruence(day, month, year) : 16 | if (month == 1) : 17 | month = 13 18 | year = year - 1 19 | 20 | if (month == 2) : 21 | month = 14 22 | year = year - 1 23 | q = day 24 | m = month 25 | k = year % 100 26 | j = year // 100 27 | h = q + 13 * (m + 1) // 5 + k + k // 4 + j // 4 + 5 * j 28 | h = h % 7 29 | print(switch (h)) 30 | 31 | 32 | 33 | # Driver code 34 | Zellercongruence(11, 11, 2022) #date (dd/mm/yyyy) -------------------------------------------------------------------------------- /Python/Memory Allocation/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation -------------------------------------------------------------------------------- /Python/Optimization/README.md: -------------------------------------------------------------------------------- 1 | # Optimization 2 | -------------------------------------------------------------------------------- /Python/Pattern-Matching/README.md: -------------------------------------------------------------------------------- 1 | # Pattern Matching -------------------------------------------------------------------------------- /Python/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198798053-063e65a2-8aed-44c3-9932-e50e710971f1.png) 2 | -------------------------------------------------------------------------------- /Python/Recursion/Permutations.py: -------------------------------------------------------------------------------- 1 | def permutations(p, up): 2 | # Base case: If the remaining characters to be permuted is empty, print the permutation 3 | if len(up) == 0: 4 | print(p) 5 | return 6 | 7 | ch = up[0] 8 | 9 | for i in range(len(p) + 1): 10 | # Split the permutation string into two parts 11 | f = p[:i] # First part 12 | s = p[i:] # Second part 13 | 14 | # Recursive call: Generate permutations by inserting the current character at each possible position 15 | permutations(f + ch + s, up[1:]) 16 | 17 | 18 | permutations("", "abc") -------------------------------------------------------------------------------- /Python/Recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recusion -------------------------------------------------------------------------------- /Python/Searching/Jump_Search.py: -------------------------------------------------------------------------------- 1 | # Python implementation of Jump Search Algorithm 2 | import math 3 | 4 | def jumpSearch(array, size, key): 5 | start = 0 6 | end = int(math.sqrt(size)) # the square root of array length 7 | 8 | while array[end] <= key and end < size: 9 | start = end # if it is not the correct block then shift block 10 | end += int(math.sqrt(size)) 11 | 12 | if end > size - 1: 13 | end = size # if right exceeds then bound the range 14 | 15 | # perform linear search in selected block 16 | for i in range(start, end): 17 | if array[i] == key: 18 | return i # the correct position of the key 19 | 20 | return -1 21 | 22 | 23 | # taking input of number of elements and key 24 | n, key = map(int, input().split()) 25 | arr = list(map(int, input().split())) 26 | 27 | # function call 28 | ind = jumpSearch(arr, n, key) 29 | 30 | if ind >= 0: # key is present in the array 31 | print("Key found at location:", ind) 32 | else: # key is not present in the array 33 | print("Key not found in the array") 34 | -------------------------------------------------------------------------------- /Python/Searching/README.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/76027425/198502450-2428b4b5-9507-4061-9c72-99ae7e88674b.png) 2 | -------------------------------------------------------------------------------- /Python/Searching/binary_search.py: -------------------------------------------------------------------------------- 1 | ''' 2 | <----------------------------------PROBLEM------------------------------------> 3 | Given a sorted array arr[] of n elements, write a function to search a given 4 | element x in arr[] and return the index of x in the array. 5 | 6 | Variables used: 7 | n = total number of elements in array. 8 | arr = array initialized with the input size. 9 | el = element to be searched in the array. 10 | lt = left-most index of the search space. 11 | rt = right-most index of the search space. 12 | ''' 13 | 14 | n = int(input()) 15 | arr = [] 16 | for i in range(n): 17 | x = int(input()) 18 | arr.append(x) 19 | 20 | el = int(input()) 21 | 22 | lt, rt = 0, n-1 23 | # to keep a track if the element has been found till then. 24 | flag=False 25 | while lt<=rt: 26 | # in order to prevent overflow of `int`, it has been written lt + (rt-lt)/2 instead of (lt+rt)/2 27 | mid = int(lt + (rt-lt)/2) 28 | # element is equal to mid, return the index 29 | if arr[mid] == el: 30 | flag=True 31 | break 32 | # element is larger than mid, then shift search space to right. 33 | elif arr[mid] < el: 34 | lt = mid + 1 35 | # element is smaller than mid, then shift search space to left. 36 | else: 37 | rt = mid - 1 38 | 39 | if flag: 40 | print("Element found:)") 41 | else: 42 | print("Element not found:(") -------------------------------------------------------------------------------- /Python/Searching/linear_search.py: -------------------------------------------------------------------------------- 1 | ''' 2 | <----------------------------------PROBLEM------------------------------------> 3 | Given an array arr[] of n elements, write a function to search a given element el in arr[]. 4 | 5 | Variables used: 6 | n = total number of elements in array 7 | arr = list of elements 8 | el = element to be searched in the array. 9 | ''' 10 | 11 | n = int(input()) 12 | arr = [] 13 | for i in range(n): 14 | x = int(input()) 15 | arr.append(x) 16 | el = int(input()) 17 | # Flag variable to keep a check if the element is found till then. 18 | flag = False 19 | for i in arr: 20 | if i == el: 21 | flag = True 22 | 23 | if flag: 24 | print("Element found:)\n") 25 | else: 26 | print("Element not found:(\n") -------------------------------------------------------------------------------- /Python/Signal Processing/README.md: -------------------------------------------------------------------------------- 1 | # Signal Processing -------------------------------------------------------------------------------- /Python/Sliding Window/README.md: -------------------------------------------------------------------------------- 1 | # Sliding Window Algorithm -------------------------------------------------------------------------------- /Python/Sorting/Bead_sort.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def display(arr): 4 | for num in arr: 5 | print(num, end=" ") 6 | print() 7 | 8 | def bead_sort(arr): 9 | max_val = max(arr) 10 | len_arr = len(arr) 11 | beads = np.zeros((len_arr, max_val), dtype=int) 12 | 13 | for i in range(len_arr): 14 | for j in range(arr[i]): 15 | beads[i][j] = 1 16 | 17 | for j in range(max_val): 18 | sum_val = 0 19 | for i in range(len_arr): 20 | sum_val += beads[i][j] 21 | beads[i][j] = 0 22 | for i in range(len_arr - sum_val, len_arr): 23 | beads[i][j] = 1 24 | 25 | for i in range(len_arr): 26 | j = 0 27 | while j < max_val and beads[i][j] != 0: 28 | j += 1 29 | arr[i] = j 30 | 31 | if __name__ == '__main__': 32 | n = int(input("Enter the size of the array: ")) 33 | arr = [] 34 | elements = input("Enter the elements of the array: ").split() 35 | for i in range(n): 36 | arr.append(int(elements[i])) 37 | 38 | print("Original array:", end=" ") 39 | display(arr) 40 | 41 | bead_sort(arr) 42 | 43 | print("Sorted array:", end=" ") 44 | display(arr) -------------------------------------------------------------------------------- /Python/Sorting/Bidirectional_Selection_Sort.py: -------------------------------------------------------------------------------- 1 | def bidirectional_selection_sort(arr): 2 | left = 0 3 | right = len(arr) - 1 4 | 5 | while left < right: 6 | minimum = arr[left] 7 | maximum = arr[right] 8 | min_index = left 9 | max_index = right 10 | 11 | for i in range(left+1, right + 1): 12 | if arr[i] < minimum: 13 | minimum = arr[i] 14 | min_index = i 15 | if arr[i] > maximum: 16 | maximum = arr[i] 17 | max_index = i 18 | 19 | if min_index == right: 20 | arr[left], arr[min_index] = arr[min_index], arr[left] 21 | else: 22 | arr[left], arr[min_index] = arr[min_index], arr[left] 23 | arr[right], arr[max_index] = arr[max_index], arr[right] 24 | 25 | left += 1 26 | right -= 1 27 | 28 | n = int(input("Enter the number of elements in the array: ")) 29 | array = list(map(int, input("Enter the elements: ").split())) 30 | 31 | print("Original array:", array) 32 | 33 | bidirectional_selection_sort(array) 34 | 35 | print("Sorted array:", array) 36 | -------------------------------------------------------------------------------- /Python/Sorting/BogoSortIn_python.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def shuffle(arr): 4 | for i in range(len(arr)): 5 | j = random.randrange(len(arr)) 6 | t = arr[i] 7 | arr[i] = arr[j] 8 | arr[j] = t 9 | 10 | def is_sorted(arr): 11 | for i in range(1, len(arr)): 12 | if arr[i] < arr[i - 1]: 13 | return False 14 | return True 15 | 16 | def bogosort(arr): 17 | randShuffles = 0 18 | while not is_sorted(arr): 19 | shuffle(arr) 20 | randShuffles += 1 21 | print("Number of randShuffles:", randShuffles) 22 | 23 | def main(): 24 | arr = [random.randint(0, 100) for _ in range(10)] 25 | print("The unsorted arr is:", arr) 26 | bogosort(arr) 27 | print("The sorted arr is:", arr) 28 | 29 | if __name__ == "__main__": 30 | main() 31 | -------------------------------------------------------------------------------- /Python/Sorting/Bucket_Sort.py: -------------------------------------------------------------------------------- 1 | def bucket_sort(alist): 2 | largest = max(alist) 3 | length = len(alist) 4 | size = largest/length 5 | 6 | buckets = [[] for _ in range(length)] 7 | for i in range(length): 8 | j = int(alist[i]/size) 9 | if j != length: 10 | buckets[j].append(alist[i]) 11 | else: 12 | buckets[length - 1].append(alist[i]) 13 | 14 | for i in range(length): 15 | insertion_sort(buckets[i]) 16 | 17 | result = [] 18 | for i in range(length): 19 | result = result + buckets[i] 20 | 21 | return result 22 | 23 | def insertion_sort(alist): 24 | for i in range(1, len(alist)): 25 | temp = alist[i] 26 | j = i - 1 27 | while (j >= 0 and temp < alist[j]): 28 | alist[j + 1] = alist[j] 29 | j = j - 1 30 | alist[j + 1] = temp 31 | 32 | 33 | alist = input('Enter the list of numbers: ').split() 34 | alist = [int(x) for x in alist] 35 | sorted_list = bucket_sort(alist) 36 | print('Sorted list: ', end='') 37 | print(sorted_list) -------------------------------------------------------------------------------- /Python/Sorting/Odd_Even_Sort.py: -------------------------------------------------------------------------------- 1 | def oddEvenSort(arr, n): 2 | # Initially array is unsorted 3 | isSorted = 0 4 | while isSorted == 0: 5 | isSorted = 1 6 | temp = 0 7 | for i in range(1, n-1, 2): 8 | if arr[i] > arr[i+1]: 9 | arr[i], arr[i+1] = arr[i+1], arr[i] 10 | isSorted = 0 11 | 12 | for i in range(0, n-1, 2): 13 | if arr[i] > arr[i+1]: 14 | arr[i], arr[i+1] = arr[i+1], arr[i] 15 | isSorted = 0 16 | 17 | return 18 | 19 | 20 | arr = [50, 10, 2, -4, 0] 21 | n = len(arr) 22 | 23 | oddEvenSort(arr, n) 24 | for i in range(0, n): 25 | print(arr[i], end = ' ') 26 | -------------------------------------------------------------------------------- /Python/Sorting/README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | -------------------------------------------------------------------------------- /Python/Sorting/StoogeSort.py: -------------------------------------------------------------------------------- 1 | def stooge_sort(arr, low, high): 2 | if low >= high: 3 | return 4 | # If the first element is greater than the last element, swap them 5 | if arr[low] > arr[high]: 6 | arr[low], arr[high] = arr[high], arr[low] 7 | # If there are more than 2 elements in the array 8 | if high - low + 1 > 2: 9 | t = (high - low + 1) // 3 10 | # Sort the first two-thirds of the array 11 | stooge_sort(arr, low, high - t) 12 | # Sort the last two-thirds of the array 13 | stooge_sort(arr, low + t, high) 14 | # Sort the first two-thirds of the array again 15 | stooge_sort(arr, low, high - t) 16 | # Function to print the array 17 | def print_array(arr): 18 | for num in arr: 19 | print(num, end=" ") 20 | print() 21 | # Driver Code 22 | arr = [10, 5, 8, 2, 1, 6] 23 | print("Original array:", end=" ") 24 | print_array(arr) 25 | stooge_sort(arr, 0, len(arr) - 1) 26 | print("Sorted array:", end=" ") 27 | print_array(arr) -------------------------------------------------------------------------------- /Python/Sorting/bingo_sort.py: -------------------------------------------------------------------------------- 1 | def bingoSort(arr): 2 | min_val = min(arr) 3 | max_val = max(arr) 4 | 5 | flags = [0] * (max_val - min_val + 1) 6 | for num in arr: 7 | flags[num - min_val] = 1 8 | 9 | sorted_arr = [] 10 | for i in range(len(flags)): 11 | if flags[i]: 12 | sorted_arr.append(i + min_val) 13 | 14 | return sorted_arr 15 | 16 | 17 | def main(): 18 | n = int(input("Enter the length of the array: ")) 19 | a = [] 20 | print("Enter the elements of the array:") 21 | for _ in range(n): 22 | num = int(input()) 23 | a.append(num) 24 | 25 | print("Original array:") 26 | for num in a: 27 | print(num, end=" ") 28 | 29 | sorted_arr = bingoSort(a) 30 | 31 | print("\nSorted array:") 32 | for num in sorted_arr: 33 | print(num, end=" ") 34 | 35 | 36 | if __name__ == "__main__": 37 | main() 38 | -------------------------------------------------------------------------------- /Python/Sorting/bubble_sort.py: -------------------------------------------------------------------------------- 1 | def sort(list1): 2 | #slection sort 3 | for i in range(len(list1)-1,0,-1): 4 | for j in range(i): 5 | if list1[j]>list1[j+1]: 6 | temp = list1[j] 7 | list1[j] = list1[j+1] 8 | list1[j+1] = temp 9 | 10 | 11 | #Taking list input 12 | list1 = [] 13 | num = int(input("ENTER THE NUMBER OF ELEMENTS : ")) 14 | for i in range(0, num): 15 | element = int(input()) 16 | list1.append(element) 17 | print(list1) 18 | 19 | #Function call 20 | sort(list1) 21 | 22 | #Printing sorted array 23 | print("SORTED LIST : ", list1) 24 | -------------------------------------------------------------------------------- /Python/Sorting/comb_sort.py: -------------------------------------------------------------------------------- 1 | def get_next_gap(gap): 2 | gap = (gap * 10) // 13 3 | if gap < 1: 4 | return 1 5 | return gap 6 | def comb_sort(a): 7 | n = len(a) 8 | gap = n 9 | swapped = True 10 | while gap != 1 or swapped: 11 | gap = get_next_gap(gap) 12 | swapped = False 13 | for i in range(n - gap): 14 | if a[i] > a[i + gap]: 15 | a[i], a[i + gap] = a[i + gap], a[i] 16 | swapped = True 17 | if __name__ == "__main__": 18 | n = int(input("Enter the length of the array: ")) 19 | a = [] 20 | print("Enter the elements of the array:") 21 | for _ in range(n): 22 | element = int(input()) 23 | a.append(element) 24 | print("Original array:") 25 | print(*a) 26 | comb_sort(a) 27 | print("Sorted array:") 28 | print(*a) 29 | -------------------------------------------------------------------------------- /Python/Sorting/cyclic_sort.py: -------------------------------------------------------------------------------- 1 | #Approach: in-place swapping takes place by the formation of cycles. 2 | # Time Complexity: O(n2) 3 | 4 | # defination of an array 5 | def cyclic_sort(array,temp,place): 6 | count=0 7 | # increment count 8 | for num in array: 9 | if num < temp: 10 | count+=1 11 | 12 | # if the number is not sorted 13 | if temp != array[count]: 14 | x=array[place] 15 | array[place] = array[count] 16 | array[count]= x 17 | #recursively call function until sorted 18 | cyclic_sort(array,array[place],place) 19 | 20 | else: 21 | if place <=(n-2): 22 | cyclic_sort(array,array[place+1],place+1) 23 | # print if sorted 24 | else: 25 | print("Sorted array is : ") 26 | for i in range(0, n) : 27 | print(array[i], end = " ") 28 | 29 | 30 | if __name__=="__main__": 31 | # Enter the number of elements 32 | print("Enter the number of elements in an array : ") 33 | n = int(input()) 34 | array = [] 35 | # Append the elements in the array 36 | print("Enter the elements in an array : ") 37 | for i in range(n): 38 | x = int(input()) 39 | array.append(x) 40 | 41 | # function call 42 | cyclic_sort(array,array[0],0) 43 | -------------------------------------------------------------------------------- /Python/Sorting/dutch_national_flag_algorithm.py: -------------------------------------------------------------------------------- 1 | #Time Complexity - O(N) 2 | #Space Complexity - O(1) 3 | 4 | def sort012(arr, n): 5 | 6 | #start left pointer from 0 7 | #start right pointer from n-1 8 | 9 | left = 0 10 | right = n-1 11 | i=0 12 | 13 | # If the ith element is 0 then we will swap the element with the low range. 14 | # Similarly, if the ith element is 1 then keep it as it is. 15 | # If the element is 2 then we swap it with an element in the high range. 16 | 17 | while i<=right: 18 | if arr[i] == 0: 19 | arr[left], arr[i] = arr[i], arr[left] 20 | left+=1 21 | elif arr[i] == 2: 22 | arr[right], arr[i] = arr[i], arr[right] 23 | right-=1 24 | i-=1 25 | 26 | i+=1 27 | 28 | print('Enter the number of elements in array:') 29 | n = int(input()) 30 | 31 | print('Enter the array values (in space separated format):') 32 | arr = [int(i) for i in input().split(' ')] 33 | 34 | sort012(arr, n) 35 | 36 | print('Sorted array is:') 37 | print(' '.join([str(i) for i in arr])) 38 | -------------------------------------------------------------------------------- /Python/Sorting/heap_sort.py: -------------------------------------------------------------------------------- 1 | # max heap 2 | def heapify(ar, n, i): 3 | maxIdx = i 4 | leftChild, rightChild = 2 * i + 1, 2 * i + 2 5 | # left child greater than root 6 | if leftChild < n and ar[leftChild] > ar[maxIdx]: 7 | maxIdx = leftChild 8 | # right child greater than root 9 | if rightChild < n and ar[rightChild] > ar[maxIdx]: 10 | maxIdx = rightChild 11 | 12 | #swap and heapify until max ele not found 13 | if maxIdx != i: 14 | ar[i], ar[maxIdx] = ar[maxIdx], ar[i] 15 | heapify(ar, n, maxIdx) 16 | 17 | def heapSort(ar): 18 | n = len(ar) 19 | # max heapify 20 | for i in range(n // 2 - 1, -1, -1): 21 | heapify(ar, n, i) 22 | # heap sort 23 | for i in range(n - 1, 0, -1): 24 | ar[i], ar[0] = ar[0], ar[i] 25 | heapify(ar, i, 0) 26 | 27 | # main method 28 | def main(): 29 | # input code 30 | n = int(input("Enter array size : ")) 31 | print("Enter array elements :", end=" ") 32 | ar = [int(i) for i in input().split()][:n] 33 | 34 | # function call 35 | heapSort(ar) 36 | # display sorted array 37 | print("Sorted Array :", end=" ") 38 | for i in ar: 39 | print(i, end=" ") 40 | 41 | # main method call 42 | main() -------------------------------------------------------------------------------- /Python/Sorting/insertion_sort.py: -------------------------------------------------------------------------------- 1 | def Sort(a, n): 2 | for i in range(1, n): 3 | k = a[i] 4 | j = i - 1 5 | 6 | while j>=0 and a[j] > k: 7 | a[j+1] = a[j] 8 | j = j-1 9 | a[j+1] = k 10 | 11 | for i in range(0, n): 12 | print(a[i], " ") 13 | 14 | 15 | arr = [45, 55, 20, 3, 5] 16 | n = len(arr) 17 | Sort(arr, n) -------------------------------------------------------------------------------- /Python/Sorting/pancake_sort.py: -------------------------------------------------------------------------------- 1 | def flip(arr, i): 2 | start = 0 3 | while start < i: 4 | temp = arr[start] 5 | arr[start] = arr[i] 6 | arr[i] = temp 7 | start += 1 8 | i -= 1 9 | 10 | def findMaxIndex(arr, n): 11 | maxIndex = 0 12 | for i in range(1, n): 13 | if arr[i] > arr[maxIndex]: 14 | maxIndex = i 15 | return maxIndex 16 | 17 | def pancakeSort(arr): 18 | n = len(arr) 19 | currSize = n 20 | while currSize > 1: 21 | maxIndex = findMaxIndex(arr, currSize) 22 | if maxIndex != currSize - 1: 23 | flip(arr, maxIndex) 24 | flip(arr, currSize - 1) 25 | currSize -= 1 26 | 27 | arr = [6, 3, 9, 2, 5] 28 | 29 | print("Original array:", arr) 30 | pancakeSort(arr) 31 | print("Sorted array:", arr) 32 | -------------------------------------------------------------------------------- /Python/Sorting/pigeonhole sort.py: -------------------------------------------------------------------------------- 1 | def pigeonhole_sort(arr): 2 | min_val = min(arr) 3 | max_val = max(arr) 4 | size = max_val - min_val + 1 5 | pigeonholes = [0] * size 6 | 7 | for num in arr: 8 | pigeonholes[num - min_val] += 1 9 | 10 | index = 0 11 | for i in range(size): 12 | while pigeonholes[i] > 0: 13 | arr[index] = i + min_val 14 | pigeonholes[i] -= 1 15 | index += 1 16 | 17 | return arr 18 | 19 | n = int(input("Enter the number of elements: ")) 20 | arr = [] 21 | print("Enter the elements:") 22 | for _ in range(n): 23 | arr.append(int(input())) 24 | 25 | arr = pigeonhole_sort(arr) 26 | 27 | print("Sorted array:", arr) 28 | -------------------------------------------------------------------------------- /Python/Sorting/radixSort.py: -------------------------------------------------------------------------------- 1 | # Sorting the components based on significant places using counting sort(csort function) 2 | def csort(ip_array, i, arr_len): 3 | output = [0] * (arr_len) 4 | temp = [0] * (10) 5 | # Calculate count of elements 6 | for ele in range(0, arr_len): 7 | idx = (ip_array[ele]/i) 8 | temp[int((idx)%10)] += 1 9 | 10 | # Calculate cumulative count(from element in range(1,10)) 11 | for ele in range(1,10): 12 | temp[ele] += temp[ele-1] 13 | 14 | # Place the elements in sorted order 15 | ele = arr_len-1 16 | while ele >= 0: 17 | idx = (ip_array[ele]/i) 18 | output[ temp[ int((idx)%10) ] - 1] = ip_array[ele] 19 | temp[int((idx)%10)] -= 1 20 | ele -= 1 21 | 22 | 23 | ele = 0 24 | for ele in range(0,len(ip_array)): 25 | ip_array[ele] = output[ele] 26 | 27 | # Main function to implement radix sort 28 | def sort(ip_array): 29 | # Get maximum element 30 | max_ele = max(ip_array) 31 | # Use the counting sort to arrange the elements according to place value. 32 | i = 1 33 | while max_ele/i > 0: 34 | csort(ip_array,i, len(ip_array)) 35 | i *= 10 36 | 37 | ip_arr = [78,88,99,199,101] 38 | sort(ip_arr) 39 | print("The sorted array is :") 40 | for i in range(len(ip_arr)): 41 | print(ip_arr[i]) 42 | -------------------------------------------------------------------------------- /Python/Sorting/randomized_quicksort.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def partition(arr, p, r): 4 | x = arr[r] 5 | i = p - 1 6 | for j in range(p, r): 7 | if arr[j] <= x: 8 | i += 1 9 | arr[i], arr[j] = arr[j], arr[i] 10 | arr[i+1], arr[r] = arr[r], arr[i+1] 11 | return i+1 12 | 13 | def randomized_partition(arr, p, r): 14 | i = p + random.randrange(0, r-p+1) 15 | arr[i], arr[r] = arr[r], arr[i] 16 | return partition(arr, p, r) 17 | 18 | def randomized_quicksort(arr, p, r): 19 | if p < r: 20 | q = randomized_partition(arr, p, r) 21 | randomized_quicksort(arr, p, q-1) 22 | randomized_quicksort(arr, q+1, r) 23 | 24 | arr = [78, 9, 10, -1, 2, -45, 91] 25 | n = len(arr) 26 | random.seed() 27 | randomized_quicksort(arr, 0, n-1) 28 | print("Sorted array using Randomized quicksort is:") 29 | for i in range(n): 30 | print(arr[i], end=" ") 31 | print() -------------------------------------------------------------------------------- /Python/Sorting/selection_sort.py: -------------------------------------------------------------------------------- 1 | def sort(list1): 2 | 3 | for i in range(num-1): 4 | minpos = i 5 | for j in range(i,num): 6 | if list1[j] < list1[minpos]: 7 | minpos = j 8 | 9 | temp = list1[j] 10 | list1[i] = list1[minpos] 11 | list1[minpos] = temp 12 | 13 | print(list1) 14 | 15 | 16 | list1 = [] 17 | num = int(input("ENTER THE NUMBER OF ELEMENTS : ")) 18 | for i in range(0, num): 19 | element = int(input()) 20 | list1.append(element) 21 | print(list1) 22 | 23 | sort(list1) 24 | print("SORTED LIST : ", list1) -------------------------------------------------------------------------------- /Python/Sorting/shell_sort.py: -------------------------------------------------------------------------------- 1 | def shell_sort(arr): 2 | n = len(arr) 3 | 4 | # Start with a big gap, then reduce the gap 5 | gap = n // 2 6 | while gap > 0: 7 | # Do a gapped insertion sort for this gap size. 8 | # The first gap elements a[0..gap-1] are already in gapped order. 9 | # Keep adding one more element until the entire array is gap sorted. 10 | for i in range(gap, n): 11 | temp = arr[i] 12 | j = i 13 | while j >= gap and arr[j - gap] > temp: 14 | # Shift earlier gap-sorted elements up until the correct location for a[i] is found. 15 | arr[j] = arr[j - gap] 16 | j -= gap 17 | # Put temp (the original a[i]) in its correct location. 18 | arr[j] = temp 19 | gap //= 2 20 | 21 | return arr 22 | 23 | # Test the algorithm 24 | arr = [9, 8, 1, 7, 4, 3] 25 | print("Array before sorting:") 26 | print(arr) 27 | sorted_arr = shell_sort(arr) 28 | print("Array after sorting:") 29 | print(sorted_arr) -------------------------------------------------------------------------------- /Python/Sorting/wave_sort.py: -------------------------------------------------------------------------------- 1 | def waveSort(arr): 2 | i = 0 3 | n = len(arr) 4 | while(i < n): 5 | if arr[i] > arr[i-1]: 6 | arr[i],arr[i-1] = arr[i-1], arr[i] 7 | 8 | if (i <= (n - 2))and(arr[i] > arr[i+1]) : 9 | arr[i],arr[i+1] = arr[i+1],arr[i] 10 | i+=2 11 | print(arr) 12 | 13 | def main(): 14 | arr = [10, 5, 6, 3, 2, 20, 100, 80] 15 | waveSort(arr) 16 | 17 | 18 | if __name__ == "__main__": 19 | main() -------------------------------------------------------------------------------- /Python/Stack/README.md: -------------------------------------------------------------------------------- 1 | # Stacks -------------------------------------------------------------------------------- /Python/String/Camel_Case.py: -------------------------------------------------------------------------------- 1 | #Name : Atul Kumar 2 | #Github username : atul1510 3 | #Repositary name : Algorithms 4 | #Problem Description 5 | #Program to convert text sentence in CamelCase format . 6 | #Examples: 7 | #1] INPUT: "this is camel case" 8 | #OUTPUT "thisIsCamelCase" 9 | #2] INPUT: "Hi Atul here" 10 | # OUTPUT: "hiAtulHere" 11 | #Example Explanation 12 | 13 | # Main Function 14 | def main(): 15 | N = (int(input("No of TestCases : "))) 16 | for _ in range(N): 17 | S = input("Enter a sentence : ") 18 | print("Original: {}".format(S)) 19 | S = S.title() #Convert the first only the first letter of a word to uppercase and rest all to lowercase 20 | S = S.split() #Generates a list of words 21 | S[0] = S[0].lower() 22 | S = "".join(S) #Joins all the words 23 | print("Camel Casing : {}".format(S)) 24 | print() 25 | 26 | if __name__=="__main__": 27 | main() -------------------------------------------------------------------------------- /Python/Sub-Arrays/Kadane_Algorithm.py: -------------------------------------------------------------------------------- 1 | ''' 2 | function implementing Kadane's Algorithm 3 | Kadane Algorithm is used to find Largest Sum Contiguous Subarray 4 | It works in O(N) Time Complexity and O(1) Space Complexity 5 | ''' 6 | 7 | 8 | def maxSubArraySum(nums): 9 | # Initialize the maxSoFar variable with the first element of the array 10 | maxSoFar = nums[0] 11 | # Initialize the maxEndingHere variable with the first element of the array 12 | maxEndingHere = nums[0] 13 | # iterate through the array starting from the second element 14 | for i in range(1, len(nums)): 15 | # update maxEndingHere with the maximum of the current element or current element plus maxEndingHere 16 | maxEndingHere = max(nums[i], maxEndingHere + nums[i]) 17 | # update maxSoFar with the maximum of maxSoFar and maxEndingHere 18 | maxSoFar = max(maxSoFar, maxEndingHere) 19 | # return the final maxSoFar value 20 | return maxSoFar 21 | 22 | 23 | def main(): 24 | # input code 25 | n = int(input("Enter array size : ")) 26 | print("Enter array elements :", end=" ") 27 | ar = [int(i) for i in input().split()][:n] 28 | # function call & output 29 | # Get the maximum sum of a contiguous subarray by calling the maxSubArraySum function 30 | print("Max Sum : ", maxSubArraySum(ar)) 31 | 32 | 33 | # call the main function 34 | main() 35 | -------------------------------------------------------------------------------- /Python/Sub-Arrays/README.md: -------------------------------------------------------------------------------- 1 | # Algorithms on Sub-Arrays 2 | 3 | ## 1. Boyer-Moore majority voting algorithm 4 | 5 | This script takes a list of integers as input and finds the majority element. 6 | The majority element is defined as the element with number of occurances greater than N/2, 7 | where N is the length of the array. If no majority element is present, it displays None. 8 | Comparison of time and space complexity: 9 | 10 | 1. Brute force approach: O(n^2) time and O(1) space 11 | 2. Map approach: O(n) time and O(n) space 12 | 3. Boyer-Moore algorithm: O(n) time and O(1) space 13 | 14 | Reference: [topcoder - Boyer-Moore majority vote algorithm](https://www.topcoder.com/thrive/articles/boyer-moore-majority-vote-algorithm) 15 | Python: 3.10.8 16 | External packages: None 17 | -------------------------------------------------------------------------------- /Python/Trees/Fenwick Tree.py: -------------------------------------------------------------------------------- 1 | class FenwickTree: 2 | def __init__(self, size): 3 | self.tree = [0] * (size + 1) 4 | 5 | def next(self, index, delta): 6 | while index < len(self.tree): 7 | self.tree[index] += delta 8 | index += index & (-index) 9 | 10 | def get_prefix_sum(self, index): 11 | sum = 0 12 | while index > 0: 13 | sum += self.tree[index] 14 | index -= index & (-index) 15 | return sum 16 | 17 | def get_rangeSum(self, left, right): 18 | return self.get_prefix_sum(right) - self.get_prefix_sum(left - 1) 19 | 20 | 21 | values = [1, 3, 2, -5, 6, 4, 2, 1] 22 | n = len(values) 23 | 24 | fenwick_tree = FenwickTree(n) 25 | 26 | # building the Fenwick tree 27 | for i in range(n): 28 | fenwick_tree.next(i + 1, values[i]) 29 | 30 | # get prefix sum at index 4 31 | prefix_sum = fenwick_tree.get_prefix_sum(4) 32 | print("Prefix Sum at index 5:", prefix_sum) 33 | 34 | # get range sum from index 2 to 6 35 | rangeSum = fenwick_tree.get_rangeSum(2, 6) 36 | print("Range Sum from index 2 to 6:", rangeSum) 37 | -------------------------------------------------------------------------------- /Python/Trees/README.md: -------------------------------------------------------------------------------- 1 | ![1_QwwrXC3c7YzndILdmZAAew](https://user-images.githubusercontent.com/76027425/210194882-70a6ef95-e836-4e30-87f5-86fa34317cc7.png) 2 | -------------------------------------------------------------------------------- /Python/Trees/tree_traversals.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, item): 3 | self.left = None 4 | self.right = None 5 | self.val = item 6 | 7 | 8 | def inorder(root): 9 | 10 | if root: 11 | # Traverse left 12 | inorder(root.left) 13 | # Traverse root 14 | print(str(root.val) + "->", end='') 15 | # Traverse right 16 | inorder(root.right) 17 | 18 | 19 | def postorder(root): 20 | 21 | if root: 22 | # Traverse left 23 | postorder(root.left) 24 | # Traverse right 25 | postorder(root.right) 26 | # Traverse root 27 | print(str(root.val) + "->", end='') 28 | 29 | 30 | def preorder(root): 31 | 32 | if root: 33 | # Traverse root 34 | print(str(root.val) + "->", end='') 35 | # Traverse left 36 | preorder(root.left) 37 | # Traverse right 38 | preorder(root.right) 39 | 40 | 41 | root = Node(1) 42 | root.left = Node(2) 43 | root.right = Node(3) 44 | root.left.left = Node(4) 45 | root.left.right = Node(5) 46 | 47 | print("\nInorder traversal ") 48 | inorder(root) 49 | 50 | print("\n\nPreorder traversal ") 51 | preorder(root) 52 | 53 | print("\n\nPostorder traversal ") 54 | postorder(root) -------------------------------------------------------------------------------- /Python/arrays/Fisher_Yates_Shuffle_Algorithm.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | # Function to perform Fisher-Yates shuffle 4 | def fisherYatesShuffle(arr): 5 | n = len(arr) 6 | 7 | for i in range(n - 1, 0, -1): 8 | j = random.randint(0, i) # Generate a random index between 0 and i (inclusive) 9 | arr[i], arr[j] = arr[j], arr[i] # Swap the elements at indices i and j 10 | 11 | # Create a list of integers 12 | size = int(input("Enter the size of the array: ")) 13 | numbers = [] 14 | 15 | print("Enter the elements of the array: ") 16 | for _ in range(size): 17 | numbers.append(int(input())) 18 | 19 | # Print the original list 20 | print("Original list:", numbers) 21 | 22 | # Shuffle the list using Fisher-Yates shuffle 23 | fisherYatesShuffle(numbers) 24 | 25 | # Print the shuffled list 26 | print("Shuffled list:", numbers) 27 | -------------------------------------------------------------------------------- /Python/arrays/README.md: -------------------------------------------------------------------------------- 1 | # Arrays -------------------------------------------------------------------------------- /Python/arrays/monotonic_array.py: -------------------------------------------------------------------------------- 1 | def isMonotonic(nums): 2 | s = 0 3 | n = len(nums) 4 | e = n-1 5 | while (s < e): 6 | if (nums[s]<=nums[s+1] and nums[s]<=nums[e] and e==n-1): 7 | s += 1 8 | 9 | elif (nums[e]<=nums[e-1] and nums[e]<=nums[s] and s==0): 10 | e -= 1 11 | 12 | else: 13 | break 14 | 15 | if(s==n-1 or e==0): 16 | return True 17 | return False 18 | 19 | def main(): 20 | nums = [] 21 | n = int(input("Enter no of elements in array:")) 22 | print("Enter elements of array:") 23 | for i in range(n): 24 | element = input() 25 | nums.append(element) 26 | 27 | if isMonotonic(nums): 28 | print("The array is Monotonic") 29 | else: 30 | print("The array is not monotonic") 31 | 32 | main() 33 | # MONOTONIC ARRAY IN python 34 | # This Program Tells if an array is monotonic or not. 35 | --------------------------------------------------------------------------------