MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / RandomizedSelect

Function RandomizedSelect

Data-Structures/Array/QuickSelect.js:22–32  ·  view source on GitHub ↗
(items, left, right, i)

Source from the content-addressed store, hash-verified

20}
21
22function RandomizedSelect(items, left, right, i) {
23 if (left === right) return items[left]
24
25 const pivotIndex = RandomizedPartition(items, left, right)
26 const k = pivotIndex - left + 1
27
28 if (i === k) return items[pivotIndex]
29 if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i)
30
31 return RandomizedSelect(items, pivotIndex + 1, right, i - k)
32}
33
34function RandomizedPartition(items, left, right) {
35 const rand = getRandomInt(left, right)

Callers 1

QuickSelectFunction · 0.85

Calls 1

RandomizedPartitionFunction · 0.85

Tested by

no test coverage detected