问题
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 update their position as they are being sorted. I'm not sure if there is an easy way to do this with the way my code is currently written or if I need to break the sorting function up and re draw it after each stage?
var values = [];
var numLines = 500;
function setup() {
createCanvas(900, 600);
colorMode(HSB, height);
for (i = 0; i < numLines; i++) {
values[i] = (round(random(height)));
}
values = mergeSort(values);
noLoop();
}
function draw() {
background(51);
for (let 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);
}
}
function mergeSort(a) {
if (a.length <= 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), mergeSort(right));
}
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>
回答1:
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>
回答2:
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>
来源:https://stackoverflow.com/questions/55288158/javascript-merge-sort-visualisation