|
1 |
| -/* |
| 1 | +/** |
2 | 2 | * Exponential Search
|
3 | 3 | *
|
4 |
| - * The algorithm consists of two stages. The first stage determines a |
5 |
| - * range in which the search key would reside if it were in the list. |
| 4 | + * The algorithm consists of two stages. The first stage determines a |
| 5 | + * range in which the search key would reside if it were in the list. |
6 | 6 | * In the second stage, a binary search is performed on this range.
|
7 |
| - * |
8 | 7 | *
|
9 |
| - * |
| 8 | + * |
| 9 | + * |
10 | 10 | */
|
11 | 11 |
|
12 |
| -function binarySearch(arr, x, floor, ceiling) { |
13 |
| - // Middle index |
14 |
| - let mid = Math.floor((floor + ceiling) / 2); |
15 |
| - |
16 |
| - // If value is at the mid position return this position |
17 |
| - if (arr[mid] === x) { |
18 |
| - return mid; |
19 |
| - } |
20 |
| - |
21 |
| - if(floor > ceiling) return -1; |
22 |
| - |
23 |
| - // If the middle element is great than the value |
24 |
| - // search the left part of the array |
25 |
| - if (arr[mid] > value) { |
26 |
| - return binarySearch(arr, value, floor, mid - 1); |
27 |
| - //If the middle element is lower than the value |
28 |
| - //search the right part of the array |
29 |
| - } else { |
30 |
| - return binarySearch(arr, value, mid + 1, ceiling); |
31 |
| - } |
32 |
| - |
33 |
| - |
| 12 | +function binarySearch (arr, x, floor, ceiling) { |
| 13 | + // Middle index |
| 14 | + const mid = Math.floor((floor + ceiling) / 2) |
| 15 | + |
| 16 | + // If value is at the mid position return this position |
| 17 | + if (arr[mid] === x) { |
| 18 | + return mid |
| 19 | + } |
| 20 | + |
| 21 | + if (floor > ceiling) return -1 |
| 22 | + |
| 23 | + // If the middle element is great than the value |
| 24 | + // search the left part of the array |
| 25 | + if (arr[mid] > value) { |
| 26 | + return binarySearch(arr, value, floor, mid - 1) |
| 27 | + // If the middle element is lower than the value |
| 28 | + // search the right part of the array |
| 29 | + } else { |
| 30 | + return binarySearch(arr, value, mid + 1, ceiling) |
| 31 | + } |
34 | 32 | }
|
35 | 33 |
|
| 34 | +function exponentialSearch (arr, length, value) { |
| 35 | + // If value is the first element of the array return this position |
| 36 | + if (arr[0] === value) { |
| 37 | + return 0 |
| 38 | + } |
36 | 39 |
|
37 |
| -function exponentialSearch(arr, length, value) { |
38 |
| - // If value is the first element of the array return this position |
39 |
| - if (arr[0] == value) { |
40 |
| - return 0; |
41 |
| - } |
42 |
| - |
43 |
| - // Find range for binary search |
44 |
| - let i = 1; |
45 |
| - while (i < length && arr[i] <= value) { |
46 |
| - i = i * 2; |
47 |
| - } |
| 40 | + // Find range for binary search |
| 41 | + let i = 1 |
| 42 | + while (i < length && arr[i] <= value) { |
| 43 | + i = i * 2 |
| 44 | + } |
48 | 45 |
|
49 |
| - // Call binary search for the range found above |
50 |
| - return binarySearch(arr, value, i / 2, Math.min(i, length)); |
| 46 | + // Call binary search for the range found above |
| 47 | + return binarySearch(arr, value, i / 2, Math.min(i, length)) |
51 | 48 | }
|
52 | 49 |
|
53 |
| -let arr = [2, 3, 4, 10, 40, 65 , 78 , 100]; |
54 |
| -let value = 78; |
55 |
| -let result = exponentialSearch(arr, arr.length, value); |
| 50 | +const arr = [2, 3, 4, 10, 40, 65, 78, 100] |
| 51 | +const value = 78 |
| 52 | +const result = exponentialSearch(arr, arr.length, value) |
56 | 53 |
|
57 | 54 | if (result < 0) {
|
58 |
| - console.log("Element not found"); |
| 55 | + console.log('Element not found') |
59 | 56 | } else {
|
60 |
| - console.log("Element found at position :" + result); |
| 57 | + console.log('Element found at position :' + result) |
61 | 58 | }
|
0 commit comments