image.png
    image.png


    const checkboxes = Array.from(document.querySelectorAll(‘input[type=”checkbox”]’));
    checkboxes.forEach((checkbox) => {checkbox.addEventListener(‘click’, a)});
    function a(e) {
    if (e.shiftKey && this.checked) {

    // event 不同的话 e 的属性也是不同的哦


    image.png

    1. //选择排序 selection sorting
    2. function selectionSort(arr) {
    3. for (let i=0; i<arr.length; i++) {
    4. let indexofMin = i;
    5. for (let j=i+1; j<arr.length; j++) {
    6. if (arr[j] < arr[indexofMin]) {
    7. indexofMin = j;
    8. }
    9. }
    10. if (i !== indexofMin) {
    11. [arr[i], arr[indexofMin]] = [arr[indexofMin], arr[i]];
    12. }
    13. }
    14. return arr;
    15. }

    image.png

    1. // 并归排序
    2. function mergeSort(arr) {
    3. if (arr.length === 1) {
    4. return arr;
    5. }
    6. const center = Math.floor(arr.length / 2);
    7. const left = arr.slice(0, center);
    8. const right = arr.slice(center);
    9. return merge(mergeSort(left), mergeSort(right));
    10. }
    11. function merge(left, right) {
    12. const results = [];
    13. while (left.length && right.length) {
    14. if (left[0] < right[0]) {
    15. results.push(left.shift());
    16. } else {
    17. results.push(right.shift());
    18. }
    19. }
    20. return [...results, ...left, ...right];
    21. }