Javascript merge sort visualisation

前端 未结 2 453
自闭症患者
自闭症患者 2021-01-24 08:24

I have managed to get a merge sort working in p5.js to sort different length lines but can not figure out how to actually show them being sorted. I.e show them unsorted and then

相关标签:
2条回答
  • 2021-01-24 08:52

    This answer uses a non-recursive merge sort that keeps a history of the stages of the sort. The entire sort is performed before draw and then draw steps through all the stages so that we can see how the algorithm moves the lines to achieve the sort. The code is adapted from Mike Bostock's Visualizing Algorithms.

    https://bost.ocks.org/mike/algorithms/ https://bl.ocks.org/mbostock/1b5450d525babd28425f

    var values = [];
    var numLines = 500;
    var sortHist = [];
    function setup() {
      createCanvas(900, 600);
      colorMode(HSB, height);
      for (i = 0; i < numLines; i++) {
        values[i] =random(height);
      }
      sortHist = mergeSort(values);
      frameRate(1);
    }
      
    var historyIndex = 0;
    function draw() {
      background(51);
      for (i = 0; i < sortHist[historyIndex].length; i++) {
        let col = color(sortHist[historyIndex][i], height, height);
        stroke(col);
        fill(col);
        var location = map(i, 0, sortHist[historyIndex].length, 0, width);
        rect(location, height - sortHist[historyIndex][i], width/numLines, height);
      } 
      historyIndex++;
      if (historyIndex > sortHist.length -1){
        noLoop();
      }
    }
    
    function mergeSort(array) {
      var arrays = [array.slice()],
      n = array.length,
      array0 = array,
      array1 = new Array(n);
    
      for (var m = 1; m < n; m <<= 1) {
        for (var i = 0; i < n; i += (m << 1)) {
          merge(i, Math.min(i + m, n), Math.min(i + (m << 1), n));
        }
        arrays.push(array1.slice());
        array = array0, array0 = array1, array1 = array;
      }
    
    function merge(left, right, end) {
      for (var i0 = left, i1 = right, j = left; j < end; ++j) {
        array1[j] = array0[i0 < right && (i1 >= end || array0[i0] <=    array0[i1]) ? i0++ : i1++];
       }
     }
     return arrays;
    }  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>

    0 讨论(0)
  • 2021-01-24 08:53

    In order to visualize the sort we need to draw at intervals during the sort. Here I have added a depth variable so that I can control how far the sort goes. Each time draw is called I increment depth so we can see progress.

    var values = [];
    var numLines = 500;
    
    function setup() {
      createCanvas(900, 600);
      colorMode(HSB, height);
      for (i = 0; i < numLines; i++) {
        values[i] = (round(random(height)));
      }
      frameRate(1);
     }
    
    var depth = 1;
    function draw() {
      background(51);
      values = mergeSort(values, depth);
      depth++;
      for (i = 0; i < values.length; i++) {
        let col = color(values[i], height, height);
        stroke(col);
        fill(col);
        var location = map(i, 0, values.length, 0, width);
        rect(location, height - values[i], width/numLines, height);
      } 
      if (depth > 10){
       noLoop();
      }
    }
    
    function mergeSort(a, d) {
      if (a.length <= 1) {
        return a;
      }
      d--;
      if (d < 1){
        return a;
      }
      var mid = Math.round((a.length / 2));
      var left = a.slice(0, mid);
      var right = a.slice(mid);
      return merge(mergeSort(left,d), mergeSort(right, d));
    }
    
    function merge(left, right) {
      sorted = [];
      while (left && left.length > 0 && right && right.length > 0) {
        if (left[0] <= right[0]) {
          sorted.push(left.shift());
        }
        else {
          sorted.push(right.shift());
        }
      }
      return sorted.concat(left, right);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>

    0 讨论(0)
提交回复
热议问题