programming-examples/js/Algorithms/3-way-string-quicksort.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
(function (exports) {
'use strict';
var quicksort = (function () {
function charAt(str, i) {
return (i < str.length) ? str.charCodeAt(i) : -1;
}
function swap(arr, i, j) {
var temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
function quicksort(arr, lo, hi, d) {
if (lo >= hi) {
return;
}
var lowPointer = lo;
var highPointer = hi;
var p = charAt(arr[lo], d);
var i = lo + 1;
var current;
while (i <= highPointer) {
current = charAt(arr[i], d);
if (current < p) {
swap(arr, i, lowPointer);
lowPointer += 1;
} else if (current > p) {
swap(arr, i, highPointer);
highPointer -= 1;
i += 1;
} else {
i += 1;
}
}
quicksort(arr, lo, lowPointer - 1, d);
if (p >= 0) {
quicksort(arr, lowPointer, highPointer, d + 1);
}
quicksort(arr, highPointer + 1, hi, d);
}
/**
* Effective inplace string sorting algorithm.
* Algorithm is NOT stable.
*
* @example
*
* var sort = require('path-to-algorithms/src/sorting'+
* '/3-way-string-quicksort').quicksort;
* console.log(sort(['bb', 'aa', 'cc'])); // [ 'aa', 'bb', 'cc' ]
*
* @public
* @module sorting/3-way-string-quicksort
* @param arr {Array} array which should be sorted.
* @return {Array} Sorted array.
*/
return function sort(arr) {
quicksort(arr, 0, arr.length - 1, 0);
return arr;
};
}());
exports.quicksort = quicksort;
})(typeof window === 'undefined' ? module.exports : window);