8000 Added Binary and Linear search in javascript by Gomatii · Pull Request #686 · dheeraj-2000/dsalgo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added Binary and Linear search in javascript #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Searching algos/Binary_Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class BinarySearch {
static iterativeBinarySearch(array, valueToSearch) {
let low = 0;
let high = array.length - 1;

while (low <= high) {
const middle = Math.floor((low + high) / 2);

if (array[middle] < valueToSearch) {
low = middle + 1;
} else if (array[middle] > valueToSearch) {
high = middle - 1;
} else {
return middle; // Value found at index `middle`
}
}
return -1; // Value not found
}

static recursiveBinarySearch(array, valueToSearch, low, high) {
if (high < low) return -1; // Base case: value not found

const middle = Math.floor((low + high) / 2);

if (array[middle] === valueToSearch) {
return middle; // Value found at index `middle`
} else if (array[middle] > valueToSearch) {
return this.recursiveBinarySearch(array, valueToSearch, low, middle - 1); // Search left
} else {
return this.recursiveBinarySearch(array, valueToSearch, middle + 1, high); // Search right
}
}
}

// Example usage:
const arrayToSearch = [1, 4, 5, 6, 7, 10, 14, 15, 21, 33, 34];
console.log(BinarySearch.iterativeBinarySearch(arrayToSearch, 6)); // Output: 3
console.log(BinarySearch.recursiveBinarySearch(arrayToSearch, 15, 0, arrayToSearch.length - 1)); // Output: 7

21 changes: 21 additions & 0 deletions Searching algos/Linear_Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class LinearSearch {
static linear(array, key) {
for (let index = 0; index < array.length; index++) {
if (array[index] === key) {
return index;
}
}
return -1;
}
}

// Example usage:
const array = [5, 6, 12, 4, 56, 87, 12];
const key = 56;
const index = LinearSearch.linear(array, key);

if (index === -1) {
console.log("Element not found.");
} else {
console.log("Element found at index " + index);
}
84 changes: 42 additions & 42 deletions Searching algos/binarySearch.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
// Java implementation of iterative Binary Search
class BinarySearch {
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at "
+ "index " + result);
}
// Java implementation of iterative Binary Search
class BinarySearch {
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid
if (arr[m] == x)
return m;

// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}

// if we reach here, then element was
// not present
return -1;
}

// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at "
+ "index " + result);
}
}
0