Skip to content

feat: add binary search origin algorithm #1715

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

Closed
Closed
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
50 changes: 49 additions & 1 deletion Search/BinarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,52 @@ function binarySearchIterative(arr, x, low = 0, high = arr.length - 1) {
return -1
}

export { binarySearchIterative, binarySearchRecursive }
/* binary search for unsorted arrays, returns original index. */
function binarySearchOrigin(arr, target) {
// check if all elements in the array are of the same type
const firstType = typeof arr[0]
const allSameType = arr.every((item) => typeof item === firstType)

if (!allSameType) {
return 'Cannot perform search: Array contains elements of different types.'
}

const originalArrayWithIndices = arr.map((value, index) => ({
value,
index
}))

// sorting function based on type (number or string)
const sortedArrayWithIndices = originalArrayWithIndices.sort((a, b) => {
if (typeof a.value === 'number' && typeof b.value === 'number') {
return a.value - b.value // sort numbers
} else if (typeof a.value === 'string' && typeof b.value === 'string') {
return a.value.localeCompare(b.value) // sort strings
}
})

let start = 0
let end = sortedArrayWithIndices.length - 1

// binary search loop
while (start <= end) {
const midIndex = Math.floor((start + end) / 2)
const mid = sortedArrayWithIndices[midIndex].value

if (mid === target) {
// return the original index if the target is found
return sortedArrayWithIndices[midIndex].index
}

if (mid < target) {
start = midIndex + 1
} else {
end = midIndex - 1
}
}

// return -1 if target is not found
return -1
}

export { binarySearchIterative, binarySearchRecursive, binarySearchOrigin }
12 changes: 10 additions & 2 deletions Search/test/BinarySearch.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { binarySearchIterative, binarySearchRecursive } from '../BinarySearch'
import {
binarySearchIterative,
binarySearchRecursive,
binarySearchOrigin
} from '../BinarySearch'

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const stringArr = [
Expand Down Expand Up @@ -31,7 +35,11 @@ const stringArr = [
]

describe('Binary Search', () => {
const funcs = [binarySearchIterative, binarySearchRecursive]
const funcs = [
binarySearchIterative,
binarySearchRecursive,
binarySearchOrigin
]
for (const func of funcs) {
test('expect to return the index of the item in the array', () => {
expect(func(arr, 3)).toBe(2)
Expand Down