1. //选择
    2. function selection(array) {
    3. for (let i = 0; i < array.length - 1; i++) {
    4. let minIndex = i;
    5. for (let j = i + 1; j < array.length; j++) {
    6. minIndex = array[j] < array[minIndex] ? j : minIndex;
    7. }
    8. swap(array, i, minIndex);
    9. }
    10. return array;
    11. }
    12. function insertion(array) {
    13. for (let i = 1; i < array.length; i++) {
    14. for (let j = i - 1; j >= 0 && array[j] > array[j + 1]; j--) {
    15. swap(array, j, j + 1)
    16. }
    17. }
    18. return array;
    19. }
    20. //冒泡排序
    21. function bubble(array) {
    22. for (let i = array.length - 1; i > 0; i--) {
    23. for (let j = 0; j < i; j++) {
    24. if (array[j] > array[j + 1]) swap(array, j, j + 1)
    25. }
    26. }
    27. }
    28. function selection(array) {
    29. for (let i = 0; i < array.length - 1; i++) {
    30. let minIndex = i;
    31. for (let j = i + 1; j < array.length; j++) {
    32. minIndex = array[j] < array[minIndex] ? j : minIndex;
    33. }
    34. swap(array, i, minIndex)
    35. }
    36. return array;
    37. }
    38. function insertion(array) {
    39. for (let i = 1; i < array.length; i++) {
    40. for (let j = i - 1; j >= 0 && array[j] > array[j + 1]; j--) {
    41. swap(array, j, j + 1)
    42. }
    43. }
    44. return array;
    45. }
    46. let create = function (o) {
    47. var F = function () { };
    48. F.prototype = o;
    49. return new F();
    50. }
    51. // 快排
    52. function quickSort(arr, i, j) {
    53. if (i < j) {
    54. let left = i;
    55. let right = j;
    56. let prvot = arr[letf];
    57. while (i < j) {
    58. while (arr[j] >= pivot && i < j) {
    59. j--;
    60. }
    61. if (i < j) {
    62. arr[i++] = arr[j];
    63. }
    64. while (arr[i] <= pivot && i < j) {
    65. i++;
    66. }
    67. if (i < j) {
    68. arr[j--] = arr[i]
    69. }
    70. }
    71. arr[i] = pivot;
    72. quickSort(arr, left, i - 1);
    73. quickSort(arr, i + 1, right);
    74. return arr;
    75. }
    76. }
    77. function quickSort(arr) {
    78. if (arr.length <= 1) {
    79. return arr;
    80. }
    81. let pivotIndex = Math.floor(arr.length / 2);
    82. let pivot = arr.slice(pivotIndex, 1)[0];
    83. let left = [];
    84. let right = [];
    85. arr.forEach(item => {
    86. if (item > pivot) {
    87. right.push(item)
    88. } else {
    89. left.push(item)
    90. }
    91. })
    92. return quickSort(left).concat([pivot], quickSort(right));
    93. }