Skip to content

Quick Select Search #131

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

Merged
merged 6 commits into from
May 7, 2020
Merged
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
68 changes: 68 additions & 0 deletions QuickSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* QuickSelect is an algorithm to find the kth smallest number
*
* Notes:
* -QuickSelect is related to QuickSort, thus has optimal best and average
* case (O(n)) but unlikely poor worst case (O(n^2))
* -This implementation uses randomly selected pivots for better performance
*
* @complexity: O(n) (on average )
* @complexity: O(n^2) (worst case)
* @flow
*/

function QuickSelect (items, kth) {
return RandomizedSelect(items, 0, items.length - 1, kth)
}

function RandomizedSelect (
items,
left,
right,
i
) {
if (left === right) return items[left]

const pivotIndex = RandomizedPartition(items, left, right)
const k = pivotIndex - left + 1

if (i === k) return items[pivotIndex]
if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i)

return RandomizedSelect(items, pivotIndex + 1, right, i - k)
}

function RandomizedPartition (items, left, right) {
const rand = getRandomInt(left, right)
Swap(items, rand, right)
return Partition(items, left, right)
}

function Partition (items, left, right) {
const x = items[right]
let pivotIndex = left - 1

for (let j = left; j < right; j++) {
if (items[j] <= x) {
pivotIndex++
Swap(items, pivotIndex, j)
}
}

Swap(items, pivotIndex + 1, right)

return pivotIndex + 1
}

function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}

function Swap (arr, x, y) {
const temp = arr[x]
arr[x] = arr[y]
arr[y] = temp
}

// testing
console.log(QuickSelect([1, 4, 2, -2, 4, 5]))
48 changes: 48 additions & 0 deletions Sorts/Heapsort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let arrayLength = 0

/* to create MAX array */

function heapRoot (input, i) {
const left = 2 * i + 1
const right = 2 * i + 2
let max = i

if (left < arrayLength && input[left] > input[max]) {
max = left
}

if (right < arrayLength && input[right] > input[max]) {
max = right
}

if (max !== i) {
swap(input, i, max)
heapRoot(input, max)
}
}

function swap (input, indexA, indexB) {
const temp = input[indexA]

input[indexA] = input[indexB]
input[indexB] = temp
}

function heapSort (input) {
arrayLength = input.length

for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
heapRoot(input, i)
}

for (let i = input.length - 1; i > 0; i--) {
swap(input, 0, i)
arrayLength--

heapRoot(input, 0)
}
}

const arr = [3, 0, 2, 5, -1, 4, 1]
heapSort(arr)
console.log(arr)
45 changes: 45 additions & 0 deletions maths/ReverseString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* A short example showing how to reverse a string
* @flow
*/

/**
* Create a new string and append
* @complexity O(n)
*/

function ReverseStringIterative (string) {
let reversedString = ''
let index

for (index = string.length - 1; index >= 0; index--) {
reversedString += string[index]
}

return reversedString
}

/**
* JS disallows string mutation so we're actually a bit slower.
*
* @complexity: O(n)
*
* 'some' -> 'eoms' -> 'emos'
*/

function ReverseStringIterativeInplace (string) {
const _string = string.split('')

for (let i = 0; i < Math.floor(_string.length / 2); i++) {
const first = _string[i]
const second = _string[_string.length - 1 - i]
_string[i] = second
_string[_string.length - 1 - i] = first
}

return _string.join('')
}

// testing
console.log(ReverseStringIterative('Javascript'))
console.log(ReverseStringIterativeInplace('Javascript'))