MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / quickSelect

Function quickSelect

javascript/0973-k-closest-points-to-origin.js:134–151  ·  view source on GitHub ↗
(points, k)

Source from the content-addressed store, hash-verified

132};
133
134var quickSelect = function (points, k) {
135 let left = 0,
136 right = points.length - 1;
137 let pivotIndex = points.length;
138 while (pivotIndex !== k) {
139 // Repeatedly partition the array
140 // while narrowing in on the kth element
141 pivotIndex = partition(points, left, right);
142 if (pivotIndex < k) {
143 left = pivotIndex;
144 } else {
145 right = pivotIndex - 1;
146 }
147 }
148
149 // Return the first k elements of the partially sorted array
150 return points.slice(0, k);
151};
152
153var partition = function (points, left, right) {
154 let pivot = choosePivot(points, left, right);

Callers 1

kClosestFunction · 0.70

Calls 1

partitionFunction · 0.70

Tested by

no test coverage detected