From fe0f03f282e90455d3f6e3169c4538d0413038f9 Mon Sep 17 00:00:00 2001 From: Mohd Abbas Haidar Date: Wed, 18 Oct 2023 14:09:51 +0530 Subject: [PATCH 1/3] Add BinarySeach.java Removed the binary file BinarySearch.class and added the java code file BinarySearch.java --- BinarySearch.class | Bin 1111 -> 0 bytes BinarySearch.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) delete mode 100644 BinarySearch.class create mode 100644 BinarySearch.java diff --git a/BinarySearch.class b/BinarySearch.class deleted file mode 100644 index bcdbf9d3aca3cae82debfc506063820d717df256..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1111 zcmaJ=O-~b16g{t<&a}f|OQ9gpYE{IxNGp;MV}V4P5StWhQp7ZDcx_)`=yax;nG*Xa zT)Q;7V8cciYKY+nxc1i=zwWdcN@!fnyB~A!x#!+@-?!hVX8=~QtilJs0!c#vGDGB$ zAMu>Q&HY@tb|~sDgS@Jny1UNcPo;NM1QAl8Y8ZjWF#g20dDqEp=q9(j6~XQL0Yj*! zoBwnS?SV2U?7^0WTRJx8UT7+(Os93DjBcNNk=eBOT71tKLm5&nF2@Nq! zGEB92Qxq(-&fS-~dqA4wa?^DvH&smc(|i0WS2y?bDz0N%!HkAk#EJ0`d4Z#pD>85zrY1M?i&1(jEMk_9_H@A5E+?HBgC!ERD?raWqL6VL2<3HbhCzR{!p=Sw|N|Gf*Az~WU87JQ?X*cM_%p;5} zqFBZRRxpX16dgg}7Zk>R(qjn!faIf`B(>G&_5u>I1(qW6SGDSw%2g>)t_I|CRaVMX zC0MQoBXYU4pnQR{_7S7;Q!0p0n7U{=N)W&_5tzXoQF(wkLEa{x4|m7|B1(5d-vSoN MQc25F)I&`F1#FW99{>OV diff --git a/BinarySearch.java b/BinarySearch.java new file mode 100644 index 0000000..9a9aec3 --- /dev/null +++ b/BinarySearch.java @@ -0,0 +1,42 @@ +public class BinarySearch { + + // Binary search function + public static int binarySearch(int[] array, int target) { + int left = 0; + int right = array.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + // Check if the target is present at mid + if (array[mid] == target) { + return mid; + } + + // If the target is greater, ignore left half + if (array[mid] < target) { + left = mid + 1; + } + // If the target is smaller, ignore right half + else { + right = mid - 1; + } + } + + // Target not found in the array + return -1; + } + + // Main method to test binary search + public static void main(String[] args) { + int[] sortedArray = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72}; + int target = 23; + int result = binarySearch(sortedArray, target); + + if (result == -1) { + System.out.println("Element not present in the array"); + } else { + System.out.println("Element found at index: " + result); + } + } +} From 27b61b3c3cf7457f30a05dc1b3da1039dcec2820 Mon Sep 17 00:00:00 2001 From: Mohd Abbas Haidar Date: Wed, 18 Oct 2023 14:14:53 +0530 Subject: [PATCH 2/3] Add fibonacci.cpp Added fibonacci.cpp that contains C++ program to find and display the first n terms of the Fibonacci sequence --- fibonacci.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 fibonacci.cpp diff --git a/fibonacci.cpp b/fibonacci.cpp new file mode 100644 index 0000000..0726967 --- /dev/null +++ b/fibonacci.cpp @@ -0,0 +1,22 @@ +// C++ program to print first n terms of the Fibonacci series: + +#include + +using namespace std; + +int main() +{ + int n; + cin >> n; + + int a = -1, b = 1, c; + for (int i = 0; i < n; ++i) + { + c = a + b; + a = b; + b = c; + + cout << c << ' '; + } + return 0; +} \ No newline at end of file From 7b75a32644ea29d8619008593ba556268d9029fa Mon Sep 17 00:00:00 2001 From: Mohd Abbas Haidar Date: Wed, 18 Oct 2023 14:24:45 +0530 Subject: [PATCH 3/3] Add sieve_of_eratosthenes.cpp Added C++ program to find all the primes till the given number --- sieve_of_eratosthenes.cpp | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sieve_of_eratosthenes.cpp diff --git a/sieve_of_eratosthenes.cpp b/sieve_of_eratosthenes.cpp new file mode 100644 index 0000000..3288efb --- /dev/null +++ b/sieve_of_eratosthenes.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; + +void sieve (vector& isPrime) +{ + int upperLimit = isPrime.size() - 1; + + isPrime[0] = isPrime[1] = false; + for (int i = 2; i*i <= upperLimit; ++i) + { + if (isPrime[i]) + { + for (int j = i*i; j <= upperLimit; j += i) + { + isPrime[j] = false; + } + } + } +} + +int main() +{ + cout << "Enter the number till which you want to know all the primes: "; + int userInput; + cin >> userInput; + + vector isPrime (userInput + 1, true); + sieve (isPrime); + + cout << "All the primes till " << userInput << ": " << endl; + for (int currentNumber = 2; currentNumber <= userInput; ++currentNumber) + { + if (isPrime[currentNumber]) + { + cout << currentNumber << ' '; + } + } + cout << endl; + + return 0; +} \ No newline at end of file