forked from simranlotey/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchWithQuickSort.ts
51 lines (42 loc) · 1.14 KB
/
BinarySearchWithQuickSort.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid; // Found the target value at index 'mid'
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Target value not found in the array
}
function quicksort(arr: number[]): number[] {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[0];
const left = [];
const right = [];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [...quicksort(left), pivot, ...quicksort(right)];
}
/* Testcase
const unsortedArray = [13, 7, 3, 9, 5, 11, 1];
const sortedArray = quicksort(unsortedArray);
const targetValue = 7;
const result = binarySearch(sortedArray, targetValue);
if (result !== -1) {
console.log(`The target value ${targetValue} was found at index ${result}.`);
} else {
console.log(`The target value ${targetValue} was not found in the array.`);
}
*/