We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 830cffb commit 9b315edCopy full SHA for 9b315ed
1 file changed
code.js
@@ -403,6 +403,19 @@
403
// From: https://en.wikipedia.org/wiki/Merge_sort
404
function topDownSplitMerge(B, iBegin, iEnd, A) {
405
if (iEnd - iBegin <= 6) return;
406
+
407
+ // Optimization: Don't do merge sort if it's already sorted
408
+ let isAlreadySorted = true;
409
+ for (let i = iBegin + 3, j = i + 6; j < iEnd; i = j, j += 6) {
410
+ // Compare mappings first by original line (index 3) and then by original column (index 4)
411
+ if (A[i] < A[j] || (A[i] === A[j] && A[i + 1] <= A[j + 1])) continue;
412
+ isAlreadySorted = false;
413
+ break;
414
+ }
415
+ if (isAlreadySorted) {
416
+ return;
417
418
419
const iMiddle = ((iEnd / 6 + iBegin / 6) >> 1) * 6;
420
topDownSplitMerge(A, iBegin, iMiddle, B);
421
topDownSplitMerge(A, iMiddle, iEnd, B);
0 commit comments