UNPKG

kepler.gl

Version:

kepler.gl is a webgl based application to visualize large scale location data in the browser

132 lines (127 loc) 12.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = quickInsertionSort; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); // SPDX-License-Identifier: MIT // Copyright contributors to the kepler.gl project /** * Inplace quick insertion sorting algorithm for numeric values * 1. using a stack to eliminate recursion * 2. sorting inplace to reduce memory usage * 3. using insertion sort for small partition sizes * The original source code is from: * https://www.measurethat.net/Benchmarks/Show/3549/0/javascript-sorting-algorithms * https://quick.work/?page=view-blog&id=24 * * @param arr */ function quickInsertionSort(arr) { if (!arr || arr.length < 1) { return; } var stack = []; stack.push([0, arr.length, 2 * Math.floor(Math.log(arr.length) / Math.log(2))]); while (stack.length > 0) { var _stack = (0, _slicedToArray2["default"])(stack[stack.length - 1], 3), start = _stack[0], end = _stack[1], depth = _stack[2]; stack.pop(); if (depth === 0) { // for worst case of quick sort: too many partitions shellSortBound(arr, start, end); } else { var pivot = Math.round((start + end) / 2); var pivotNewIndex = inplaceQuicksortPartition(arr, start, end, pivot); // more than 32 elements: faster to be sorted in QuickSort partition // less than 32 elements: faster to be sorted in InsertionSort if (end - pivotNewIndex > 16) { stack.push([pivotNewIndex, end, depth - 1]); } if (pivotNewIndex - start > 16) { stack.push([start, pivotNewIndex, depth - 1]); } } } insertionSort(arr); } /** * shellsort is a generalization of insertion sort * shellsort perform best on partially sorted array * Don't use shellsort on array (>10k) * @param arr * @param start * @param end * * Learn more about Shellsort at https://en.wikipedia.org/wiki/Shellsort */ function shellSortBound(arr, start, end) { if (arr.length <= 1) return; var inc = Math.round((start + end) / 2); var i; var j; var t; while (inc > start) { for (i = inc; i < end; i++) { t = arr[i]; j = i; while (j >= inc && arr[j - inc] > t) { arr[j] = arr[j - inc]; j -= inc; } arr[j] = t; } inc = Math.round((inc - start) / 2.2 + start); } } /** * Insertion sort * @param arr */ function insertionSort(arr) { for (var i = 1, l = arr.length; i < l; i++) { var value = arr[i]; var j = void 0; for (j = i - 1; j >= 0; j--) { if (arr[j] <= value) break; arr[j + 1] = arr[j]; } arr[j + 1] = value; } } /** * In-place quick sort * @param arr * @param start * @param end * @param pivotIndex * @returns number */ function inplaceQuicksortPartition(arr, start, end, pivotIndex) { var i = start; var j = end; var pivot = arr[pivotIndex]; var partition = true; while (partition) { while (arr[i] < pivot) { i++; } j--; while (pivot < arr[j]) { j--; } if (!(i < j)) { return i; } // swap(arr, i, j); var t = arr[i]; arr[i] = arr[j]; arr[j] = t; i++; } return i; } //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJxdWlja0luc2VydGlvblNvcnQiLCJhcnIiLCJsZW5ndGgiLCJzdGFjayIsInB1c2giLCJNYXRoIiwiZmxvb3IiLCJsb2ciLCJfc3RhY2siLCJfc2xpY2VkVG9BcnJheTIiLCJzdGFydCIsImVuZCIsImRlcHRoIiwicG9wIiwic2hlbGxTb3J0Qm91bmQiLCJwaXZvdCIsInJvdW5kIiwicGl2b3ROZXdJbmRleCIsImlucGxhY2VRdWlja3NvcnRQYXJ0aXRpb24iLCJpbnNlcnRpb25Tb3J0IiwiaW5jIiwiaSIsImoiLCJ0IiwibCIsInZhbHVlIiwicGl2b3RJbmRleCIsInBhcnRpdGlvbiJdLCJzb3VyY2VzIjpbIi4uL3NyYy9xdWljay1pbnNlcnRpb24tc29ydC50cyJdLCJzb3VyY2VzQ29udGVudCI6WyIvLyBTUERYLUxpY2Vuc2UtSWRlbnRpZmllcjogTUlUXG4vLyBDb3B5cmlnaHQgY29udHJpYnV0b3JzIHRvIHRoZSBrZXBsZXIuZ2wgcHJvamVjdFxuXG4vKipcbiAqIElucGxhY2UgcXVpY2sgaW5zZXJ0aW9uIHNvcnRpbmcgYWxnb3JpdGhtIGZvciBudW1lcmljIHZhbHVlc1xuICogMS4gdXNp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