Skip to content

Commit ce07a8b

Browse files
NourBcitsvinayak
andauthored
Quick Select Search (#131)
* HeapSort algorithm * Create QuickSelect.js * Algorithm to reverse a string. * Update ReverseString.js * Update Heapsort.js * Update QuickSelect.js Co-authored-by: vinayak <[email protected]>
1 parent a052808 commit ce07a8b

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

QuickSelect.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* QuickSelect is an algorithm to find the kth smallest number
3+
*
4+
* Notes:
5+
* -QuickSelect is related to QuickSort, thus has optimal best and average
6+
* case (O(n)) but unlikely poor worst case (O(n^2))
7+
* -This implementation uses randomly selected pivots for better performance
8+
*
9+
* @complexity: O(n) (on average )
10+
* @complexity: O(n^2) (worst case)
11+
* @flow
12+
*/
13+
14+
function QuickSelect (items, kth) {
15+
return RandomizedSelect(items, 0, items.length - 1, kth)
16+
}
17+
18+
function RandomizedSelect (
19+
items,
20+
left,
21+
right,
22+
i
23+
) {
24+
if (left === right) return items[left]
25+
26+
const pivotIndex = RandomizedPartition(items, left, right)
27+
const k = pivotIndex - left + 1
28+
29+
if (i === k) return items[pivotIndex]
30+
if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i)
31+
32+
return RandomizedSelect(items, pivotIndex + 1, right, i - k)
33+
}
34+
35+
function RandomizedPartition (items, left, right) {
36+
const rand = getRandomInt(left, right)
37+
Swap(items, rand, right)
38+
return Partition(items, left, right)
39+
}
40+
41+
function Partition (items, left, right) {
42+
const x = items[right]
43+
let pivotIndex = left - 1
44+
45+
for (let j = left; j < right; j++) {
46+
if (items[j] <= x) {
47+
pivotIndex++
48+
Swap(items, pivotIndex, j)
49+
}
50+
}
51+
52+
Swap(items, pivotIndex + 1, right)
53+
54+
return pivotIndex + 1
55+
}
56+
57+
function getRandomInt (min, max) {
58+
return Math.floor(Math.random() * (max - min + 1)) + min
59+
}
60+
61+
function Swap (arr, x, y) {
62+
const temp = arr[x]
63+
arr[x] = arr[y]
64+
arr[y] = temp
65+
}
66+
67+
// testing
68+
console.log(QuickSelect([1, 4, 2, -2, 4, 5]))

Sorts/Heapsort.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
let arrayLength = 0
2+
3+
/* to create MAX array */
4+
5+
function heapRoot (input, i) {
6+
const left = 2 * i + 1
7+
const right = 2 * i + 2
8+
let max = i
9+
10+
if (left < arrayLength && input[left] > input[max]) {
11+
max = left
12+
}
13+
14+
if (right < arrayLength && input[right] > input[max]) {
15+
max = right
16+
}
17+
18+
if (max !== i) {
19+
swap(input, i, max)
20+
heapRoot(input, max)
21+
}
22+
}
23+
24+
function swap (input, indexA, indexB) {
25+
const temp = input[indexA]
26+
27+
input[indexA] = input[indexB]
28+
input[indexB] = temp
29+
}
30+
31+
function heapSort (input) {
32+
arrayLength = input.length
33+
34+
for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
35+
heapRoot(input, i)
36+
}
37+
38+
for (let i = input.length - 1; i > 0; i--) {
39+
swap(input, 0, i)
40+
arrayLength--
41+
42+
heapRoot(input, 0)
43+
}
44+
}
45+
46+
const arr = [3, 0, 2, 5, -1, 4, 1]
47+
heapSort(arr)
48+
console.log(arr)

maths/ReverseString.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* A short example showing how to reverse a string
3+
* @flow
4+
*/
5+
6+
/**
7+
* Create a new string and append
8+
* @complexity O(n)
9+
*/
10+
11+
function ReverseStringIterative (string) {
12+
let reversedString = ''
13+
let index
14+
15+
for (index = string.length - 1; index >= 0; index--) {
16+
reversedString += string[index]
17+
}
18+
19+
return reversedString
20+
}
21+
22+
/**
23+
* JS disallows string mutation so we're actually a bit slower.
24+
*
25+
* @complexity: O(n)
26+
*
27+
* 'some' -> 'eoms' -> 'emos'
28+
*/
29+
30+
function ReverseStringIterativeInplace (string) {
31+
const _string = string.split('')
32+
33+
for (let i = 0; i < Math.floor(_string.length / 2); i++) {
34+
const first = _string[i]
35+
const second = _string[_string.length - 1 - i]
36+
_string[i] = second
37+
_string[_string.length - 1 - i] = first
38+
}
39+
40+
return _string.join('')
41+
}
42+
43+
// testing
44+
console.log(ReverseStringIterative('Javascript'))
45+
console.log(ReverseStringIterativeInplace('Javascript'))

0 commit comments

Comments
 (0)