Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-sort.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // TODO: use something more complex like timsort?
  2. var floor = Math.floor;
  3. var mergeSort = function (array, comparefn) {
  4. var length = array.length;
  5. var middle = floor(length / 2);
  6. return length < 8 ? insertionSort(array, comparefn) : merge(
  7. mergeSort(array.slice(0, middle), comparefn),
  8. mergeSort(array.slice(middle), comparefn),
  9. comparefn
  10. );
  11. };
  12. var insertionSort = function (array, comparefn) {
  13. var length = array.length;
  14. var i = 1;
  15. var element, j;
  16. while (i < length) {
  17. j = i;
  18. element = array[i];
  19. while (j && comparefn(array[j - 1], element) > 0) {
  20. array[j] = array[--j];
  21. }
  22. if (j !== i++) array[j] = element;
  23. } return array;
  24. };
  25. var merge = function (left, right, comparefn) {
  26. var llength = left.length;
  27. var rlength = right.length;
  28. var lindex = 0;
  29. var rindex = 0;
  30. var result = [];
  31. while (lindex < llength || rindex < rlength) {
  32. if (lindex < llength && rindex < rlength) {
  33. result.push(comparefn(left[lindex], right[rindex]) <= 0 ? left[lindex++] : right[rindex++]);
  34. } else {
  35. result.push(lindex < llength ? left[lindex++] : right[rindex++]);
  36. }
  37. } return result;
  38. };
  39. module.exports = mergeSort;