quickSelect.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. function defaultCompareFunc(a, b) {
  41. return a - b;
  42. }
  43. function swapElement(arr, idx0, idx1) {
  44. var tmp = arr[idx0];
  45. arr[idx0] = arr[idx1];
  46. arr[idx1] = tmp;
  47. }
  48. function select(arr, left, right, nth, compareFunc) {
  49. var pivotIdx = left;
  50. var pivotValue;
  51. while (right > left) {
  52. pivotIdx = Math.round((right + left) / 2);
  53. pivotValue = arr[pivotIdx]; // Swap pivot to the end
  54. swapElement(arr, pivotIdx, right);
  55. pivotIdx = left;
  56. for (var i = left; i <= right - 1; i++) {
  57. if (compareFunc(pivotValue, arr[i]) >= 0) {
  58. swapElement(arr, i, pivotIdx);
  59. pivotIdx++;
  60. }
  61. }
  62. swapElement(arr, right, pivotIdx);
  63. if (pivotIdx === nth) {
  64. return pivotIdx;
  65. } else if (pivotIdx < nth) {
  66. left = pivotIdx + 1;
  67. } else {
  68. right = pivotIdx - 1;
  69. }
  70. } // Left == right
  71. return left;
  72. }
  73. function quickSelect(arr, left, right, nth, compareFunc) {
  74. if (arguments.length <= 3) {
  75. nth = left;
  76. if (arguments.length === 2) {
  77. compareFunc = defaultCompareFunc;
  78. } else {
  79. compareFunc = right;
  80. }
  81. left = 0;
  82. right = arr.length - 1;
  83. }
  84. return select(arr, left, right, nth, compareFunc);
  85. }
  86. export default quickSelect;