Skip to content

Commit 74f2965

Browse files
committed
Search/Sorts algoruthms : remove live code & console.log, leave examples as comments.
1 parent 8a7be96 commit 74f2965

22 files changed

+122
-198
lines changed

Search/BinarySearch.js

+37-36
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,43 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
4949
return -1
5050
}
5151

52-
/* ---------------------------------- Test ---------------------------------- */
52+
export { binarySearchIterative, binarySearchRecursive}
5353

54-
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
55-
const stringArr = [
56-
'Alpha',
57-
'Bravo',
58-
'Charlie',
59-
'Delta',
60-
'Echo',
61-
'Foxtrot',
62-
'Golf',
63-
'Hotel',
64-
'India',
65-
'Juliet',
66-
'Kilo',
67-
'Lima',
68-
'Mike',
69-
'November',
70-
'Oscar',
71-
'Papa',
72-
'Quebec',
73-
'Romeo',
74-
'Sierra',
75-
'Tango',
76-
'Uniform',
77-
'Victor',
78-
'Whiskey',
79-
'X-Ray',
80-
'Yankee',
81-
'Zulu'
82-
]
54+
/* ---------------------------------- Test ---------------------------------- */
8355

84-
console.log(binarySearchRecursive(arr, 3))
85-
console.log(binarySearchIterative(arr, 7))
86-
console.log(binarySearchRecursive(arr, 13))
56+
// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
57+
// const stringArr = [
58+
// 'Alpha',
59+
// 'Bravo',
60+
// 'Charlie',
61+
// 'Delta',
62+
// 'Echo',
63+
// 'Foxtrot',
64+
// 'Golf',
65+
// 'Hotel',
66+
// 'India',
67+
// 'Juliet',
68+
// 'Kilo',
69+
// 'Lima',
70+
// 'Mike',
71+
// 'November',
72+
// 'Oscar',
73+
// 'Papa',
74+
// 'Quebec',
75+
// 'Romeo',
76+
// 'Sierra',
77+
// 'Tango',
78+
// 'Uniform',
79+
// 'Victor',
80+
// 'Whiskey',
81+
// 'X-Ray',
82+
// 'Yankee',
83+
// 'Zulu'
84+
// ]
8785

88-
console.log(binarySearchIterative(stringArr, 'Charlie'))
89-
console.log(binarySearchRecursive(stringArr, 'Zulu'))
90-
console.log(binarySearchIterative(stringArr, 'Sierra'))
86+
// binarySearchRecursive(arr, 3)
87+
// binarySearchIterative(arr, 7)
88+
// binarySearchRecursive(arr, 13)
89+
// binarySearchIterative(stringArr, 'Charlie')
90+
// binarySearchRecursive(stringArr, 'Zulu')
91+
// binarySearchIterative(stringArr, 'Sierra')

Search/ExponentialSearch.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ function exponentialSearch (arr, length, value) {
4747
return binarySearch(arr, value, i / 2, Math.min(i, length))
4848
}
4949

50-
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
51-
const value = 78
52-
const result = exponentialSearch(arr, arr.length, value)
53-
54-
if (result < 0) {
55-
console.log('Element not found')
56-
} else {
57-
console.log('Element found at position :' + result)
58-
}
50+
export { binarySearch, exponentialSearch}
51+
52+
// const arr = [2, 3, 4, 10, 40, 65, 78, 100]
53+
// const value = 78
54+
// const result = exponentialSearch(arr, arr.length, value)

Search/FibonacciSearch.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* the item (number) to be searched for and the length of the items in the array
2020
****************************************************************************/
2121

22-
const fibonacciSearch = (arr, x, n) => {
22+
export const fibonacciSearch = (arr, x, n) => {
2323
let fib2 = 0 // (K-2)'th Fibonacci Number
2424
let fib1 = 1 // (K-1)'th Fibonacci Number.
2525
let fibK = fib2 + fib1 // Kth Fibonacci
@@ -69,9 +69,9 @@ const fibonacciSearch = (arr, x, n) => {
6969
// element not found. return -1
7070
return -1
7171
}
72+
7273
// Example
73-
const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
74-
const n = myArray.length
75-
const x = 90
76-
const fibFinder = fibonacciSearch(myArray, x, n)
77-
console.log('Element found at index:', fibFinder)
74+
// const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
75+
// const n = myArray.length
76+
// const x = 90
77+
// const fibFinder = fibonacciSearch(myArray, x, n)

Search/InterpolationSearch.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*/
1111

12-
function interpolationSearch (arr, key) {
12+
export function interpolationSearch (arr, key) {
1313
const length = arr.length - 1
1414
let low = 0
1515
let high = length
@@ -38,9 +38,9 @@ function interpolationSearch (arr, key) {
3838
return -1
3939
}
4040

41-
const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39]
41+
// const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39]
4242

43-
console.log('Found at position :' + interpolationSearch(arr, 2))
44-
console.log('Found at position :' + interpolationSearch(arr, 12))
45-
console.log('Found at position :' + interpolationSearch(arr, 1000))
46-
console.log('Found at position :' + interpolationSearch(arr, 39))
43+
// interpolationSearch(arr, 2)
44+
// interpolationSearch(arr, 12)
45+
// interpolationSearch(arr, 1000)
46+
// interpolationSearch(arr, 39)

Search/LinearSearch.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function Search (theArray, key) {
2121
return -1
2222
}
2323

24-
const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
25-
SearchArray(3, ar)
26-
SearchArray(4, ar)
27-
SearchArray(11, ar)
24+
export { SearchArray, Search }
25+
26+
// const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
27+
// SearchArray(3, ar)
28+
// SearchArray(4, ar)
29+
// SearchArray(11, ar)

Search/QuickSelectSearch.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
1313
*/
14-
function quickSelectSearch (array, k) {
14+
export function quickSelectSearch (array, k) {
1515
if (!array || array.length <= k) {
1616
throw new Error('Invalid arguments')
1717
}
@@ -49,7 +49,7 @@ function quickSelectSearch (array, k) {
4949

5050
/* ---------------------------------- Test ---------------------------------- */
5151

52-
const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110]
53-
console.log(quickSelectSearch(arr, 5)) // [ 19, 21, 28, 41, 5, 66, 333, 11110, 1121111, 7777 ]
54-
console.log(quickSelectSearch(arr, 2)) // [ 19, 5, 21, 41, 28, 333, 11110, 1121111, 7777, 66 ]
55-
console.log(quickSelectSearch(arr, 7)) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ]
52+
// const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110]
53+
// quickSelectSearch(arr, 5) // [ 19, 21, 28, 41, 5, 66, 333, 11110, 1121111, 7777 ]
54+
// quickSelectSearch(arr, 2) // [ 19, 5, 21, 41, 28, 333, 11110, 1121111, 7777, 66 ]
55+
// quickSelectSearch(arr, 7) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ]

Search/StringSearch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function makeTable (str) {
3535
}
3636

3737
// Find all the words that matches in a given string `str`
38-
function stringSearch (str, word) {
38+
export function stringSearch (str, word) {
3939
// find the prefix table in O(n)
4040
const prefixes = makeTable(word)
4141
const matches = []
@@ -80,4 +80,4 @@ function stringSearch (str, word) {
8080
return matches
8181
}
8282

83-
console.log(stringSearch('Hello search the position of me', 'pos'))
83+
// stringSearch('Hello search the position of me', 'pos')

Sorts/CountingSort.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Animated Visual: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html
99
*/
1010

11-
const countingSort = (arr, min, max) => {
11+
export const countingSort = (arr, min, max) => {
1212
// Create an auxiliary resultant array
1313
const res = []
1414
// Create and initialize the frequency[count] array
@@ -33,11 +33,5 @@ const countingSort = (arr, min, max) => {
3333
/**
3434
* Implementation of Counting Sort
3535
*/
36-
const array = [3, 0, 2, 5, 4, 1]
37-
// Before Sort
38-
console.log('\n- Before Sort | Implementation of Counting Sort -')
39-
console.log(array)
40-
// After Sort
41-
console.log('- After Sort | Implementation of Counting Sort -')
42-
console.log(countingSort(array, 0, 5))
43-
console.log('\n')
36+
// const array = [3, 0, 2, 5, 4, 1]
37+
// countingSort(array, 0, 5)

Sorts/FlashSort.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Wikipedia: https://en.wikipedia.org/wiki/Flashsort
77
*/
88

9-
function flashSort (arr) {
9+
export function flashSort (arr) {
1010
let max = 0; let min = arr[0]
1111
const n = arr.length
1212
const m = ~~(0.45 * n)
@@ -80,11 +80,5 @@ function flashSort (arr) {
8080
/**
8181
* Implementation of Flash Sort
8282
*/
83-
const array = [3, 0, 2, 5, -1, 4, 1, -2]
84-
// Before Sort
85-
console.log('\n- Before Sort | Implementation of Flash Sort -')
86-
console.log(array)
87-
// After Sort
88-
console.log('- After Sort | Implementation of Flash Sort -')
89-
console.log(flashSort(array))
90-
console.log('\n')
83+
// const array = [3, 0, 2, 5, -1, 4, 1, -2]
84+
// flashSort(array)

Sorts/GnomeSort.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* more information: https://en.wikipedia.org/wiki/Gnome_sort
44
*
55
*/
6-
function gnomeSort (items) {
6+
export function gnomeSort (items) {
77
if (items.length <= 1) {
88
return
99
}
@@ -23,9 +23,6 @@ function gnomeSort (items) {
2323

2424
// Implementation of gnomeSort
2525

26-
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
27-
// Array before Sort
28-
console.log(ar)
29-
gnomeSort(ar)
30-
// Array after sort
31-
console.log(ar)
26+
// const ar = [5, 6, 7, 8, 1, 2, 12, 14]
27+
// gnomeSort(ar)
28+

Sorts/HeapSort.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Array.prototype.heapify = function (index, heapSize) {
3333
* utilizing the heap property.
3434
* For more information see: https://en.wikipedia.org/wiki/Heapsort
3535
*/
36-
function heapSort (items) {
36+
export function heapSort (items) {
3737
const length = items.length
3838

3939
for (let i = Math.floor(length / 2) - 1; i > -1; i--) {
@@ -50,9 +50,5 @@ function heapSort (items) {
5050

5151
// Implementation of heapSort
5252

53-
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
54-
// Array before Sort
55-
console.log(ar)
56-
heapSort(ar)
57-
// Array after sort
58-
console.log(ar)
53+
// const ar = [5, 6, 7, 8, 1, 2, 12, 14]
54+
// heapSort(ar)

Sorts/HeapSortV2.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function swap (input, indexA, indexB) {
2525
[input[indexA], input[indexB]] = [input[indexB], input[indexA]]
2626
}
2727

28-
function heapSort (input) {
28+
export function heapSort (input) {
2929
arrayLength = input.length
3030

3131
for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
@@ -39,7 +39,3 @@ function heapSort (input) {
3939
heapRoot(input, 0)
4040
}
4141
}
42-
43-
const arr = [3, 0, 2, 5, -1, 4, 1]
44-
heapSort(arr)
45-
console.log(arr)

Sorts/InsertionSort.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* element one by one from unsorted part; insert into the sorted part at
55
* the correct position and expand sorted part one element at a time.
66
*/
7-
function insertionSort (unsortedList) {
7+
export function insertionSort (unsortedList) {
88
const len = unsortedList.length
99
for (let i = 1; i < len; i++) {
1010
let j
@@ -19,7 +19,3 @@ function insertionSort (unsortedList) {
1919
unsortedList[j + 1] = tmp
2020
}
2121
}
22-
23-
const arr = [5, 3, 1, 2, 4, 8, 3, 8]
24-
insertionSort(arr)
25-
console.log(arr)

Sorts/IntroSort.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ function introsort (array, compare) {
244244
/**
245245
* @example Demo run of the sort routine
246246
* The data is randomly generated
247-
* Prints RIGHT:) if the sort routine worked as expected
248-
* If not prints WRONG!!
247+
* Returns 'RIGHT:)' if the sort routine worked as expected,
248+
* 'WRONG!!' otherwise
249249
*/
250-
(function demo () {
250+
function demo1 () {
251251
const data = []
252252
const size = 1000000
253253
let i = 0
@@ -268,18 +268,18 @@ function introsort (array, compare) {
268268
}
269269
}
270270
if (faulty) {
271-
console.log('WRONG!!')
271+
return 'WRONG!!'
272272
} else {
273-
console.log('RIGHT:)')
273+
return 'RIGHT:)'
274274
}
275-
})();
275+
}
276276

277277
/**
278278
* @example Demo run of the sort routine
279279
* using the default compare function and
280280
* comparing the results with Array.sort
281281
*/
282-
(function demo () {
282+
function demo2 () {
283283
const data = []
284284
const data2 = []
285285
const size = 1000000
@@ -300,8 +300,10 @@ function introsort (array, compare) {
300300
}
301301
}
302302
if (faulty) {
303-
console.log('WRONG Implented Comparator!!')
303+
return 'WRONG Implented Comparator!!'
304304
} else {
305-
console.log('Comparator Works Fine:)')
305+
return 'Comparator Works Fine:)'
306306
}
307-
})()
307+
}
308+
309+
export { introsort, demo1, demo2 }

Sorts/OddEvenSort.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function swap (arr, i, j) {
1313
arr[j] = tmp
1414
}
1515

16-
function oddEvenSort (arr) {
16+
export function oddEvenSort (arr) {
1717
let sorted = false
1818
while (!sorted) {
1919
sorted = true
@@ -31,10 +31,3 @@ function oddEvenSort (arr) {
3131
}
3232
}
3333
}
34-
const testArray = [5, 6, 7, 8, 1, 2, 12, 14, 5, 3, 2, 2]
35-
36-
// Array before sort
37-
console.log(testArray)
38-
oddEvenSort(testArray)
39-
// Array after sort
40-
console.log(testArray)

0 commit comments

Comments
 (0)