├── .vscode └── settings.json ├── CODE_OF_CONDUCT.md ├── Codechef └── practiceProblem │ └── beginner │ ├── HS08TEST.cpp │ └── INTEST.cpp ├── Knuth └── Vol1 │ ├── The Art of Computer Programming, Vol. 1 Fundamental Algorithms.pdf │ ├── a.out │ └── euclidAlgorithm.cpp ├── Leetcode └── Array │ └── coinPath.cpp ├── README.md ├── Reconsider.md ├── cp-algorithms ├── algebra │ ├── a.out │ └── binary-expon.cpp └── readme.md ├── gfg └── bitwise-algorithms │ ├── a.out │ ├── add-1-to-num.cpp │ ├── clearbit.cpp │ ├── detect-opp-sign.cpp │ ├── elem-appear-once.cpp │ ├── getbit.cpp │ ├── multiple-by-3p5.cpp │ ├── power-of-4.cpp │ ├── resource.md │ ├── setbit.cpp │ └── turn-off-rightmost.cpp └── introductionToAlgorithmCLRS ├── .vscode ├── launch.json └── settings.json ├── README.md ├── chp1 └── README.md ├── chp2 ├── Chapter_Problems │ ├── q1InsertionMergeMix │ ├── q1InsertionMergeMix.c │ ├── q1InsertionMergeMix.java │ ├── q1InsertionMergeMix.py │ ├── q2BubbleSort │ ├── q2BubbleSort.c │ ├── q2BubbleSort.java │ ├── q2BubbleSort.py │ ├── q3HornersRule │ ├── q3HornersRule.c │ ├── q3HornersRule.java │ ├── q3HornersRule.py │ ├── q4Inversions │ ├── q4Inversions.c │ ├── q4Inversions.java │ └── q4Inversions.py ├── Exercise │ ├── Ex_2.1 │ │ ├── q2RevInsertionSort │ │ ├── q2RevInsertionSort.c │ │ ├── q2RevInsertionSort.java │ │ ├── q2RevInsertionSort.py │ │ ├── q3SearchingProblem │ │ ├── q3SearchingProblem.c │ │ ├── q3SearchingProblem.java │ │ ├── q3SearchingProblem.py │ │ ├── q4n_bitBinarySum │ │ ├── q4n_bitBinarySum.c │ │ ├── q4n_bitBinarySum.java │ │ └── q4n_bitBinarySum.py │ ├── Ex_2.2 │ │ ├── q2SelectionSort │ │ ├── q2SelectionSort.c │ │ ├── q2SelectionSort.java │ │ └── q2SelectionSort.py │ └── Ex_2.3 │ │ ├── q2MergeSortNoSentinals │ │ ├── q2MergeSortNoSentinals.c │ │ ├── q2MergeSortNoSentinals.java │ │ ├── q2MergeSortNoSentinals.py │ │ ├── q4RecursiveInsertion │ │ ├── q4RecursiveInsertion.c │ │ ├── q4RecursiveInsertion.java │ │ ├── q4RecursiveInsertion.py │ │ ├── q5BinarySearch │ │ ├── q5BinarySearch.c │ │ ├── q5BinarySearch.java │ │ ├── q5BinarySearch.py │ │ ├── q6BinarySearch_Insertion │ │ ├── q6BinarySearch_Insertion.c │ │ ├── q6BinarySearch_Insertion.java │ │ ├── q6BinarySearch_Insertion.py │ │ ├── q7 │ │ ├── q7.c │ │ ├── q7.java │ │ └── q7.py ├── InChaptAlgo │ ├── README.md │ ├── insertionSort │ ├── insertionSort.c │ ├── insertionSort.java │ ├── insertionSort.py │ ├── mergeSort │ ├── mergeSort.c │ ├── mergeSort.java │ └── mergeSort.py └── README.md ├── chp3 └── README.md ├── chp4 ├── Chapter_Problem │ ├── MongeArray │ ├── MongeArray.c │ ├── MongeArray.java │ └── MongeArray.py ├── Exercises │ ├── Ex_4.1 │ │ ├── q2BruteForceMaxSubarray │ │ ├── q2BruteForceMaxSubarray.c │ │ ├── q2BruteForceMaxSubarray.java │ │ ├── q2BruteForceMaxSubarray.py │ │ ├── q5NonRecursiveMaxSubarray │ │ ├── q5NonRecursiveMaxSubarray.c │ │ ├── q5NonRecursiveMaxSubarray.java │ │ └── q5NonRecursiveMaxSubarray.py │ └── Ex_4.2 │ │ ├── q3StrassenNonSquare │ │ ├── q3StrassenNonSquare.c │ │ ├── q3StrassenNonSquare.java │ │ ├── q3StrassenNonSquare.py │ │ ├── q7ComplexMulti │ │ ├── q7ComplexMulti.c │ │ ├── q7ComplexMulti.java │ │ └── q7ComplexMulti.py ├── InChaptAlgo │ ├── MatrixMutliplication │ ├── MatrixMutliplication.c │ ├── MatrixMutliplication.java │ ├── MatrixMutliplication.py │ ├── MaxSubArray │ ├── MaxSubArray.c │ ├── MaxSubArray.java │ ├── MaxSubArray.py │ ├── RecursiveMatrixMulti │ ├── RecursiveMatrixMulti.c │ ├── RecursiveMatrixMulti.java │ ├── RecursiveMatrixMulti.py │ ├── StrassenMatrixMulti │ ├── StrassenMatrixMulti.c │ ├── StrassenMatrixMulti.java │ └── StrassenMatrixMulti.py └── README.md ├── chp5 └── README.md └── chp6 ├── Exercises ├── Ex_6.2 │ ├── q2MinHeapify.c │ ├── q2MinHeapify.java │ ├── q2MinHeapify.py │ ├── q5LoopMaxHeapify.c │ ├── q5LoopMaxHeapify.java │ └── q5LoopMaxHeapify.py └── Ex_6.5 │ ├── q3.java │ ├── q6.java │ └── q7.java ├── InChaptAlgo ├── BuildMaxHeap.h ├── BuildMaxHeap.java ├── BuildMaxHeap.py ├── HeapSort.h ├── HeapSort.java ├── HeapSort.py ├── MaxHeapify.h ├── MaxHeapify.java ├── MaxHeapify.py ├── master ├── master.c ├── master.java └── master.py └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "chrono": "cpp", 4 | "iomanip": "cpp" 5 | }, 6 | "editor.acceptSuggestionOnCommitCharacter": false, 7 | "editor.acceptSuggestionOnEnter": "off", 8 | "files.autoSave": "onWindowChange", 9 | "editor.snippetSuggestions": "none", 10 | "editor.parameterHints.enabled": false, 11 | "editor.quickSuggestions": null, 12 | "editor.suggest.showTypeParameters": false, 13 | "C_Cpp.autocomplete": "Disabled" 14 | } -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at bgoel4132@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /Codechef/practiceProblem/beginner/HS08TEST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * ? Pooja would like to withdraw X $US from an ATM. 3 | * ? The cash machine will only accept the transaction if 4 | * ? X is a multiple of 5, and Pooja's account balance has 5 | * ? enough cash to perform the withdrawal transaction 6 | * ? (including bank charges). For each successful withdrawal 7 | * ? the bank charges 0.50 $US. Calculate Pooja's account 8 | * ? balance after an attempted transaction. 9 | **/ 10 | 11 | #include "iostream" 12 | #include 13 | 14 | using namespace std; 15 | 16 | float atm(int withdraw, float amt) 17 | { 18 | if ((withdraw % 5 == 0) && (amt >= (withdraw + 0.50))) 19 | { 20 | return amt - withdraw - 0.50; 21 | } 22 | else 23 | { 24 | return amt; 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | float amt; 31 | int withdraw; 32 | cin >> withdraw >> amt; 33 | cout << fixed << setprecision(2) << atm(withdraw, amt) << "\n"; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codechef/practiceProblem/beginner/INTEST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * ? The purpose of this problem is to verify whether 3 | * ? the method you are using to read input data is 4 | * ? sufficiently fast to handle problems branded with 5 | * ? the enormous Input/Output warning. You are expected 6 | * ? to be able to process at least 2.5MB of input data 7 | * ? per second at runtime. 8 | **/ 9 | #include "iostream" 10 | using namespace std; 11 | 12 | bool divTest(int num, int k) 13 | { 14 | if (num % k == 0) 15 | { 16 | return true; 17 | } 18 | else 19 | { 20 | return false; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int n, k; 27 | cin >> n >> k; 28 | int count = 0; 29 | for (int i = 0; i < n; i++) 30 | { 31 | int num; 32 | cin >> num; 33 | if (divTest(num, k)) 34 | { 35 | count += 1; 36 | } 37 | } 38 | cout << count; 39 | } -------------------------------------------------------------------------------- /Knuth/Vol1/The Art of Computer Programming, Vol. 1 Fundamental Algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/Knuth/Vol1/The Art of Computer Programming, Vol. 1 Fundamental Algorithms.pdf -------------------------------------------------------------------------------- /Knuth/Vol1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/Knuth/Vol1/a.out -------------------------------------------------------------------------------- /Knuth/Vol1/euclidAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace std::chrono; 7 | 8 | /** 9 | * todo: finding the greatest common divisor 10 | * ! find remaider 11 | * ? if remainder 0, divisor is the ansewr 12 | * ? else m<-n, n<-r 13 | * 14 | **/ 15 | int euclid(int m, int n) 16 | { 17 | 18 | int rem = m % n; 19 | if (rem == 0) 20 | { 21 | return n; 22 | } 23 | else 24 | { 25 | return euclid(n, rem); 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | auto start = high_resolution_clock::now(); 32 | 33 | cout << euclid(100, 121) << "\n"; 34 | cout << euclid(121, 100) << "\n"; 35 | 36 | auto stop = high_resolution_clock::now(); 37 | auto duration = duration_cast(stop - start); 38 | 39 | cout << "Time taken by function: " << duration.count() << " microseconds" << endl; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Leetcode/Array/coinPath.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * ? Given an array A (index starts at 1) consisting of N 3 | * ? integers: A1, A2, ..., AN and an integer B. The integer 4 | * ? B denotes that from any place (suppose the index is i) 5 | * ? in the array A, you can jump to any one of the place 6 | * ? in the array A indexed i+1, i+2, …, i+B if this place 7 | * ? can be jumped to. Also, if you step on the index i, 8 | * ? you have to pay Ai coins. If Ai is -1, it means you 9 | * ? can’t jump to the place indexed i in the array. 10 | * 11 | * ? Now, you start from the place indexed 1 in the array 12 | * ? A, and your aim is to reach the place indexed N using 13 | * ? the minimum coins. You need to return the path of indexes 14 | * ? (starting from 1 to N) in the array you should take 15 | * ? to get to the place indexed N using minimum coins. 16 | * 17 | * ? If there are multiple paths with the same cost, return 18 | * ? the lexicographically smallest such path. 19 | * 20 | * ? If it's not possible to reach the place indexed N 21 | * ? then you need to return an empty array. 22 | **/ 23 | 24 | #include "iostream" 25 | 26 | int *coinPath(int arr[], int n) { 27 | 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms 2 | -------------------------------------------------------------------------------- /Reconsider.md: -------------------------------------------------------------------------------- 1 | | Link | Site | Tries | Revised | 2 | | --- | --- | --- | --- | 3 | | https://www.geeksforgeeks.org/find-the-element-that-appears-once/ | GFG | 2 | 0 | 4 | -------------------------------------------------------------------------------- /cp-algorithms/algebra/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/cp-algorithms/algebra/a.out -------------------------------------------------------------------------------- /cp-algorithms/algebra/binary-expon.cpp: -------------------------------------------------------------------------------- 1 | // 2021-08-26T03:44:04+05:30 2 | 3 | /** 4 | * * Calculating the exponential value 5 | * ! Traditional method using O(n) using ercursive for loop. 6 | * ? Converting to binary and caclulating decreases time complexity to O(logn) 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int recursive(int a, int b) 13 | { 14 | int res = 1; 15 | if (b == 0) 16 | { 17 | return res; 18 | } 19 | else 20 | { 21 | for (int i = 0; i < b; i++) 22 | { 23 | res = res * a; 24 | } 25 | } 26 | return res; 27 | } 28 | 29 | int exponent(int a, int b) 30 | { 31 | int res = 1; 32 | while (b > 0) 33 | { 34 | if (b & 1) 35 | { 36 | res = res * a; 37 | } 38 | a = a * a; 39 | b >>= 1; 40 | } 41 | return res; 42 | } 43 | 44 | int main() 45 | { 46 | int a, b; 47 | cin >> a >> b; 48 | 49 | int ans = exponent(a, b); 50 | cout << ans << "\n"; 51 | 52 | ans = recursive(a, b); 53 | cout << ans << "\n"; 54 | } -------------------------------------------------------------------------------- /cp-algorithms/readme.md: -------------------------------------------------------------------------------- 1 | ### This folder embarks my preparation for technical/coding interview at various comapnies. 2 | 3 | 4 | --- 5 | 6 | Credits - https://cp-algorithms.com/ -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/gfg/bitwise-algorithms/a.out -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/add-1-to-num.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/add-1-to-a-given-number/ 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | cin >> n; 12 | 13 | cout << (n | 1) << "\n"; 14 | 15 | // Flipping all 1s till 0 16 | 17 | int m = 1; 18 | while (n & m) 19 | { 20 | n = n ^ m; 21 | m <<= 1; 22 | } 23 | 24 | n = n ^ m; 25 | cout << n << "\n"; 26 | 27 | // 2s Complement ~x = ~(x+1) 28 | cout << (~(~n)) << "\n"; 29 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/clearbit.cpp: -------------------------------------------------------------------------------- 1 | // Clear bit at a given position 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | int i; 12 | cin >> i; 13 | 14 | cout << (n & ~(1 << i)) << "\n"; 15 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/detect-opp-sign.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/detect-if-two-integers-have-opposite-signs/ 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int a, b; 10 | cin >> a>> b; 11 | 12 | if ((a ^ b ) < 0 ) { 13 | cout << "Opposite\n"; 14 | } 15 | else { 16 | cout << "Same\n"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/elem-appear-once.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/find-the-element-that-appears-once/ 2 | 3 | #include 4 | // Used to prints bits 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // For elements repeated even times 10 | int elem_once_even(int arr[], int n) 11 | { 12 | int ones = 0; 13 | for (int i = 0; i < n; i++) 14 | { 15 | ones = ones ^ arr[i]; 16 | } 17 | return ones; 18 | } 19 | 20 | // For elements repeated odd times 21 | int elem_once_odd(int arr[], int n) 22 | { 23 | int ones = 0, twos = 0; 24 | int common_bit_mask; 25 | 26 | for (int i = 0; i < n; i++) 27 | { 28 | twos = twos | (ones & arr[i]); 29 | // cout << "Twos :\t" << bitset<4>(twos) << endl; 30 | 31 | ones = ones ^ arr[i]; 32 | // cout << "Ones :\t" << bitset<4>(ones) << endl; 33 | 34 | common_bit_mask = ~(ones & twos); 35 | // cout << "Comn :\t" << bitset<4>(common_bit_mask) << endl; 36 | 37 | ones &= common_bit_mask; 38 | // cout << "Ones :\t" << bitset<4>(ones) << endl; 39 | 40 | twos &= common_bit_mask; 41 | // cout << "Twos :\t" << bitset<4>(twos) << endl; 42 | } 43 | 44 | return ones; 45 | } 46 | 47 | // Using bit position count 48 | int position_count(int arr[], int n) 49 | { 50 | int tn = INT_MAX; 51 | int tnp1 = 0; 52 | int tnp2 = 0; 53 | 54 | for (int i = 0; i < n; i++) 55 | { 56 | int cwtn = tn & arr[i]; 57 | int cwtnp1 = tnp1 & arr[i]; 58 | int cwtnp2 = tnp2 & arr[i]; 59 | 60 | tn = tn & ~(cwtn); 61 | tnp1 = tnp1 | cwtn; 62 | 63 | tnp1 = tnp1 & ~(cwtnp1); 64 | tnp2 = tnp2 | cwtnp1; 65 | 66 | tnp2 = tnp2 & ~(cwtnp2); 67 | tn = tn | cwtnp2; 68 | } 69 | 70 | return tnp1; 71 | } 72 | 73 | int main() 74 | { 75 | int n; 76 | cin >> n; 77 | int arr[n]; 78 | for (int i = 0; i < n; i++) 79 | { 80 | cin >> arr[i]; 81 | } 82 | 83 | int obj = elem_once_even(arr, n); 84 | cout << "Even \t" << obj << "\n"; 85 | 86 | obj = elem_once_odd(arr, n); 87 | cout << "Odd \t" << obj << "\n"; 88 | 89 | obj = position_count(arr, n); 90 | cout << "Odd \t" << obj << "\n"; 91 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/getbit.cpp: -------------------------------------------------------------------------------- 1 | // Find bit at particular position 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | int i; 12 | cin >> i; 13 | 14 | cout << ((n & (1 << i)) != 0) << "\n"; 15 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/multiple-by-3p5.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/multiply-an-integer-with-3-5/ 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int x; 11 | cin >> x; 12 | 13 | // 2x + x + x/2 14 | cout << (x << 1) + (x) + (x >> 1) << "\n"; 15 | 16 | // (8x - x)/2 17 | cout << ((x << 3) - (x)) / 2 << "\n"; 18 | 19 | // 7x / 2 20 | 21 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/power-of-4.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/find-whether-a-given-number-is-a-power-of-4-or-not/ 2 | 3 | #include 4 | // Used to prints bits 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n; 12 | cin >> n; 13 | 14 | // and comdition for 0 15 | if (n && !(n & (n - 1))) 16 | { 17 | int bit = 1; 18 | int k = n-1; 19 | int flag = 0; 20 | while (bit & k) { 21 | k = k << 1; 22 | flag +=1; 23 | } 24 | cout < https://www.geeksforgeeks.org/bitwise-algorithms/ | -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/setbit.cpp: -------------------------------------------------------------------------------- 1 | // Set bit at particular position 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | int i; 12 | cin >> i; 13 | 14 | cout << (n | (1 << i)) << "\n"; 15 | } -------------------------------------------------------------------------------- /gfg/bitwise-algorithms/turn-off-rightmost.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/turn-off-the-rightmost-set-bit/ 2 | 3 | #include 4 | // Used to prints bits 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int n; 12 | cin >> n; 13 | // bitset<8> a(n); 14 | // cout << a << "\n"; 15 | int x = 1; 16 | int k = ~n; 17 | while (x & k) 18 | { 19 | x = x << 1; 20 | // bitset<8> b(x); 21 | // cout << b << "\n"; 22 | } 23 | 24 | n = x ^ n; 25 | 26 | // bitset<8> y(n); 27 | // cout << y << "\n"; 28 | cout << n << "\n"; 29 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "java", 9 | "name": "CodeLens (Launch) - MongeArray", 10 | "request": "launch", 11 | "mainClass": "MongeArray", 12 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 13 | }, 14 | { 15 | "type": "java", 16 | "name": "CodeLens (Launch) - q7ComplexMulti", 17 | "request": "launch", 18 | "mainClass": "q7ComplexMulti", 19 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 20 | }, 21 | { 22 | "type": "java", 23 | "name": "CodeLens (Launch) - q3StrassenNonSquare", 24 | "request": "launch", 25 | "mainClass": "q3StrassenNonSquare", 26 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 27 | }, 28 | { 29 | "type": "java", 30 | "name": "CodeLens (Launch) - StrassenMatrixMulti", 31 | "request": "launch", 32 | "mainClass": "StrassenMatrixMulti", 33 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 34 | }, 35 | { 36 | "type": "java", 37 | "name": "CodeLens (Launch) - RecursiveMatrixMulti", 38 | "request": "launch", 39 | "mainClass": "RecursiveMatrixMulti", 40 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 41 | }, 42 | { 43 | "type": "java", 44 | "name": "Debug (Launch) - Current File", 45 | "request": "launch", 46 | "mainClass": "${file}" 47 | }, 48 | { 49 | "type": "java", 50 | "name": "Debug (Launch)-MatrixMutliplication", 51 | "request": "launch", 52 | "mainClass": "MatrixMutliplication", 53 | "projectName": "introductionToAlgorithmCLRS_7aebed80" 54 | } 55 | ] 56 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.exclude": { 3 | "**/.classpath": true, 4 | "**/.project": true, 5 | "**/.settings": true, 6 | "**/.factorypath": true 7 | } 8 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/README.md: -------------------------------------------------------------------------------- 1 | # Thomas Cormen - Intro To Algo 2 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp1/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 2 | ## The Role of Algorithms in Computing 3 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q1InsertionMergeMix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Chapter_Problems/q1InsertionMergeMix -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q1InsertionMergeMix.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int *InsertionSort(int array[], int p , int q) { 5 | int *sortedArray = array; 6 | for(int j = p+1; j < q; j++) { 7 | int key = sortedArray[j]; 8 | int i = j - 1 ; 9 | while (i >-1 && sortedArray[i] > key ) { 10 | sortedArray[i + 1] = sortedArray[i]; 11 | i = i - 1; 12 | } 13 | sortedArray[i+1] = key; 14 | } 15 | 16 | return(sortedArray); 17 | } 18 | 19 | int* Merge(int array[], int p, int r) { 20 | if (p < r) { 21 | int q = (p+r)/2; 22 | array = Merge(array, p, q); 23 | array = Merge(array, q+1, r); 24 | array = InsertionSort(array, p, r+1); 25 | 26 | } 27 | return array; 28 | } 29 | 30 | void main(){ 31 | 32 | int size; 33 | printf("Enter the size of array you wish to have :- "); 34 | scanf("%d", &size); 35 | int array[size]; 36 | 37 | printf("Enter the elements of the array : "); 38 | 39 | for (int i = 0; i < size; i++) { 40 | scanf("%d", &array[i]); 41 | } 42 | 43 | 44 | int *sortedArray = Merge(array, 0, size-1); 45 | 46 | printf("Sorted array is : [ "); 47 | for (int i = 0; i < size; i++) { 48 | printf("%d ", sortedArray[i]); 49 | } 50 | printf("]"); 51 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q1InsertionMergeMix.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | 5 | public class q1InsertionMergeMix { 6 | 7 | public static int[] InsertionSort (int[] array, int p , int q) { 8 | int sortedArray[] = array; 9 | for(int j = p+1; j < q; j++) { 10 | int key = sortedArray[j]; 11 | int i = j - 1 ; 12 | while (i >-1 && sortedArray[i] > key ) { 13 | sortedArray[i + 1] = sortedArray[i]; 14 | i = i - 1; 15 | } 16 | sortedArray[i+1] = key; 17 | } 18 | 19 | return(sortedArray); 20 | } 21 | 22 | public static int[] Merge(int array[], int p, int r) { 23 | if (p < r) { 24 | int q = (p+r)/2; 25 | array = Merge(array, p, q); 26 | array = Merge(array, q+1, r); 27 | array = InsertionSort(array, p, r+1); 28 | 29 | } 30 | return array; 31 | } 32 | 33 | public static void main(String[] args){ 34 | 35 | int size; 36 | System.out.print("Enter the size of array you wish to have :- "); 37 | Scanner element = new Scanner(System.in); 38 | size = element.nextInt(); 39 | int array[] = new int[size]; 40 | 41 | System.out.print("Enter the elements of the array : "); 42 | 43 | for (int i = 0; i < size; i++) { 44 | array[i] = element.nextInt(); 45 | } 46 | 47 | element.close(); 48 | 49 | int sortedArray[] = Merge(array, 0, size-1); 50 | 51 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q1InsertionMergeMix.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def InsertionSort (array, p , q): 4 | sortedArray = array 5 | for j in range(p+1, q, 1): 6 | key = sortedArray[j] 7 | i = j - 1 8 | while (i >-1 and sortedArray[i] > key ): 9 | sortedArray[i + 1] = sortedArray[i] 10 | i = i - 1 11 | 12 | sortedArray[i+1] = key 13 | 14 | 15 | return(sortedArray) 16 | 17 | 18 | def Merge(array, p, r): 19 | if (p < r): 20 | q = math.floor((p+r)/2) 21 | array = Merge(array, p, q) 22 | array = Merge(array, q+1, r) 23 | array = InsertionSort(array, p, r+1) 24 | 25 | 26 | return array 27 | 28 | if __name__ == "__main__": 29 | 30 | size = int(input("Enter the size of array you wish to have : ")) 31 | array = list(map(lambda x: int(x), input("Enter elements of the array : ").split())) 32 | sortedArray = Merge(array, 0, size-1) 33 | 34 | print("Sorted Array is ", sortedArray) 35 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q2BubbleSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Chapter_Problems/q2BubbleSort -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q2BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *BubbleSort(int array[], int size) { 4 | 5 | for (int i = 0 ; i < size-1; i++) { 6 | for (int j = size-1; j > i; j--) { 7 | if (array[j] < array[j-1]) { 8 | int temp = array[j]; 9 | array[j] = array[j-1]; 10 | array[j-1] = temp; 11 | } 12 | } 13 | } 14 | return array; 15 | } 16 | 17 | void main(){ 18 | 19 | int size; 20 | printf("Enter the size of array you wish to have :- "); 21 | scanf("%d", &size); 22 | int array[size]; 23 | 24 | printf("Enter the elements of the array : "); 25 | 26 | for (int i = 0; i < size; i++) { 27 | scanf("%d", &array[i]); 28 | } 29 | 30 | 31 | int *sortedArray = BubbleSort(array, size); 32 | 33 | printf("Sorted array is : [ "); 34 | for (int i = 0; i < size; i++) { 35 | printf("%d ", sortedArray[i]); 36 | } 37 | printf("]"); 38 | 39 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q2BubbleSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class q2BubbleSort { 5 | public static int[] BubbleSort(int array[], int size) { 6 | 7 | for (int i = 0 ; i < size-1; i++) { 8 | for (int j = size-1; j > i; j--) { 9 | if (array[j] < array[j-1]) { 10 | int temp = array[j]; 11 | array[j] = array[j-1]; 12 | array[j-1] = temp; 13 | } 14 | } 15 | } 16 | return array; 17 | } 18 | 19 | public static void main(String[] args){ 20 | 21 | int size; 22 | System.out.print("Enter the size of array you wish to have :- "); 23 | Scanner element = new Scanner(System.in); 24 | size = element.nextInt(); 25 | int array[] = new int[size]; 26 | 27 | System.out.print("Enter the elements of the array : "); 28 | 29 | for (int i = 0; i < size; i++) { 30 | array[i] = element.nextInt(); 31 | } 32 | 33 | element.close(); 34 | 35 | int sortedArray[] = BubbleSort(array, size); 36 | 37 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 38 | 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q2BubbleSort.py: -------------------------------------------------------------------------------- 1 | def BubbleSort(array, size): 2 | 3 | for i in range(size-1): 4 | for j in range(size-1, i, -1): 5 | if (array[j] < array[j-1]): 6 | temp = array[j] 7 | array[j] = array[j-1] 8 | array[j-1] = temp 9 | 10 | return array 11 | 12 | if __name__ == "__main__": 13 | 14 | size = int(input("Enter the size of array you wish to have : ")) 15 | array = list(map(lambda x: int(x), input("Enter elements of the array : ").split())) 16 | sortedArray = BubbleSort(array, size) 17 | 18 | print("Sorted Array is ", sortedArray) -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q3HornersRule: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Chapter_Problems/q3HornersRule -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q3HornersRule.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int HornersRule(int degree, int coef[], int x) { 4 | int val = 0; 5 | for (int i = degree; i >-1; i--) { 6 | val = coef[i] + x*val; 7 | } 8 | 9 | return val; 10 | } 11 | 12 | void main() { 13 | 14 | printf("Enter the degree of polynomial you wish to have : "); 15 | int degree; 16 | scanf("%d", °ree); 17 | 18 | int coef[degree+1]; 19 | printf("Enter the coefficients of the polynomial : "); 20 | 21 | for (int i = 0 ; i < degree+1; i++) { 22 | scanf("%d", &coef[i]); 23 | } 24 | 25 | printf("Enter the value of x : "); 26 | int x; 27 | scanf("%d", &x); 28 | int val = HornersRule(degree, coef, x); 29 | printf("The calculated value is : %d" ,val); 30 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q3HornersRule.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | 5 | public class q3HornersRule { 6 | 7 | public static int HornersRule(int degree, int coef[], int x) { 8 | int val = 0; 9 | for (int i = degree; i >-1; i--) { 10 | val = coef[i] + x*val; 11 | } 12 | 13 | return val; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner element = new Scanner(System.in); 18 | 19 | System.out.print("Enter the degree of polynomial you wish to have : "); 20 | int degree = element.nextInt(); 21 | 22 | int coef[] = new int[(degree+1)]; 23 | System.out.print("Enter the coefficients of the polynomial : "); 24 | 25 | for (int i = 0 ; i < degree+1; i++) { 26 | coef[i] = element.nextInt(); 27 | } 28 | 29 | System.out.println("Coeeficients are : " +Arrays.toString(coef)); 30 | 31 | System.out.print("Enter the value of x : "); 32 | int x = element.nextInt(); 33 | element.close(); 34 | int val = HornersRule(degree, coef, x); 35 | System.out.println("The calculated value is : " +val); 36 | } 37 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q3HornersRule.py: -------------------------------------------------------------------------------- 1 | def HornersRule(degree, coef, x): 2 | val = 0 3 | for i in range(degree, -1, -1): 4 | val = coef[i] + x*val 5 | 6 | return val 7 | 8 | 9 | if __name__ == "__main__": 10 | 11 | degree = int(input("Enter the degree of polynomial you wish to have : ")) 12 | 13 | coef = list(map(lambda x: int(x), input("Enter the coefficients of the polynomial : ").split())) 14 | 15 | 16 | x = int(input("Enter the value of x : ")) 17 | 18 | val = HornersRule(degree, coef, x); 19 | print("The calculated value is : " ,val) 20 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q4Inversions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Chapter_Problems/q4Inversions -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Chapter_Problems/q4Inversions.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Inversion(int array[], int size) { 4 | 5 | int count = 0; 6 | for(int i = 0; i < size-1 ; i++) { 7 | for (int j = i+1 ; j array[j]) { 9 | count++; 10 | } 11 | } 12 | } 13 | return count; 14 | } 15 | 16 | 17 | int Merge(int array[], int p, int q, int r) { 18 | int n1 = q - p + 1; 19 | int n2 = r - q; 20 | int count = 0; 21 | 22 | int left[n1]; 23 | int right[n2]; 24 | 25 | for (int i = 0 ; i < n1; ++i) { 26 | left[i] = array[p + i]; 27 | } 28 | for (int j = 0; j < n2; ++j) { 29 | right[j] = array[q + j + 1]; 30 | } 31 | 32 | int i = 0; 33 | int j = 0; 34 | int k = p; 35 | 36 | while(iarray[j]) { 10 | count++; 11 | } 12 | } 13 | } 14 | return count; 15 | } 16 | 17 | 18 | public static int Merge(int array[], int p, int q, int r) { 19 | int n1 = q - p + 1; 20 | int n2 = r - q; 21 | int count = 0; 22 | 23 | int left[] = new int[n1]; 24 | int right[] = new int[n2]; 25 | 26 | for (int i = 0 ; i < n1; ++i) { 27 | left[i] = array[p + i]; 28 | } 29 | for (int j = 0; j < n2; ++j) { 30 | right[j] = array[q + j + 1]; 31 | } 32 | 33 | int i = 0; 34 | int j = 0; 35 | int k = p; 36 | 37 | while(iarray[j]): 9 | count+=1 10 | 11 | return count 12 | 13 | 14 | 15 | def Merge(array, p, q, r): 16 | n1 = q - p + 1 17 | n2 = r - q 18 | count = 0 19 | 20 | left = [] 21 | right = [] 22 | 23 | for i in range(n1): 24 | left.append(array[p + i]) 25 | 26 | for j in range(n2): 27 | right.append(array[q + j + 1]) 28 | 29 | 30 | i = 0 31 | j = 0 32 | k = p 33 | 34 | while(i 2 | 3 | int *insertionSort(int array[], int size) { 4 | 5 | for (int i = 1; i < size; i++) { 6 | int key = array[i]; 7 | int j = i - 1; 8 | while (j > -1 && array[j] < key) { 9 | array[j + 1] = array[j]; 10 | j = j - 1; 11 | } 12 | array[j + 1] = key; 13 | } 14 | return array; 15 | } 16 | int main() { 17 | int size; 18 | printf("Enter the size of array you need : "); 19 | scanf("%d", &size); 20 | int array[size]; 21 | printf("Enter the elements of the array \n"); 22 | for (int i = 0 ; i< size; i++) { 23 | scanf("%d", &array[i]); 24 | } 25 | int *sortedArray; 26 | sortedArray = insertionSort(array, size); 27 | 28 | printf("Sorted array is : [ "); 29 | for (int i = 0; i < size; i++) { 30 | printf("%d ", sortedArray[i]); 31 | } 32 | printf("]"); 33 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q2RevInsertionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class q2RevInsertionSort { 5 | 6 | public static int[] sorting (int[] array) { 7 | int sortedArray[] = array; 8 | for(int j = 1; j < sortedArray.length; j++) { 9 | int key = sortedArray[j]; 10 | int i = j - 1 ; 11 | while (i >-1 && sortedArray[i] < key ) { 12 | sortedArray[i + 1] = sortedArray[i]; 13 | i = i - 1; 14 | } 15 | sortedArray[i+1] = key; 16 | } 17 | 18 | return(sortedArray); 19 | } 20 | 21 | public static void main(String[] args){ 22 | 23 | int size; 24 | System.out.println("Enter the size of array you wish to have :-"); 25 | Scanner element = new Scanner(System.in); 26 | size = element.nextInt(); 27 | int array[] = new int[size]; 28 | 29 | System.out.println("First Defination of array is : " + Arrays.toString(array)); 30 | System.out.println("Enter the elements of the array"); 31 | 32 | for (int i = 0; i < size; i++) { 33 | array[i] = element.nextInt(); 34 | } 35 | 36 | element.close(); 37 | System.out.println("Defination of array after inserting elements : " + Arrays.toString(array)); 38 | 39 | int sortedArray[] = sorting(array); 40 | 41 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 42 | 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q2RevInsertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(array = []): 2 | sortedArray = array 3 | for i in range(len(array)): 4 | key = sortedArray[i] 5 | j = i - 1 6 | while (j > -1 and sortedArray[j] < key): 7 | sortedArray[j + 1] = sortedArray[j] 8 | j = j - 1 9 | sortedArray[j + 1] = key 10 | return sortedArray 11 | 12 | print("Enter the size of Array : ", end="") 13 | size = int(input()) 14 | 15 | print("Enter the elements of the array : ", end="") 16 | array = list(map(lambda x: int(x), input().split())) 17 | 18 | sortedArray = insertionSort(array) 19 | 20 | print("New Sorted Array is : ", sortedArray) 21 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q3SearchingProblem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q3SearchingProblem -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q3SearchingProblem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int size; 5 | printf("Enter the size of array you need : "); 6 | scanf("%d", &size); 7 | int array[size]; 8 | printf("Enter the elements of the array \n"); 9 | for (int i = 0 ; i< size; i++) { 10 | scanf("%d", &array[i]); 11 | } 12 | 13 | int value; 14 | printf("Enter the element to be searched for : "); 15 | scanf("%d", &value); 16 | int count = 0; 17 | for (int i = 0; i < size; i++) { 18 | if (array[i] == value) { 19 | count++; 20 | } 21 | } 22 | int index[count]; 23 | if (count >0) { 24 | int j = 0; 25 | for(int i = 0; i < size; i++) { 26 | if (array[i] == value) { 27 | index[j] = i + 1; 28 | j++; 29 | } 30 | } 31 | } 32 | if (count > 0) { 33 | printf("Index of searched elements are : [ "); 34 | for (int i = 0; i < count; i++) { 35 | printf("%d ", index[i]); 36 | } 37 | printf("]"); 38 | } 39 | 40 | else { 41 | printf("No elements found !! \n"); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q3SearchingProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class q3SearchingProblem { 5 | 6 | public static int[] search(int[] array, int value) { 7 | int count = 0; 8 | for (int i = 0; i < array.length; i++) { 9 | if (array[i] == value) { 10 | count ++; 11 | } 12 | } 13 | int index[] = new int[count]; 14 | 15 | if (count > 0) { 16 | int j = 0; 17 | for(int i = 0 ; i < array.length ; i++) { 18 | if (array[i] == value) { 19 | index[j] = i + 1; 20 | j++; 21 | } 22 | } 23 | } 24 | return index; 25 | } 26 | public static void main(String[] args) { 27 | int size; 28 | System.out.println("Enter the size of array you wish to have :-"); 29 | Scanner element = new Scanner(System.in); 30 | size = element.nextInt(); 31 | int array[] = new int[size]; 32 | 33 | System.out.println("First Defination of array is : " + Arrays.toString(array)); 34 | System.out.println("Enter the elements of the array"); 35 | 36 | for (int i = 0; i < size; i++) { 37 | array[i] = element.nextInt(); 38 | } 39 | 40 | int value; 41 | System.out.println("Enter the element to be searcher for :- "); 42 | value = element.nextInt(); 43 | element.close(); 44 | int index[] = search(array, value); 45 | 46 | if (index.length == 0) { 47 | System.out.println("No matching element found!!"); 48 | } 49 | 50 | else { 51 | System.out.println("Matching elements are found at element(s) : " +Arrays.toString(index)); 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q3SearchingProblem.py: -------------------------------------------------------------------------------- 1 | def search(array = [], value = int): 2 | index = [] 3 | for i in range(len(array)): 4 | if (array[i] == value): 5 | index.append(i + 1) 6 | 7 | return index 8 | 9 | print("Enter the size of Array : ", end="") 10 | size = int(input()) 11 | 12 | print("Enter the elements of the array : ", end="") 13 | array = list(map(lambda x: int(x), input().split())) 14 | value = int(input("Enter the value to be searched for : ")) 15 | index = search(array, value) 16 | 17 | if len(index)>0: 18 | print("Index of the searched element are : ", index) 19 | else: 20 | print("No element found !!") 21 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q4n_bitBinarySum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q4n_bitBinarySum -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q4n_bitBinarySum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long long convert(int num) { 5 | long long bin = 0; 6 | int rem, i; 7 | i = 1; 8 | while (num != 0 ) { 9 | rem = num % 2; 10 | num /= 2; 11 | bin += rem * i; 12 | i *= 10; 13 | } 14 | return bin; 15 | } 16 | 17 | long long sumBin(long long bin1, long long bin2, long long sumBinary) { 18 | int init = 1; 19 | sumBinary = 0; 20 | int res = 0; 21 | while(bin1 != 0 || bin2 != 0) { 22 | sumBinary += ((bin1 %10 + bin2%10 + res) %2)*init; 23 | res = (bin1 %10 + bin2%10 + res)/2; 24 | bin1 /= 10; 25 | bin2 /= 10; 26 | init *= 10; 27 | } 28 | if (res != 0) { 29 | sumBinary += res*init; 30 | } 31 | 32 | return sumBinary; 33 | } 34 | int main() { 35 | int num1, num2; 36 | printf("Enter the numbers to be added : "); 37 | scanf("%d %d", &num1, &num2); 38 | 39 | long long num1Binary, num2Binary; 40 | 41 | num1Binary = convert(num1); 42 | num2Binary = convert(num2); 43 | 44 | printf("Binary of the %d is %lld.\n", num1, num1Binary); 45 | printf("Binary of the %d is %lld.\n", num2, num2Binary); 46 | 47 | long long sumBinary; 48 | sumBinary = sumBin(num1Binary, num2Binary, sumBinary); 49 | 50 | printf("sum in Binary is : %lld ", sumBinary); 51 | 52 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q4n_bitBinarySum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class q4n_bitBinarySum { 5 | 6 | public static long convert(int num) { 7 | long bin = 0; 8 | int i; 9 | i = 1; 10 | while(num != 0) { 11 | bin += (num%2)*i; 12 | num = num / 2; 13 | i *= 10; 14 | } 15 | return bin; 16 | } 17 | 18 | public static long sum(long bin1, long bin2) { 19 | long sumBinary = 0; 20 | long init = 1; 21 | long res = 0; 22 | while(bin1 != 0 || bin2 != 0) { 23 | sumBinary += ((bin1 % 10 + bin2%10 + res)%2 )* init; 24 | res = (bin1%10 + bin2%10 + res)/2; 25 | bin1 = bin1/10; 26 | bin2 = bin2/10; 27 | init *= 10; 28 | } 29 | if (res != 0) { 30 | sumBinary += res*init; 31 | } 32 | 33 | return sumBinary; 34 | } 35 | public static void main(String[] args) { 36 | int num1, num2; 37 | Scanner element = new Scanner(System.in); 38 | System.out.println("Enter the numbers to be added : "); 39 | num1 = element.nextInt(); 40 | num2 = element.nextInt(); 41 | element.close(); 42 | long num1Binary = convert(num1); 43 | long num2Binary = convert(num2); 44 | 45 | System.out.println("Binary of " +num1 +" is " +num1Binary); 46 | System.out.println("Binary of " +num2 +" is " +num2Binary); 47 | 48 | long sumBinary = sum(num1Binary, num2Binary); 49 | 50 | System.out.println("Sum in Binary is " +sumBinary); 51 | } 52 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.1/q4n_bitBinarySum.py: -------------------------------------------------------------------------------- 1 | def sumBin(bin1, bin2): 2 | init = 1 3 | sumBinary = 0 4 | res = 0 5 | while (bin1 != 0 or bin2 != 0): 6 | sumBinary += ((bin1%10 + bin2%10 + res)%2)*init 7 | res = (bin1%10 + bin2%10 + res)//2 8 | bin1 = bin1 // 10 9 | bin2 = bin2 // 10 10 | init = init * 10 11 | if (res != 0): 12 | sumBinary += (res*init) 13 | return sumBinary 14 | 15 | num1 = int(input("Enter first Element : ")) 16 | num2 = int(input("Enter second Element : ")) 17 | num1Binary = bin(num1).replace("0b", "") 18 | num2Binary = bin(num2).replace("0b", "") 19 | num1Binary = int(num1Binary) 20 | num2Binary = int(num2Binary) 21 | 22 | print("Binary of the " , num1 , "is", num1Binary, ".") 23 | print("Binary of the " , num2 , "is", num2Binary, ".") 24 | 25 | sumBinary = sumBin(num1Binary, num2Binary) 26 | 27 | print("Sum in binary is ", sumBinary) 28 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.2/q2SelectionSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.2/q2SelectionSort -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.2/q2SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *selectionSort(int array[], int size) { 4 | for (int i = 0 ; i < size ; i++) { 5 | int smallest = array[i]; 6 | for(int j = i; j < size; j++) { 7 | if (smallest > array[j]) { 8 | int temp = array[i]; 9 | array[i] = array[j]; 10 | array[j] = temp; 11 | smallest = array[j]; 12 | } 13 | } 14 | } 15 | return array; 16 | } 17 | 18 | int main() { 19 | int size; 20 | printf("Enter the size of array : "); 21 | scanf("%d", &size); 22 | int array[size]; 23 | printf("Enter the elements of the array : "); 24 | for (int i = 0 ; i < size ; i ++) { 25 | scanf("%d", &array[i]); 26 | } 27 | 28 | int *sortedArray = selectionSort(array, size); 29 | 30 | printf("Sorted array is : [ "); 31 | for (int i = 0; i < size; i++) { 32 | printf("%d ", sortedArray[i]); 33 | } 34 | printf("]"); 35 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.2/q2SelectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | 5 | public class q2SelectionSort { 6 | 7 | public static int[] selectionSort(int array[], int size) { 8 | for (int i = 0 ; i < size; i++) { 9 | int smallest = array[i]; 10 | for (int j = i; j < size; j++) { 11 | if (smallest > array[j]) { 12 | int temp = array[i]; 13 | array[i] = array[j]; 14 | array[j] = temp; 15 | smallest = array[j]; 16 | } 17 | } 18 | } 19 | return array; 20 | } 21 | public static void main(String[] args) { 22 | System.out.print("Enter the size of array : "); 23 | Scanner element = new Scanner(System.in); 24 | int size = element.nextInt(); 25 | int array[] = new int[size]; 26 | System.out.print("Enter elements of the array : "); 27 | for (int i = 0 ; i < size; i++ ) { 28 | array[i] = element.nextInt(); 29 | } 30 | element.close(); 31 | 32 | int sortedArray[] = new int[size]; 33 | sortedArray = selectionSort(array, size); 34 | System.out.println("Sorted Array is : " + Arrays.toString(sortedArray)); 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.2/q2SelectionSort.py: -------------------------------------------------------------------------------- 1 | def selectionSort(array, size): 2 | for i in range(size): 3 | smallest = array[i] 4 | for j in range(i, size): 5 | if (smallest > array[j]): 6 | temp = array[i] 7 | array[i] = array[j] 8 | array[j] = temp 9 | smallest = array[j] 10 | return array 11 | 12 | 13 | size = int(input("Enter the size of array : ")) 14 | array = list(map(lambda x: int(x), input("Enter elements of array : ").split())) 15 | 16 | 17 | sortedArray = selectionSort(array, size) 18 | print("Sorted Array is : ", sortedArray) 19 | 20 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q2MergeSortNoSentinals: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q2MergeSortNoSentinals -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q2MergeSortNoSentinals.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *merge(int array[], int p, int q, int r) { 4 | int n1 = q - p + 1; 5 | int n2 = r - q; 6 | 7 | int left[n1]; 8 | int right[n2]; 9 | 10 | for(int i = 0 ; i 2 | 3 | int *InsertionSort(int array[], int n) { 4 | 5 | if (n<=1) { 6 | return array; 7 | } 8 | 9 | array = InsertionSort(array, n-1); 10 | 11 | int key = array[n-1]; 12 | int i = n-2; 13 | 14 | while (i>-1 && array[i] > key) { 15 | array[i+1] = array[i]; 16 | i -=1; 17 | } 18 | 19 | array[i+1] = key; 20 | 21 | return array; 22 | } 23 | 24 | int main() { 25 | int size; 26 | printf("Enter the size of the array : "); 27 | scanf("%d", &size); 28 | 29 | int array[size]; 30 | 31 | printf("Enter the elements of the array : "); 32 | 33 | for(int i = 0 ; i < size ; i++) { 34 | scanf("%d", &array[i]); 35 | } 36 | 37 | int *sortedArray; 38 | sortedArray = InsertionSort(array, size); 39 | 40 | printf("Sorted array is : [ "); 41 | for (int i = 0; i < size; i++) { 42 | printf("%d ", sortedArray[i]); 43 | } 44 | printf("]"); 45 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q4RecursiveInsertion.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | 5 | public class q4RecursiveInsertion { 6 | 7 | public static int[] InsertionSort (int[] array, int n ) { 8 | 9 | if (n<=1) { 10 | return array; 11 | } 12 | 13 | array = InsertionSort(array, n-1); 14 | 15 | int key = array[n-1]; 16 | int i = n-2 ; 17 | while (i >-1 && array[i] > key ) { 18 | array[i + 1] = array[i]; 19 | i = i - 1; 20 | } 21 | array[i+1] = key; 22 | 23 | return(array); 24 | } 25 | 26 | // public static int[] Recursive (int[] array) { 27 | // for (int i = 0 ; i <= array.length; i++) { 28 | // array = InsertionSort(array, 0, i); 29 | // } 30 | // return array; 31 | // } 32 | public static void main(String[] args) { 33 | 34 | int size; 35 | System.out.print("Enter the size of array you wish to have :- "); 36 | Scanner element = new Scanner(System.in); 37 | size = element.nextInt(); 38 | int array[] = new int[size]; 39 | 40 | System.out.print("Enter the elements of the array : "); 41 | 42 | for (int i = 0; i < size; i++) { 43 | array[i] = element.nextInt(); 44 | } 45 | 46 | element.close(); 47 | int sortedArray[] = InsertionSort(array, array.length); 48 | 49 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 50 | 51 | } 52 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q4RecursiveInsertion.py: -------------------------------------------------------------------------------- 1 | def insertionSort(array, n): 2 | 3 | if (n <= 1): 4 | return array; 5 | 6 | array = insertionSort(array, n-1) 7 | 8 | key = array[n-1] 9 | i = n-2 10 | 11 | while (i>-1 and array[i] > key): 12 | array[i+1] = array[i] 13 | i-=1 14 | 15 | array[i+1] = key 16 | 17 | return array 18 | 19 | 20 | if __name__ == "__main__": 21 | 22 | size = int(input("Enter the size of array you wish to have : ")) 23 | array = list(map(lambda x: int(x), input("Enter elements of the array : ").split())) 24 | sortedArray = insertionSort(array, size) 25 | 26 | print("Sorted Array is ", sortedArray) -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q5BinarySearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q5BinarySearch -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q5BinarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int BinarySearch(int array[], int p, int r, int value) { 4 | if (r value) { 12 | return BinarySearch(array, p, q-1, value); 13 | } 14 | 15 | else if (array[q] < value) { 16 | return BinarySearch(array, q+1, r, value); 17 | } 18 | 19 | else { 20 | index = q; 21 | } 22 | 23 | return index; 24 | } 25 | 26 | void main() { 27 | int size; 28 | printf("Enter the size of Array :- "); 29 | scanf("%d", &size); 30 | int array[size]; 31 | printf("Enter the elements of the array : "); 32 | for(int i = 0 ; i < size; i++) { 33 | scanf("%d", &array[i]); 34 | } 35 | printf("Enter the value to be searched for : "); 36 | int value; 37 | scanf("%d", &value); 38 | int index = BinarySearch(array, 0, size -1 , value); 39 | 40 | if (index != -1){ 41 | printf( "Your value is found at %d", (index+1)); 42 | } 43 | else { 44 | printf("Value not found !!!"); 45 | } 46 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q5BinarySearch.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class q5BinarySearch { 4 | public static int binarySearch(int array[], int p, int r, int value) { 5 | 6 | if (r value) { 14 | return binarySearch(array, p, q - 1, value); 15 | } 16 | 17 | else if (array[q] < value) { 18 | return binarySearch(array, q + 1, r, value); 19 | } 20 | 21 | else { 22 | index = q; 23 | } 24 | 25 | return index; 26 | } 27 | 28 | public static void main(String[] args) { 29 | System.out.print("Enter the size of array : "); 30 | Scanner element = new Scanner(System.in); 31 | int size = element.nextInt(); 32 | int array[] = new int[size]; 33 | System.out.print("Enter the elements of the array : "); 34 | for(int i = 0; i < size ; i++) { 35 | array[i] = element.nextInt(); 36 | } 37 | 38 | System.out.print("Enter the value to be searched for : "); 39 | int value = element.nextInt(); 40 | element.close(); 41 | int index = binarySearch(array, 0, size -1 , value); 42 | 43 | if (index != -1){ 44 | System.out.println("Your value is found at " +(index+1)); 45 | } 46 | else { 47 | System.out.println("Value not found !!!"); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q5BinarySearch.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def BinarySearch(array, p , r, value): 4 | 5 | if (r value): 12 | return BinarySearch(array, p, q-1, value) 13 | 14 | elif (array[q] < value): 15 | return BinarySearch(array, q+1, r, value) 16 | 17 | else: 18 | index = q 19 | 20 | return index 21 | 22 | 23 | if __name__ == "__main__": 24 | size = int(input("Enter the size of array : ")) 25 | array = list(map(lambda x: int(x), input("Enter the elements of array : ").split())) 26 | 27 | value = int(input("Enter the value to be searched for : ")) 28 | 29 | index = BinarySearch(array, 0, size -1, value) 30 | 31 | if index != -1: 32 | print("Given value found at index : ", index) 33 | else: 34 | print("Value not found!!!!") 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q6BinarySearch_Insertion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q6BinarySearch_Insertion -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q6BinarySearch_Insertion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int BinarySearch(int array[], int p, int r, int value) { 4 | 5 | if (r < p) { 6 | return (value > array[p])? (p + 1) : p; 7 | } 8 | 9 | int q = (p + r) / 2; 10 | int index; 11 | 12 | if(array[q] > value) { 13 | return BinarySearch(array, p, q-1, value); 14 | } 15 | else if (array[q] < value) { 16 | return BinarySearch(array, q+1, r, value); 17 | } 18 | 19 | else{ 20 | index = q+1; 21 | } 22 | 23 | return index; 24 | } 25 | 26 | int *InsertionBinarySearch(int array[], int size) { 27 | 28 | for (int j = 1; j = pos) { 34 | array[i+1] = array[i]; 35 | i -= 1; 36 | } 37 | array[i+1] = key; 38 | } 39 | 40 | return array; 41 | } 42 | 43 | 44 | int main() { 45 | int size; 46 | printf("Enter the size of the array : "); 47 | scanf("%d", &size); 48 | 49 | int array[size]; 50 | 51 | printf("Enter the elements of the array : "); 52 | 53 | for(int i = 0 ; i < size ; i++) { 54 | scanf("%d", &array[i]); 55 | } 56 | 57 | int *sortedArray; 58 | sortedArray = InsertionBinarySearch(array, size); 59 | 60 | printf("Sorted array is : [ "); 61 | for (int i = 0; i < size; i++) { 62 | printf("%d ", sortedArray[i]); 63 | } 64 | printf("]"); 65 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q6BinarySearch_Insertion.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | 5 | public class q6BinarySearch_Insertion { 6 | 7 | public static int binarySearch(int array[], int p, int r, int value) { 8 | 9 | if (r < p) { 10 | return (value > array[p])? (p + 1): p; 11 | } 12 | 13 | 14 | int q = (p + r) / 2; 15 | int index = -1; 16 | 17 | if (array[q] > value) { 18 | return binarySearch(array, p, q - 1 , value); 19 | } 20 | 21 | else if (array[q] < value) { 22 | return binarySearch(array, q + 1, r, value); 23 | } 24 | 25 | else { 26 | index = q+1; 27 | } 28 | 29 | return index; 30 | 31 | } 32 | 33 | public static int[] InsertionBinarySearch(int array[]) { 34 | int sortedArray[] = array; 35 | for(int j = 1; j < sortedArray.length; j++) { 36 | int key = sortedArray[j]; 37 | int i = j - 1 ; 38 | 39 | int pos = binarySearch(array, 0, i, key); 40 | while (i >=pos) { 41 | sortedArray[i + 1] = sortedArray[i]; 42 | i = i - 1; 43 | } 44 | sortedArray[i+1] = key; 45 | } 46 | 47 | return(sortedArray); 48 | } 49 | public static void main(String[] args) { 50 | 51 | int size; 52 | System.out.print("Enter the size of array you wish to have :- "); 53 | Scanner element = new Scanner(System.in); 54 | size = element.nextInt(); 55 | int array[] = new int[size]; 56 | 57 | System.out.print("Enter the elements of the array : "); 58 | 59 | for (int i = 0; i < size; i++) { 60 | array[i] = element.nextInt(); 61 | } 62 | 63 | element.close(); 64 | 65 | int sortedArray[] = InsertionBinarySearch(array); 66 | 67 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 68 | } 69 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q6BinarySearch_Insertion.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def BinarySearch(array, p , r, value): 4 | 5 | if (r array[p]: 7 | return p+1 8 | else: 9 | return p 10 | 11 | q = math.floor((p + r)/ 2) 12 | index = -1 13 | 14 | if (array[q] > value): 15 | return BinarySearch(array, p, q-1, value) 16 | 17 | elif (array[q] < value): 18 | return BinarySearch(array, q+1, r, value) 19 | 20 | else: 21 | index = q + 1 22 | 23 | return index 24 | 25 | def insertionBinarySort(array = []): 26 | sortedArray = array 27 | for i in range(len(array)): 28 | key = sortedArray[i] 29 | j = i - 1 30 | 31 | pos = BinarySearch(array, 0, i, key) 32 | 33 | while (j >= pos): 34 | sortedArray[j + 1] = sortedArray[j] 35 | j = j - 1 36 | sortedArray[j + 1] = key 37 | return sortedArray 38 | 39 | if __name__ == "__main__": 40 | print("Enter the size of Array : ", end="") 41 | size = int(input()) 42 | 43 | print("Enter the elements of the array : ", end="") 44 | array = list(map(lambda x: int(x), input().split())) 45 | 46 | sortedArray = insertionBinarySort(array) 47 | 48 | print("New Sorted Array is : ", sortedArray) -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q7 -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int BinarySearch(int array[], int p, int r, int value) { 4 | if (r >= p){ 5 | 6 | int q = (p + r) / 2; 7 | if (array[q] < value) { 8 | return BinarySearch(array, q + 1, r, value); 9 | } 10 | else if (array[q] > value) { 11 | return BinarySearch(array, p, q - 1, value); 12 | } 13 | else { 14 | return q; 15 | } 16 | } 17 | return -1; 18 | } 19 | 20 | int *binarySearchSum(int array[], int size, int value, int retIndex[]) { 21 | int flag = 0; 22 | for (int i = 0 ; i < size ; i++) { 23 | int subValue = value - array[i]; 24 | int otherIndex = BinarySearch(array, 0, size - 1, subValue); 25 | if (otherIndex != -1) { 26 | retIndex[0] = i; 27 | retIndex[1] = otherIndex; 28 | flag = 1; 29 | retIndex[2] = flag; 30 | } 31 | } 32 | return retIndex; 33 | } 34 | void main() { 35 | printf("Enter the size of array you want : "); 36 | int size; 37 | scanf("%d", &size); 38 | int array[size]; 39 | printf("Enter the elements of the array : "); 40 | for (int i = 0 ; i < size ; i++ ) { 41 | scanf("%d", &array[i]); 42 | } 43 | 44 | printf("Enter the sum value : "); 45 | int value; 46 | scanf("%d", &value); 47 | int retIndex[] = {-1, -1, 0}; 48 | int *index = binarySearchSum(array, size, value, retIndex); 49 | if (index[2] != 0) { 50 | printf("Elements are found that sum %d and the elements are %d, %d ." ,value ,array[index[0]] ,array[index[1]]); 51 | } 52 | else { 53 | printf("Summed value is not found !!!"); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class q7 { 4 | 5 | public static int binarySearch(int array[], int p, int r, int value) { 6 | 7 | if (r >= p){ 8 | 9 | int q = (p + r) / 2; 10 | if (array[q] < value) { 11 | return binarySearch(array, q + 1, r, value); 12 | } 13 | else if (array[q] > value) { 14 | return binarySearch(array, p, q - 1, value); 15 | } 16 | else { 17 | return q; 18 | } 19 | } 20 | return -1; 21 | } 22 | 23 | public static int [] binarySearchSum(int array[], int size, int value) { 24 | int flag = 0; 25 | int retIndex[] = {-1, -1, 0}; 26 | for (int i = 0 ; i < size ; i++) { 27 | int subValue = value - array[i]; 28 | int otherIndex = binarySearch(array, 0, size - 1, subValue); 29 | if (otherIndex != -1) { 30 | retIndex[0] = i; 31 | retIndex[1] = otherIndex; 32 | flag = 1; 33 | retIndex[2] = flag; 34 | } 35 | } 36 | return retIndex; 37 | } 38 | 39 | public static void main(String[] args) { 40 | System.out.print("Enter the size of array you want : "); 41 | Scanner element = new Scanner(System.in); 42 | int size = element.nextInt(); 43 | int array[] = new int[size]; 44 | System.out.print("Enter the elements of the array : "); 45 | for (int i = 0 ; i < size ; i++ ) { 46 | array[i] = element.nextInt(); 47 | } 48 | 49 | System.out.print("Enter the sum value : "); 50 | int value = element.nextInt(); 51 | element.close(); 52 | int index[] = binarySearchSum(array, size, value); 53 | 54 | if (index[2] != 0) { 55 | System.out.println("Elements are found that sum " +value + " and the elements are [" +array[index[0]] +", " + array[index[1]]+ "]."); 56 | } 57 | else { 58 | System.out.println("Summed value is not found !!!"); 59 | } 60 | 61 | } 62 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/Exercise/Ex_2.3/q7.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def binarySearch(array, p, r, value): 4 | if (r >= p): 5 | 6 | q = math.floor((p + r) / 2) 7 | if (array[q] < value) : 8 | return binarySearch(array, q + 1, r, value) 9 | elif (array[q] > value) : 10 | return binarySearch(array, p, q - 1, value) 11 | else: 12 | return q 13 | return -1 14 | 15 | 16 | 17 | def binarySearchSum(array, size, value): 18 | flag = 0 19 | retIndex = [-1, -1, 0] 20 | for i in range(size): 21 | subValue = value - array[i] 22 | otherIndex = binarySearch(array, 0, size - 1, subValue) 23 | if (otherIndex != -1): 24 | retIndex[0] = i 25 | retIndex[1] = otherIndex 26 | flag = 1 27 | retIndex[2] = flag 28 | 29 | return retIndex 30 | 31 | 32 | if __name__ == "__main__": 33 | size = int(input("Enter the size of array you want : ")) 34 | 35 | array = list(map(lambda x: int(x), input("Enter the elements of the array : ").split())) 36 | 37 | value = int(input("Enter the sum value : ")) 38 | 39 | index = binarySearchSum(array, size, value) 40 | 41 | if (index[2] != 0): 42 | print("Elements are found that sum", value, "and the elements are", array[index[0]],", " , array[index[1]]) 43 | 44 | else: 45 | print("Summed value is not found !!!") 46 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started 2 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/insertionSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/InChaptAlgo/insertionSort -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *insertionSort(int array[], int size) { 4 | 5 | for (int i = 1; i < size; i++) { 6 | int key = array[i]; 7 | int j = i - 1; 8 | while (j > -1 && array[j] > key) { 9 | array[j + 1] = array[j]; 10 | j = j - 1; 11 | } 12 | array[j + 1] = key; 13 | } 14 | return array; 15 | } 16 | int main() { 17 | int size; 18 | printf("Enter the size of array you need : "); 19 | scanf("%d", &size); 20 | int array[size]; 21 | printf("Enter the elements of the array \n"); 22 | for (int i = 0 ; i< size; i++) { 23 | scanf("%d", &array[i]); 24 | } 25 | int *sortedArray; 26 | sortedArray = insertionSort(array, size); 27 | 28 | printf("Sorted array is : [ "); 29 | for (int i = 0; i < size; i++) { 30 | printf("%d ", sortedArray[i]); 31 | } 32 | printf("]"); 33 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/insertionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class insertionSort { 5 | 6 | 7 | public static int[] sorting (int[] array) { 8 | int sortedArray[] = array; 9 | for(int j = 1; j < sortedArray.length; j++) { 10 | int key = sortedArray[j]; 11 | int i = j - 1 ; 12 | while (i >-1 && sortedArray[i] > key ) { 13 | sortedArray[i + 1] = sortedArray[i]; 14 | i = i - 1; 15 | } 16 | sortedArray[i+1] = key; 17 | } 18 | 19 | return(sortedArray); 20 | } 21 | public static void main(String[] args){ 22 | 23 | int size; 24 | System.out.print("Enter the size of array you wish to have :- "); 25 | Scanner element = new Scanner(System.in); 26 | size = element.nextInt(); 27 | int array[] = new int[size]; 28 | 29 | System.out.print("Enter the elements of the array : "); 30 | 31 | for (int i = 0; i < size; i++) { 32 | array[i] = element.nextInt(); 33 | } 34 | 35 | element.close(); 36 | 37 | int sortedArray[] = sorting(array); 38 | 39 | System.out.println("New Sorted array is : " + Arrays.toString(sortedArray)); 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/insertionSort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(array = []): 2 | sortedArray = array 3 | for i in range(len(array)): 4 | key = sortedArray[i] 5 | j = i - 1 6 | while (j > -1 and sortedArray[j] > key): 7 | sortedArray[j + 1] = sortedArray[j] 8 | j = j - 1 9 | sortedArray[j + 1] = key 10 | return sortedArray 11 | 12 | print("Enter the size of Array : ", end="") 13 | size = int(input()) 14 | 15 | print("Enter the elements of the array : ", end="") 16 | array = list(map(lambda x: int(x), input().split())) 17 | 18 | sortedArray = insertionSort(array) 19 | 20 | print("New Sorted Array is : ", sortedArray) 21 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/mergeSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp2/InChaptAlgo/mergeSort -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp2/InChaptAlgo/mergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *merge(int array[], int p, int q, int r) { 4 | int n1 = q - p + 1; 5 | int n2 = r - q; 6 | 7 | int left[n1 + 1]; 8 | int right[n2 + 1]; 9 | 10 | for(int i = 0 ; i 2 | 3 | int Monge(int size, int array[size][size]) { 4 | int count = 0; 5 | for (int i = 0 ; i < size-1; i++) { 6 | for(int j = 0 ; j < size-1 ; j++) { 7 | if ((array[i][j] + array[i+1][j+1]) <= (array[i][j+1] + array[i+1][j])) { 8 | count = 1; 9 | } 10 | else { 11 | return -1; 12 | } 13 | } 14 | } 15 | return count; 16 | } 17 | 18 | void main () { 19 | 20 | printf("Enter the size of array : "); 21 | int size; 22 | scanf("%d", &size); 23 | int array[size][size]; 24 | printf("Enter the elements of array : \n"); 25 | for (int i = 0 ; i < size ; i++) { 26 | printf("Enter element of row (%d) : " ,(i+1)); 27 | for(int j = 0 ; j < size ; j++) { 28 | scanf("%d", &array[i][j]); 29 | } 30 | } 31 | 32 | int result = Monge(size, array); 33 | 34 | if (result == -1) { 35 | printf("Given array is not a Monge Array. \n"); 36 | } 37 | 38 | else if (result == 1) { 39 | printf("Given array is a Monge Array. \n"); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Chapter_Problem/MongeArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class MongeArray { 4 | 5 | public static int Monge(int array[][], int size) { 6 | int count = 0; 7 | for (int i = 0 ; i < size-1; i++) { 8 | for(int j = 0 ; j < size-1 ; j++) { 9 | if ((array[i][j] + array[i+1][j+1]) <= (array[i][j+1] + array[i+1][j])) { 10 | count = 1; 11 | } 12 | else { 13 | return -1; 14 | } 15 | } 16 | } 17 | return count; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Scanner element = new Scanner(System.in); 22 | System.out.print("Enter the size of array : "); 23 | int size = element.nextInt(); 24 | int array[][] = new int[size][size]; 25 | System.out.println("Enter the elements of array :"); 26 | for (int i = 0 ; i < size ; i++) { 27 | System.out.print("Enter element of row (" +(i+1)+") : "); 28 | for(int j = 0 ; j < size ; j++) { 29 | array[i][j] = element.nextInt(); 30 | } 31 | } 32 | element.close(); 33 | int result = Monge(array, size); 34 | 35 | if (result == -1) { 36 | System.out.println("Given array is not a Monge Array. "); 37 | } 38 | else if (result == 1) { 39 | System.out.println("Given array is a Monge Array."); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Chapter_Problem/MongeArray.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def Monge(size, array): 4 | count = 0 5 | for i in range(size-1): 6 | for j in range(size-1): 7 | if ((array[i][j] + array[i+1][j+1]) <= (array[i][j+1] + array[i+1][j])): 8 | count = 1 9 | 10 | else: 11 | return -1 12 | 13 | return count 14 | 15 | 16 | if __name__ == "__main__": 17 | 18 | size = int(input("Enter the size of array : ")) 19 | 20 | array = [] 21 | print("Enter the elements of array : ") 22 | for i in range(size): 23 | array.append([]) 24 | print("Enter element of row " ,(i+1), " : ", end="" ) 25 | array[i] = list(map(lambda x: int(x), input().split())) 26 | 27 | 28 | 29 | result = Monge(size, array) 30 | 31 | if (result == -1): 32 | print("Given array is not a Monge Array. ") 33 | 34 | 35 | elif (result == 1): 36 | print("Given array is a Monge Array. ") 37 | 38 | 39 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q2BruteForceMaxSubarray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q2BruteForceMaxSubarray -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q2BruteForceMaxSubarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int MaxSubarray(int *array, int size) { 5 | int sum = 0; 6 | int maxSum = -99999999; 7 | 8 | for (int i = 0 ; i < size-1 ; i++) { 9 | sum = array[i]; 10 | for (int j = i+1 ; j < size ; j++) { 11 | sum += array[j]; 12 | if (sum > maxSum) { 13 | maxSum = sum; 14 | } 15 | } 16 | 17 | } 18 | return maxSum; 19 | } 20 | 21 | void main() { 22 | 23 | printf("Enter the size of array :- "); 24 | int size; 25 | scanf("%d", &size); 26 | 27 | printf("Enter the elements of the array : "); 28 | 29 | int array[size]; 30 | for(int i = 0; i maxSum) { 15 | maxSum = sum; 16 | } 17 | } 18 | 19 | } 20 | return maxSum; 21 | } 22 | 23 | public static void main(String[] args) { 24 | 25 | System.out.print("Enter the size of array :- "); 26 | Scanner element = new Scanner(System.in); 27 | int size = element.nextInt(); 28 | 29 | System.out.print("Enter the elements of the array : "); 30 | 31 | int array[] = new int[size]; 32 | for(int i = 0; i 2 | 3 | int NonRecursive(int *array, int j) { 4 | 5 | int sum = 0; 6 | int maxSum = -9999999; 7 | for (int i = j ; i > -1 ; i--) { 8 | sum += array[i]; 9 | if (maxSum < sum) { 10 | maxSum = sum; 11 | } 12 | else { 13 | break; 14 | } 15 | } 16 | 17 | if ((maxSum+array[j+1]) > maxSum) { 18 | maxSum+= array[j+1]; 19 | } 20 | return maxSum; 21 | } 22 | 23 | void main() { 24 | 25 | printf("Enter the size of array :- "); 26 | int size; 27 | scanf("%d", &size); 28 | 29 | printf("Enter the elements of the array : "); 30 | 31 | int array[size]; 32 | for(int i = 0; i= size-1) { 40 | j = size-2; 41 | } 42 | int MaxSum = NonRecursive(array, j); 43 | 44 | printf("Maximum SubArray sum is : %d\n" ,MaxSum); 45 | 46 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q5NonRecursiveMaxSubarray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class q5NonRecursiveMaxSubarray { 5 | 6 | public static int NonRecursive(int array[], int j) { 7 | 8 | int sum = 0; 9 | int maxSum = -9999999; 10 | for (int i = j ; i > -1 ; i--) { 11 | sum += array[i]; 12 | if (maxSum < sum) { 13 | maxSum = sum; 14 | } 15 | else { 16 | break; 17 | } 18 | } 19 | 20 | if ((maxSum+array[j+1]) > maxSum) { 21 | maxSum+= array[j+1]; 22 | } 23 | return maxSum; 24 | } 25 | 26 | public static void main(String[] args) { 27 | 28 | System.out.print("Enter the size of array :- "); 29 | Scanner element = new Scanner(System.in); 30 | int size = element.nextInt(); 31 | 32 | System.out.print("Enter the elements of the array : "); 33 | 34 | int array[] = new int[size]; 35 | for(int i = 0; i= size-1) { 43 | j = size-2; 44 | } 45 | int MaxSum = NonRecursive(array, j); 46 | 47 | System.out.println("Maximum SubArray sum is : " +MaxSum); 48 | 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q5NonRecursiveMaxSubarray.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.1/q5NonRecursiveMaxSubarray.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int split(int **p, int **c, int iB, int jB, int size) { 7 | int n = size; 8 | for (int i1=0, i2=iB ; i1 < n ; i1++, i2++) { 9 | for (int j1=0,j2=jB; j1 size) { 222 | for (int i = size ; i < newSize; i++) { 223 | for (int j = size ; j < newSize ; j++) { 224 | newArrayA[i][j] = 0; 225 | newArrayB[i][j] = 0; 226 | } 227 | } 228 | 229 | for (int i = 0 ; i < size ; i++) { 230 | for (int j = 0 ; j < size ; j++) { 231 | newArrayA[i][j] = arrayA[i][j]; 232 | newArrayB[i][j] = arrayB[i][j]; 233 | } 234 | } 235 | } 236 | 237 | int **arrayC = Multiply(newArrayA, newArrayB, newSize); 238 | printf("Array C is : \n"); 239 | for (int i = 0 ; i < size ; i++) { 240 | printf("\t\t[ "); 241 | for(int j = 0 ; j < size ; j++) { 242 | printf("%d ",arrayC[i][j]); 243 | } 244 | printf("]\n"); 245 | } 246 | } 247 | 248 | 249 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | 5 | public class q3StrassenNonSquare { 6 | 7 | public static void split(int[][] p, int[][] c, int iB, int jB) { 8 | for (int i1=0, i2=iB ; i1 < c.length ; i1++, i2++) { 9 | for (int j1=0,j2=jB; j1 size) { 137 | for (int i = size ; i < newSize; i++) { 138 | for (int j = size ; j < newSize ; j++) { 139 | newArrayA[i][j] = 0; 140 | newArrayB[i][j] = 0; 141 | } 142 | } 143 | 144 | for (int i = 0 ; i < size ; i++) { 145 | for (int j = 0 ; j < size ; j++) { 146 | newArrayA[i][j] = arrayA[i][j]; 147 | newArrayB[i][j] = arrayB[i][j]; 148 | } 149 | } 150 | } 151 | System.out.println("Array A is : "); 152 | for (int i = 0 ; i < size ; i++) { 153 | System.out.println("\t\t" +Arrays.toString(arrayA[i])); 154 | } 155 | 156 | System.out.println("Array B is : "); 157 | for (int i = 0 ; i < size ; i++) { 158 | System.out.println("\t\t" +Arrays.toString(arrayB[i])); 159 | } 160 | 161 | int newArrayC[][] = Multiply(newArrayA, newArrayB); 162 | 163 | int arrayC[][] = new int[size][size]; 164 | for (int i = 0 ; i < size ; i++) { 165 | for (int j = 0 ; j < size ; j++) { 166 | arrayC[i][j] = newArrayC[i][j]; 167 | } 168 | } 169 | System.out.println("Array C is : "); 170 | for (int i = 0 ; i < size ; i++) { 171 | System.out.println("\t\t" +Arrays.toString(arrayC[i])); 172 | } 173 | } 174 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q3StrassenNonSquare.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *ComplexMulti(int *A, int *B, int *C) { 4 | int M1 = B[0] + B[1]; 5 | int M2 = A[0] + A[1]; 6 | int M3 = B[1] - B[0]; 7 | 8 | C[0] = (A[0]*M1 - B[1]*M2); 9 | C[1] = (B[1]*M2 - A[1]*M3); 10 | 11 | return C; 12 | } 13 | 14 | 15 | void main() { 16 | int complexA[2]; 17 | int complexB[2]; 18 | 19 | printf("Enter the Complex A : "); 20 | for (int i = 0; i < 2; i++) { 21 | scanf("%d", &complexA[i]); 22 | } 23 | 24 | printf("Enter the Complex B : "); 25 | for (int i = 0; i < 2; i++) { 26 | scanf("%d", &complexB[i]); 27 | } 28 | 29 | int complexC[2]; 30 | ComplexMulti(complexA, complexB, complexC); 31 | 32 | printf("Result of A*B complex number is : %d +i(%d)\n", complexC[0], complexC[1]); 33 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class q7ComplexMulti { 5 | 6 | public static int[] ComplexMulti(int[] A, int[] B) { 7 | int M1 = B[0] + B[1]; 8 | int M2 = A[0] + A[1]; 9 | int M3 = B[1] - B[0]; 10 | 11 | int []C = new int[2]; 12 | C[0] = (A[0]*M1 - B[1]*M2); 13 | C[1] = (B[1]*M2 - A[1]*M3); 14 | 15 | return C; 16 | } 17 | 18 | 19 | public static void main(String[] args) { 20 | Scanner element = new Scanner(System.in); 21 | int complexA[] = new int[2]; 22 | int complexB[] = new int[2]; 23 | 24 | System.out.print("Enter the Complex A : "); 25 | for (int i = 0; i < 2; i++) { 26 | complexA[i] = element.nextInt(); 27 | } 28 | 29 | System.out.print("Enter the Complex B : "); 30 | for (int i = 0; i < 2; i++) { 31 | complexB[i] = element.nextInt(); 32 | } 33 | element.close(); 34 | int complexC[] = new int[2]; 35 | complexC = ComplexMulti(complexA, complexB); 36 | 37 | System.out.println("Result of A*B complex number is : "+complexC[0]+" + i"+complexC[1]); 38 | } 39 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/Exercises/Ex_4.2/q7ComplexMulti.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int **ArrayMulti(int size, int arrayA[size][size], int arrayB[size][size], int result[size][size]) { 5 | 6 | for (int i = 0 ; i < size ; i ++) { 7 | for (int j = 0 ; j < size ;j++) { 8 | result[i][j] = 0; 9 | for (int k = 0 ; k < size ; k++) { 10 | result[i][j] = result[i][j] + arrayA[i][k] * arrayB[k][j]; 11 | } 12 | } 13 | } 14 | 15 | return result; 16 | } 17 | 18 | void main () { 19 | 20 | printf("Enter the size of array : "); 21 | int size; 22 | scanf("%d", &size); 23 | 24 | int arrayA[size][size]; 25 | int arrayB[size][size]; 26 | printf("Enter the elements for array A \n"); 27 | for (int i = 0 ; i < size ; i++) { 28 | printf("Enter element of row (%d) : " ,(i+1)); 29 | for(int j = 0 ; j < size ; j++) { 30 | scanf("%d", &arrayA[i][j]); 31 | } 32 | } 33 | 34 | printf("Enter the elements for array B \n"); 35 | for (int i = 0 ; i < size ; i++) { 36 | printf("Enter element of row (%d) : " ,(i+1)); 37 | for(int j = 0 ; j < size ; j++) { 38 | scanf("%d", &arrayB[i][j]); 39 | } 40 | } 41 | 42 | printf("Array A is : \n"); 43 | for (int i = 0 ; i < size ; i++) { 44 | printf("\t\t[ "); 45 | for(int j = 0 ; j < size ; j++) { 46 | printf("%d ",arrayA[i][j]); 47 | } 48 | printf("]\n"); 49 | } 50 | 51 | printf("Array B is : \n"); 52 | for (int i = 0 ; i < size ; i++) { 53 | printf("\t\t[ "); 54 | for(int j = 0 ; j < size ; j++) { 55 | printf("%d ",arrayB[i][j]); 56 | } 57 | printf("]\n"); 58 | } 59 | int arrayC[size][size]; 60 | ArrayMulti(size, arrayA, arrayB, arrayC); 61 | 62 | printf("Array C is : \n"); 63 | for (int i = 0 ; i < size ; i++) { 64 | printf("\t\t[ "); 65 | for(int j = 0 ; j < size ; j++) { 66 | printf("%d ", arrayC[i][j]); 67 | } 68 | printf("]\n"); 69 | } 70 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | 5 | public class MatrixMutliplication { 6 | 7 | public static int[][] ArrayMulti(int arrayA[][], int arrayB[][], int size) { 8 | int result[][] = new int[size][size]; 9 | for (int i = 0 ; i < size ; i ++) { 10 | for (int j = 0 ; j < size ;j++) { 11 | result[i][j] = 0; 12 | 13 | for (int k = 0 ; k < size ; k++) { 14 | result[i][j] = result[i][j] + arrayA[i][k] * arrayB[k][j]; 15 | } 16 | } 17 | } 18 | 19 | return result; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Scanner element = new Scanner(System.in); 24 | System.out.print("Enter the size of array : "); 25 | int size = element.nextInt(); 26 | 27 | int arrayA[][] = new int[size][size]; 28 | int arrayB[][] = new int[size][size]; 29 | System.out.println("Enter the elements for array A"); 30 | for (int i = 0 ; i < size ; i++) { 31 | System.out.print("Enter element of row (" +(i+1)+") : "); 32 | for(int j = 0 ; j < size ; j++) { 33 | arrayA[i][j] = element.nextInt(); 34 | } 35 | } 36 | 37 | System.out.println("Enter the elements for array B"); 38 | for (int i = 0 ; i < size ; i++) { 39 | System.out.print("Enter element of row (" +(i+1)+") : "); 40 | for(int j = 0 ; j < size ; j++) { 41 | arrayB[i][j] = element.nextInt(); 42 | } 43 | } 44 | element.close(); 45 | System.out.println("Array A is : "); 46 | for (int i = 0 ; i < size ; i++) { 47 | System.out.println("\t\t" +Arrays.toString(arrayA[i])); 48 | } 49 | 50 | System.out.println("Array B is : "); 51 | for (int i = 0 ; i < size ; i++) { 52 | System.out.println("\t\t" +Arrays.toString(arrayB[i])); 53 | } 54 | 55 | int arrayC[][] = ArrayMulti(arrayA, arrayB, size); 56 | System.out.println("Array C is : "); 57 | for (int i = 0 ; i < size ; i++) { 58 | System.out.println("\t\t" +Arrays.toString(arrayC[i])); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/InChaptAlgo/MatrixMutliplication.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MaxSubArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/InChaptAlgo/MaxSubArray -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/MaxSubArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *MaxCrossingArray(int array[], int low, int mid, int high, int result[]) { 4 | int leftSum = -99999999; //Representing negative infinity 5 | int sum = 0; 6 | int maxLeft = 0; 7 | int maxRight = 0; 8 | 9 | for (int i = mid ; i >low-1 ; i-- ) { 10 | sum = sum + array[i]; 11 | if (sum >leftSum) { 12 | leftSum = sum; 13 | maxLeft = i; 14 | } 15 | } 16 | 17 | int rightSum = -99999999; //Representing negative infinity 18 | sum = 0; 19 | 20 | for (int i = mid+1 ; i < high+1 ; i++) { 21 | sum = sum + array[i]; 22 | if (sum > rightSum) { 23 | rightSum = sum; 24 | maxRight = i; 25 | } 26 | } 27 | 28 | int res[] = {maxLeft, maxRight, leftSum+rightSum}; 29 | result = res; 30 | return result; 31 | } 32 | 33 | 34 | int *MaximumSubarray(int array[], int low, int high, int result[]) { 35 | 36 | if (high == low) { 37 | int res[] = {low, high, array[low]}; 38 | result = res; 39 | return result; 40 | } 41 | else { 42 | int mid = (low+high) / 2; 43 | 44 | int *leftResult = MaximumSubarray(array, low, mid, result); 45 | int *rightResult = MaximumSubarray(array, mid+1, high, result); 46 | 47 | int *crossResult = MaxCrossingArray(array, low, mid, high, result); 48 | 49 | if (leftResult[2] >= rightResult[2] && leftResult[2] >= crossResult[2]) { 50 | result = leftResult; 51 | return result; 52 | } 53 | else if (rightResult[2] >= leftResult[2] && rightResult[2] >= crossResult[2]) { 54 | result = rightResult; 55 | return result; 56 | } 57 | else { 58 | result = crossResult; 59 | return result; 60 | } 61 | 62 | } 63 | } 64 | 65 | 66 | void main() { 67 | 68 | printf("Enter the size of array :- "); 69 | int size; 70 | scanf("%d", &size); 71 | 72 | printf("Enter the elements of the array : "); 73 | 74 | int array[size]; 75 | for(int i = 0; ilow-1 ; i-- ) { 12 | sum = sum + array[i]; 13 | if (sum >leftSum) { 14 | leftSum = sum; 15 | maxLeft = i; 16 | } 17 | } 18 | 19 | int rightSum = -99999999; //Representing negative infinity 20 | sum = 0; 21 | 22 | for (int i = mid+1 ; i < high+1 ; i++) { 23 | sum = sum + array[i]; 24 | if (sum > rightSum) { 25 | rightSum = sum; 26 | maxRight = i; 27 | } 28 | } 29 | 30 | int result[] = {maxLeft, maxRight, leftSum+rightSum}; 31 | return result; 32 | } 33 | 34 | 35 | public static int[] MaximumSubarray(int array[], int low, int high) { 36 | 37 | if (high == low) { 38 | int result[] ={low, high, array[low]}; 39 | return result; 40 | } 41 | else { 42 | int mid = (low+high) / 2; 43 | 44 | int leftResult[] = MaximumSubarray(array, low, mid); 45 | int rightResult[] = MaximumSubarray(array, mid+1, high); 46 | 47 | int crossResult[] = MaxCrossingArray(array, low, mid, high); 48 | 49 | if (leftResult[2] >= rightResult[2] && leftResult[2] >= crossResult[2]) { 50 | int result[] = leftResult; 51 | return result; 52 | } 53 | else if (rightResult[2] >= leftResult[2] && rightResult[2] >= crossResult[2]) { 54 | int result[] = rightResult; 55 | return result; 56 | } 57 | else { 58 | int result[] = crossResult; 59 | return result; 60 | } 61 | } 62 | } 63 | 64 | 65 | public static void main(String[] args) { 66 | 67 | System.out.print("Enter the size of array :- "); 68 | Scanner element = new Scanner(System.in); 69 | int size = element.nextInt(); 70 | 71 | System.out.print("Enter the elements of the array : "); 72 | 73 | int array[] = new int[size]; 74 | for(int i = 0; i 2 | #include 3 | #include 4 | 5 | 6 | int **MatrixMulti(int rowA, int colA, int rowB, int colB, int size, int **arrayA, int **arrayB) { 7 | 8 | int **arrayC; 9 | arrayC = malloc((size+1) * sizeof(*arrayC)); 10 | for(int i = 0 ; i < size+1 ; i++) { 11 | arrayC[i] = malloc((size+1) * sizeof(*arrayC)); 12 | } 13 | if(size == 1) { 14 | arrayC[0][0] = arrayA[rowA][colA]*arrayB[rowB][colB]; 15 | } 16 | 17 | else{ 18 | int rowC, colC; 19 | int newSize = size/2; 20 | 21 | int **newArrayA1 = MatrixMulti(rowA, colA, rowB, colB, newSize, arrayA, arrayB); 22 | int **newArrayB1 = MatrixMulti(rowA, colA+newSize, rowB+newSize, colB, newSize, arrayA, arrayB); 23 | rowC = 0; 24 | colC = 0; 25 | int n = newSize; 26 | for (int i = 0; i < n; i++) { 27 | for(int j = 0 ; j < n; j++ ) { 28 | arrayC[i + rowC][j+colC] = newArrayA1[i][j] + newArrayB1[i][j]; 29 | } 30 | } 31 | int **newArrayA2 = MatrixMulti(rowA, colA, rowB, colB + newSize, newSize, arrayA, arrayB); 32 | int **newArrayB2 = MatrixMulti(rowA, colA+newSize, rowB+newSize, colB+newSize, newSize, arrayA, arrayB); 33 | rowC = 0; 34 | colC = newSize; 35 | 36 | for (int i = 0; i < n; i++) { 37 | for(int j = 0 ; j < n; j++ ) { 38 | arrayC[i + rowC][j+colC] = newArrayA2[i][j] + newArrayB2[i][j]; 39 | } 40 | } 41 | 42 | 43 | int **newArrayA3 = MatrixMulti(rowA+newSize, colA, rowB, colB, newSize, arrayA, arrayB); 44 | int **newArrayB3 = MatrixMulti(rowA + newSize, colA+newSize, rowB+newSize, colB, newSize, arrayA, arrayB); 45 | rowC = newSize; 46 | colC = 0; 47 | for (int i = 0; i < n; i++) { 48 | for(int j = 0 ; j < n; j++ ) { 49 | arrayC[i + rowC][j+colC] = newArrayA3[i][j] + newArrayB3[i][j]; 50 | } 51 | } 52 | 53 | int **newArrayA4 = MatrixMulti(rowA + newSize, colA, rowB, colB + newSize, newSize, arrayA, arrayB); 54 | int **newArrayB4 = MatrixMulti(rowA + newSize, colA+newSize, rowB+newSize, colB+newSize, newSize, arrayA, arrayB); 55 | rowC = newSize; 56 | colC = newSize; 57 | for (int i = 0; i < n; i++) { 58 | for(int j = 0 ; j < n; j++ ) { 59 | arrayC[i + rowC][j+colC] = newArrayA4[i][j] + newArrayB4[i][j]; 60 | } 61 | 62 | } 63 | 64 | } 65 | 66 | return arrayC; 67 | 68 | } 69 | 70 | void main() { 71 | 72 | printf("Enter the size of array : "); 73 | int size; 74 | scanf("%d", &size); 75 | 76 | while (((int)(ceil((log(size) / log(2)))) != (int)(floor((log(size) / log(2)))))) { 77 | printf("Enter size to be a power of 2 : "); 78 | scanf("%d", &size); 79 | } 80 | 81 | int **arrayA; 82 | arrayA = malloc(size * sizeof(*arrayA)); 83 | for(int i = 0 ; i < size ; i++) { 84 | arrayA[i] = malloc(size * sizeof(*arrayA)); 85 | } 86 | printf("Enter the elements for array A \n"); 87 | for (int i = 0 ; i < size ; i++) { 88 | printf("Enter element of row (%d) : " ,(i+1)); 89 | for(int j = 0 ; j < size ; j++) { 90 | scanf("%d", &arrayA[i][j]); 91 | } 92 | } 93 | int **arrayB; 94 | arrayB = malloc(size * sizeof(*arrayB)); 95 | for(int i = 0 ; i < size ; i++) { 96 | arrayB[i] = malloc(size * sizeof(*arrayB)); 97 | } 98 | 99 | printf("Enter the elements for array B \n"); 100 | for (int i = 0 ; i < size ; i++) { 101 | printf("Enter element of row (%d) : " ,(i+1)); 102 | for(int j = 0 ; j < size ; j++) { 103 | scanf("%d", &arrayB[i][j]); 104 | } 105 | } 106 | 107 | 108 | printf("Array A is : \n"); 109 | for (int i = 0 ; i < size ; i++) { 110 | printf("\t\t[ "); 111 | for(int j = 0 ; j < size ; j++) { 112 | printf("%d ",arrayA[i][j]); 113 | } 114 | printf("]\n"); 115 | } 116 | 117 | 118 | printf("Array B is : \n"); 119 | for (int i = 0 ; i < size ; i++) { 120 | printf("\t\t[ "); 121 | for(int j = 0 ; j < size ; j++) { 122 | printf("%d ",arrayB[i][j]); 123 | } 124 | printf("]\n"); 125 | } 126 | 127 | int **arrayC = MatrixMulti(0, 0, 0, 0, size, arrayA, arrayB); 128 | printf("Array C is : \n"); 129 | for (int i = 0 ; i < size ; i++) { 130 | printf("\t\t[ "); 131 | for(int j = 0 ; j < size ; j++) { 132 | printf("%d ",arrayC[i][j]); 133 | } 134 | printf("]\n"); 135 | } 136 | } 137 | 138 | 139 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/RecursiveMatrixMulti.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | import java.lang.Math; 4 | 5 | 6 | 7 | public class RecursiveMatrixMulti { 8 | 9 | public static void sumMatrix(int[][]arrayC, int[][]arrayA, int[][]arrayB, int rowC, int colC) { 10 | int n = arrayA.length; 11 | for (int i = 0; i < n; i++) { 12 | for(int j = 0 ; j < n; j++ ) { 13 | arrayC[i + rowC][j+colC] = arrayA[i][j] + arrayB[i][j]; 14 | } 15 | } 16 | } 17 | 18 | public static int[][] MatrixMulti(int[][] arrayA, int[][] arrayB, int rowA, int colA, int rowB, int colB, int size) { 19 | int [][] arrayC = new int[size][size]; 20 | 21 | if(size == 1) { 22 | arrayC[0][0] = arrayA[rowA][colA]*arrayB[rowB][colB]; 23 | } 24 | 25 | else{ 26 | int newSize = size/2; 27 | 28 | sumMatrix(arrayC, MatrixMulti(arrayA, arrayB, rowA, colA, rowB, colB, newSize), MatrixMulti(arrayA, arrayB, rowA, colA+newSize, rowB+newSize, colB, newSize), 0, 0); 29 | 30 | sumMatrix(arrayC, MatrixMulti(arrayA, arrayB, rowA, colA, rowB, colB + newSize, newSize), MatrixMulti(arrayA, arrayB, rowA, colA+newSize, rowB+newSize, colB+newSize, newSize), 0, newSize); 31 | 32 | sumMatrix(arrayC, MatrixMulti(arrayA, arrayB, rowA + newSize, colA, rowB, colB, newSize), MatrixMulti(arrayA, arrayB, rowA + newSize, colA+newSize, rowB+newSize, colB, newSize), newSize, 0); 33 | 34 | sumMatrix(arrayC, MatrixMulti(arrayA, arrayB, rowA + newSize, colA, rowB, colB + newSize, newSize), MatrixMulti(arrayA, arrayB, rowA + newSize, colA+newSize, rowB+newSize, colB+newSize, newSize), newSize, newSize); 35 | 36 | } 37 | 38 | return arrayC; 39 | } 40 | 41 | public static void main(String[] args) { 42 | Scanner element = new Scanner(System.in); 43 | System.out.print("Enter the size of array : "); 44 | int size = element.nextInt(); 45 | 46 | while (((int)(Math.ceil((Math.log(size) / Math.log(2)))) != (int)(Math.floor((Math.log(size) / Math.log(2)))))) { 47 | System.out.print("Enter size to be a power of 2 : "); 48 | size = element.nextInt(); 49 | } 50 | int arrayA[][] = new int[size][size]; 51 | int arrayB[][] = new int[size][size]; 52 | System.out.println("Enter the elements for array A"); 53 | for (int i = 0 ; i < size ; i++) { 54 | System.out.print("Enter element of row (" +(i+1)+") : "); 55 | for(int j = 0 ; j < size ; j++) { 56 | arrayA[i][j] = element.nextInt(); 57 | } 58 | } 59 | 60 | System.out.println("Enter the elements for array B"); 61 | for (int i = 0 ; i < size ; i++) { 62 | System.out.print("Enter element of row (" +(i+1)+") : "); 63 | for(int j = 0 ; j < size ; j++) { 64 | arrayB[i][j] = element.nextInt(); 65 | } 66 | } 67 | element.close(); 68 | System.out.println("Array A is : "); 69 | for (int i = 0 ; i < size ; i++) { 70 | System.out.println("\t\t" +Arrays.toString(arrayA[i])); 71 | } 72 | 73 | System.out.println("Array B is : "); 74 | for (int i = 0 ; i < size ; i++) { 75 | System.out.println("\t\t" +Arrays.toString(arrayB[i])); 76 | } 77 | 78 | int arrayC[][] = MatrixMulti(arrayA, arrayB, 0, 0, 0, 0, size); 79 | System.out.println("Array C is : "); 80 | for (int i = 0 ; i < size ; i++) { 81 | System.out.println("\t\t" +Arrays.toString(arrayC[i])); 82 | } 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/RecursiveMatrixMulti.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/InChaptAlgo/RecursiveMatrixMulti.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/StrassenMatrixMulti: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp4/InChaptAlgo/StrassenMatrixMulti -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp4/InChaptAlgo/StrassenMatrixMulti.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int split(int **p, int **c, int iB, int jB, int size) { 7 | int n = size; 8 | for (int i1=0, i2=iB ; i1 < n ; i1++, i2++) { 9 | for (int j1=0,j2=jB; j1array[i]) { 21 | largest = l; 22 | } 23 | else { 24 | largest = i; 25 | } 26 | 27 | if (r <= heapSize && array[r]>array[largest]) { 28 | largest = r; 29 | } 30 | 31 | if (largest != i) { 32 | int temp = array[i]; 33 | array[i] = array[largest]; 34 | array[largest] = temp; 35 | i = largest; 36 | } 37 | else { 38 | break; 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.2/q5LoopMaxHeapify.java: -------------------------------------------------------------------------------- 1 | 2 | public class q5LoopMaxHeapify { 3 | 4 | public static int Parent(int i) { 5 | return i / 2; 6 | } 7 | 8 | public static int Left(int i) { 9 | return (2*i)+1; 10 | } 11 | 12 | public static int Right(int i) { 13 | return (2*i)+2; 14 | } 15 | 16 | public static void Max(int array[], int i, int heapSize) { 17 | while(i <=heapSize) { 18 | int l = Left(i); 19 | int r = Right(i); 20 | int largest; 21 | if (l<=heapSize && array[l]>array[i]) { 22 | largest = l; 23 | } 24 | else { 25 | largest = i; 26 | } 27 | 28 | if (r <= heapSize && array[r]>array[largest]) { 29 | largest = r; 30 | } 31 | 32 | if (largest != i) { 33 | int temp = array[i]; 34 | array[i] = array[largest]; 35 | array[largest] = temp; 36 | i = largest; 37 | } 38 | else { 39 | break; 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.2/q5LoopMaxHeapify.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.2/q5LoopMaxHeapify.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.5/q3.java: -------------------------------------------------------------------------------- 1 | public class q3 { 2 | 3 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.5/q6.java: -------------------------------------------------------------------------------- 1 | public class q6 { 2 | 3 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/Exercises/Ex_6.5/q7.java: -------------------------------------------------------------------------------- 1 | public class q7 { 2 | 3 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/BuildMaxHeap.h: -------------------------------------------------------------------------------- 1 | #include "MaxHeapify.h" 2 | 3 | int Build(int array[], int size) { 4 | int heapSize = size - 1; 5 | for (int i = heapSize/2; i>-1; i--) { 6 | Max(array, i, heapSize); 7 | } 8 | 9 | return heapSize; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/BuildMaxHeap.java: -------------------------------------------------------------------------------- 1 | public class BuildMaxHeap extends MaxHeapify{ 2 | public static int Build(int array[], int size) { 3 | int heapSize = size - 1; 4 | for (int i = heapSize/2; i>-1; i--) { 5 | Max(array, i, heapSize); 6 | } 7 | 8 | return heapSize; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/BuildMaxHeap.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/InChaptAlgo/BuildMaxHeap.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/HeapSort.h: -------------------------------------------------------------------------------- 1 | #include "BuildMaxHeap.h" 2 | void Sort(int array[], int size) { 3 | int heapSize = Build(array, size); 4 | for (int i = heapSize; i>0; i--) { 5 | int temp = array[0]; 6 | array[0] = array[i]; 7 | array[i] = temp; 8 | heapSize = heapSize - 1; 9 | Max(array, 0, heapSize); 10 | 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/HeapSort.java: -------------------------------------------------------------------------------- 1 | public class HeapSort extends BuildMaxHeap{ 2 | 3 | public static void Sort(int array[], int size) { 4 | int heapSize = Build(array, size); 5 | for (int i = heapSize; i>0; i--) { 6 | int temp = array[0]; 7 | array[0] = array[i]; 8 | array[i] = temp; 9 | heapSize = heapSize - 1; 10 | Max(array, 0, heapSize); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/HeapSort.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/InChaptAlgo/HeapSort.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/MaxHeapify.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | int Parent(int i) { 4 | return i/2; 5 | } 6 | 7 | int Left(int i) { 8 | return (2*i)+1; 9 | } 10 | 11 | int Right(int i) { 12 | return (2*i)+2; 13 | } 14 | 15 | void Max(int array[], int i, int heapSize) { 16 | 17 | int l = Left(i); 18 | int r = Right(i); 19 | int largest; 20 | 21 | if (l <= heapSize && array[l]>array[i]) { 22 | largest = l; 23 | } 24 | else { 25 | largest = i; 26 | } 27 | 28 | if (r <= heapSize && array[r]>array[largest]) { 29 | largest = r; 30 | } 31 | 32 | if (largest != i) { 33 | int temp = array[i]; 34 | array[i] = array[largest]; 35 | array[largest] = temp; 36 | Max(array, largest, heapSize); 37 | } 38 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/MaxHeapify.java: -------------------------------------------------------------------------------- 1 | public class MaxHeapify { 2 | 3 | 4 | private static int Parent(int i) { 5 | return (i/2); 6 | } 7 | 8 | private static int Left(int i) { 9 | return (2*i)+1; 10 | } 11 | 12 | private static int Right(int i) { 13 | return (2*i)+2; 14 | } 15 | 16 | public static void Max(int array[], int i, int heapSize) { 17 | 18 | int l = Left(i); 19 | int r = Right(i); 20 | int largest; 21 | 22 | if (l<=heapSize && array[l]>array[i]) { 23 | largest = l; 24 | } 25 | else { 26 | largest = i; 27 | } 28 | 29 | if (r <= heapSize && array[r] > array[largest]) { 30 | largest = r; 31 | } 32 | 33 | if (largest != i) { 34 | int temp = array[i]; 35 | array[i] = array[largest]; 36 | array[largest] = temp; 37 | Max(array, largest, heapSize); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/MaxHeapify.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/InChaptAlgo/MaxHeapify.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/master: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/InChaptAlgo/master -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/master.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "HeapSort.h" 4 | 5 | 6 | void main() { 7 | printf("Enter the size of array : "); 8 | int size; 9 | scanf("%d", &size); 10 | 11 | int array[size]; 12 | printf("Enter the elements of the array : "); 13 | 14 | for (int i = 0 ; i < size; i++) { 15 | scanf("%d", &array[i]); 16 | } 17 | printf("Your given array is : [ "); 18 | for (int i = 0; i < size; i++) { 19 | printf("%d ", array[i]); 20 | } 21 | printf("]\n"); 22 | 23 | Build(array, size); 24 | 25 | printf("Build Max Heap is : [ "); 26 | for (int i = 0; i < size; i++) { 27 | printf("%d ", array[i]); 28 | } 29 | printf("]\n"); 30 | 31 | Sort(array, size); 32 | printf("Your sorted array is : [ "); 33 | for (int i = 0; i < size; i++) { 34 | printf("%d ", array[i]); 35 | } 36 | printf("]\n"); 37 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/master.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class master extends HeapSort{ 5 | 6 | public static void main(String[] args) { 7 | Scanner element = new Scanner(System.in); 8 | System.out.print("Enter the size of array : "); 9 | int size = element.nextInt(); 10 | 11 | int array[] = new int[size]; 12 | System.out.print("Enter the elements of the array : "); 13 | 14 | for (int i = 0 ; i < size; i++) { 15 | array[i] = element.nextInt(); 16 | } 17 | element.close(); 18 | System.out.println("Your given array is : "+Arrays.toString(array)); 19 | Build(array, size); 20 | System.out.println("Build Max Heap is : " +Arrays.toString(array)); 21 | Sort(array, size); 22 | System.out.println("Your sorted array is : "+Arrays.toString(array)); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/InChaptAlgo/master.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bhavyagoel/Algorithms/132748562e89189ee86f5f42305c13ccc6836012/introductionToAlgorithmCLRS/chp6/InChaptAlgo/master.py -------------------------------------------------------------------------------- /introductionToAlgorithmCLRS/chp6/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 2 | ## Heapsort 3 | --------------------------------------------------------------------------------