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

Function partition

javascript/0973-k-closest-points-to-origin.js:153–174  ·  view source on GitHub ↗
(points, left, right)

Source from the content-addressed store, hash-verified

151};
152
153var partition = function (points, left, right) {
154 let pivot = choosePivot(points, left, right);
155 let pivotDist = squaredDistance(pivot);
156 while (left < right) {
157 // Iterate through the range and swap elements to make sure
158 // that all points closer than the pivot are to the left
159 if (squaredDistance(points[left]) >= pivotDist) {
160 [points[left], points[right]] = [points[right], points[left]];
161 right--;
162 } else {
163 left++;
164 }
165 }
166
167 // Ensure the left pointer is just past the end of
168 // the left range then return it as the new pivotIndex
169 if (squaredDistance(points[left]) < pivotDist) {
170 left++;
171 }
172
173 return left;
174};
175
176// Choose a pivot element of the array
177const choosePivot = (points, left, right) =>

Callers 1

quickSelectFunction · 0.70

Calls 2

choosePivotFunction · 0.85
squaredDistanceFunction · 0.85

Tested by

no test coverage detected