合并排序JavaScript实现

时间:2018-02-22 07:39:14

标签: javascript algorithm mergesort

我正在开设关于算法的可汗学院的课程 我经常试着自己弄清楚这些例子,但这次我真的没有得到它。练习位于:Khan Academy Merge Sort Exercise 因此,我要求善良的人为我解决这个问题,因为我已经陷入困境,这是我第一次无法实现某些目标。我确信我会理解这个算法(我想我会这样做,但是在看似解决方案后,它给我的实施错误,显然我不理解)。
这是我到目前为止所做的代码:

// Takes in an array that has two sorted subarrays,
//  from [p..q] and [q+1..r], and merges the array
var merge = function(array, p, q, r) {
    // This code has been purposefully obfuscated,
    //  as you'll write it yourself in next challenge.
    var a=[],b=[],c=p,d,e;for(d=0;c<=q;d++,c++){a[d]=array[c];}for(e=0;c<=r;e++,c++){b[e]=array[c];}c=p;for(e=d=0;d<a.length&&e<b.length;){if(a[d]<b[e]){array[c]=a[d];d++;} else {array[c]=b[e]; e++;}c++; }for(;d<a.length;){array[c]=a[d];d++;c++;}for(;e<b.length;){array[c]=b[e];e++;c++;}
};


// Takes in an array and recursively merge sorts it
var mergeSort = function(array, p, r) {
    if(r > 1) {
        var q = Math.floor((p + r) / 2);
        mergeSort(array,p,q);
        mergeSort(array,q+1,r);
        merge(array, p, q, r);
    }

};

var array = [14, 7, 3, 12, 9, 11, 6, 2];
console.log(''+array);
mergeSort(array, 0, array.length-1);
console.log("Array after sorting: " + array);

&#13;
&#13;
// Takes in an array that has two sorted subarrays,
    //  from [p..q] and [q+1..r], and merges the array
    var merge = function(array, p, q, r) {
        // This code has been purposefully obfuscated,
        //  as you'll write it yourself in next challenge.
        var a=[],b=[],c=p,d,e;for(d=0;c<=q;d++,c++){a[d]=array[c];}for(e=0;c<=r;e++,c++){b[e]=array[c];}c=p;for(e=d=0;d<a.length&&e<b.length;){if(a[d]<b[e]){array[c]=a[d];d++;} else {array[c]=b[e]; e++;}c++; }for(;d<a.length;){array[c]=a[d];d++;c++;}for(;e<b.length;){array[c]=b[e];e++;c++;}
    };
    
    
    // Takes in an array and recursively merge sorts it
    var mergeSort = function(array, p, r) {
        if(r > 1) {
            var q = Math.floor((p + r) / 2);
            mergeSort(array,p,q);
            mergeSort(array,q+1,r);
            merge(array, p, q, r);
        }
        
    };
    
    var array = [14, 7, 3, 12, 9, 11, 6, 2];
    console.log(''+array);
    mergeSort(array, 0, array.length-1);
    console.log("Array after sorting: " + array);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

它与if条件

有关

if条件实现的是检查数组末尾的r是否大于p数组的开始。

我不确定r > 1正在尝试做什么。因此它不会得到满足并递归执行,直到它耗尽堆栈空间

// Takes in an array that has two sorted subarrays,
//  from [p..q] and [q+1..r], and merges the array
var merge = function(array, p, q, r) {
  // This code has been purposefully obfuscated,
  //  as you'll write it yourself in next challenge.
  var a = [],
    b = [],
    c = p,
    d, e;
  for (d = 0; c <= q; d++, c++) {
    a[d] = array[c];
  }
  for (e = 0; c <= r; e++, c++) {
    b[e] = array[c];
  }
  c = p;
  for (e = d = 0; d < a.length && e < b.length;) {
    if (a[d] < b[e]) {
      array[c] = a[d];
      d++;
    } else {
      array[c] = b[e];
      e++;
    }
    c++;
  }
  for (; d < a.length;) {
    array[c] = a[d];
    d++;
    c++;
  }
  for (; e < b.length;) {
    array[c] = b[e];
    e++;
    c++;
  }
};


// Takes in an array and recursively merge sorts it
var mergeSort = function(array, p, r) {
  if (r > p) {
    var q = Math.floor((p + r) / 2);
    mergeSort(array, p, q);
    mergeSort(array, q + 1, r);
    merge(array, p, q, r);
  }

};

var array = [14, 7, 3, 12, 9, 11, 6, 2];
console.log('' + array);
mergeSort(array, 0, array.length - 1);
console.log("Array after sorting: " + array);