Public
Edited
May 16, 2023
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
naive_two_val_flip = {
var input, goal, matches, finished, og_idx, actions, hash, start;
[input, goal, matches, finished, og_idx, actions, hash] = setup(2)
yield* pauser(two_display(input, { goal: goal, immediate: true }));
start = new Date()
for (let i = 0; i < input.length; i++) {
for (let j = i+1; j < input.length; j++) {
if (input[i] + input[j] == goal) {
var match = {left:i, right:j}
matches.push(match)
actions.push(true)
}
yield two_display(input, { goal: goal, finished: finished, left: i, right: j, matches: matches});
actions.push(false);
}
finished.push(i)
actions.push(false)
}

yield two_display(input, {goal: goal, finished: finished, matches: matches});
output_data("naive_direct", 2, actions, matches, start)
}
Insert cell
Insert cell
Insert cell
sort_and_narrow = {
var input, goal, matches, finished, og_idx, actions, hash, start;
[input, goal, matches, finished, og_idx, actions, hash] = setup(2)
yield* pauser(two_display(input, { goal: goal, immediate: true }));
start = new Date()

let d1 = new Date()
for (let i = 0; i < input.length; i++) {
let min = i;
for (let j = i + 1; j < input.length; j++) {
actions.push(false)
if (input[j] < input[min]) {
min = j;
}
yield two_display(input, {goal: goal, left: i, right: j, extra: min, og_idx: og_idx });
}
if (min !== i) {
swap(i, min, input, og_idx)
actions.push(false)
yield two_display(input, {goal: goal, left: i, right: min, og_idx: og_idx });
}
actions.push(false)
}

let i = 0;
let j = input.length-1
yield two_display(input, {goal: goal, left:i, right:j, og_idx: og_idx });
while (i < j) {
if (input[i] + input[j] > goal) {
finished.push(j)
actions.push(false)
j = j - 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
} else if (input[i] + input[j] < goal) {
finished.push(i)
actions.push(false)
i = i + 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });

} else {
let left_val = input[i]
let right_val = input[j]
while (input[i] == left_val) {
let count = 0;
while (input[j-count] == right_val && j-count > i) {
var match = {left:i, right:j-count}
matches.push(match)
actions.push(true)
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
count = count + 1
}
finished.push(i)
actions.push(false)
i = i + 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
}
right_val = input[j]
while (input[j] == right_val) {
finished.push(j)
actions.push(false)
j = j - 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j, finished: finished, og_idx: og_idx });
}
}
}

actions.push(false)
finished.push(i)
yield two_display(input, {goal: goal, matches:matches, finished: finished, og_idx: og_idx});
output_data("naive_sort", 2, actions, matches, start)
}
Insert cell
Insert cell
Insert cell
sort_and_narrow_2 = {
var input, goal, matches, finished, og_idx, actions, hash, start;
[input, goal, matches, finished, og_idx, actions, hash] = setup(2)
yield* pauser(two_display(input, { goal: goal, immediate: true }));
start = new Date()
for (let j = 1; j < input.length; j++) {
let i = j - 1
yield two_display(input, {goal: goal, left: i, right: j, matches: matches });
while(i >= 0 && input[i] > input[i + 1]) {
swap(i,i+1, input, og_idx)
actions.push(false)
yield two_display(input, {goal: goal, left: i, right: i + 1, matches: matches});
i = i - 1
}
actions.push(false)
}

let i = 0;
let j = input.length-1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
while (i < j) {
if (input[i] + input[j] > goal) {
finished.push(j)
actions.push(false)
j = j - 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
} else if (input[i] + input[j] < goal) {
finished.push(i)
actions.push(false)
i = i + 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });

} else {
let left_val = input[i]
let right_val = input[j]
while (input[i] == left_val) {
let count = 0;
while (input[j-count] == right_val && j-count > i) {
var match = {left:i, right:j-count}
matches.push(match)
actions.push(true)
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
count = count + 1
}
finished.push(i)
actions.push(false)
i = i + 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
}
right_val = input[j]
while (input[j] == right_val) {
finished.push(j)
actions.push(false)
j = j - 1
yield two_display(input, {goal: goal, matches:matches, left:i, right:j,
finished: finished, og_idx: og_idx });
}
actions.push(false)
}
}
actions.push(false)
finished.push(i)
yield two_display(input, {goal: goal, matches:matches, finished: finished, og_idx: og_idx});
output_data("better_sort", 2, actions, matches, start)
}
Insert cell
Insert cell
Insert cell
hash_and_match = {
var input, goal, matches, finished, og_idx, actions, hash, start;
[input, goal, matches, finished, og_idx, actions, hash] = setup(2)
yield* pauser(two_display(input, { goal: goal, immediate: true }));
start = new Date()
for (let i = 0; i < input.length; i++) {
if (hash.get(input[i]) != undefined)
hash.set(input[i], hash.get(input[i])+1)
else
hash.set(input[i], 1)
yield two_display(input, { goal: goal, immediate: true, left: i, finished: finished, matches: matches, hash: hash });
actions.push(false);
}

let i = 0
let doubles = 1
for (let i = 0; i < input.length; i++) {
yield two_display(input, {goal: goal, matches:matches, left:i, finished: finished, hash: hash, });
let desired = goal - input[i]
let desired_matches = hash.get(desired)
if (desired_matches && desired >= input[i]) {
let j
if (desired == input[i]) {
j = doubles
doubles += 1
} else
j = 0
while (j < desired_matches) {
var match = {left:i, hash_val:desired}
matches.push(match)
actions.push(true)
j += 1
// yield two_display(input, {goal: goal, matches:matches, left:i, finished: finished, hash: hash});
}
}
finished.push(i)
actions.push(false);
}

yield two_display(input, {goal: goal, matches:matches, finished: finished, hash: hash });
output_data("hash_match", 2, actions, matches, start)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// function show_bars(dataset, {finished, left, right, immediate} = {} ) {
function two_display(dataset, {goal, left, right, matches = [], finished = [], hash = [], og_idx = [], extra, immediate}) {
let height = height_slider
let buffer_1 = 0
let buffer_2 = 8
let buffer_h1 = 30

let arr_width = width*(dataset.length)/(dataset.length+2+matches.length) - buffer_1 - buffer_2
// let arr_height = height / 2
let xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
// .rangeRound([width-arr_width, width])
.range([0, arr_width])
// .padding(.05);
.padding(.25/Math.sqrt(dataset.length));


let yScale = d3.scaleLinear()
.domain([0, Math.max(d3.max(dataset), goal/2)])
.range([0, height/2-buffer_h1]);
let svg = d3.select(DOM.svg(width, height));

svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("fill", "#dbefff");

// array bars
svg.selectAll("bars")
.data(dataset)
.enter().append("rect")
.attr("x", (d, i) => xScale(i))
.attr("width", xScale.bandwidth())
.attr("y", d => height - yScale(d))
.attr("height", d => yScale(d))
.attr("fill", (d, i) => { //
if (finished && finished.includes(i)) {return '#233b3b'} //
else if (i == left || i == right || i == extra) {return 'lime'} //
else {return 'steelblue'} //
;})
.attr("stroke", "black")
.attr("stroke-width", "1");

// hash bars
svg.selectAll("hash")
.data(hash.keys())
.enter().append("rect")
.attr("x", (d, i) => xScale(i))
.attr("width", xScale.bandwidth())
.attr("y", d => 0)
.attr("height", d => yScale(d))
.attr("fill", (d, i) => { //
if (goal - d == dataset[left]) {return 'lime'} //
else {return 'wheat'} //
;})
.attr("stroke", "black")
.attr("stroke-width", "1");


// matches
svg.selectAll("matches_l")
.data(matches)
.enter().append("rect")
.attr("x", (d, i) => arr_width + buffer_2 + i*xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", d => {
if (d.length > 1 || !("hash_val" in d)) {return height - yScale(dataset[d.left]);}
else {return height - yScale(d.hash_val)}
})
.attr("height", d => {
if (d.length > 1 || !("hash_val" in d)) {return yScale(dataset[d.left]);}
else {return yScale(d.hash_val);}
})
.attr("fill", '#710000')
.attr("stroke", "black")
.attr("stroke-width", "3");
svg.selectAll("matches_r")
.data(matches)
.enter().append("rect")
.attr("x", (d, i) => arr_width + buffer_2 + i*xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", d => { //
if ("right" in d) { return height - yScale(dataset[d.left]) - yScale(dataset[d.right])} //
else if ("hash_val" in d) { return height - yScale(dataset[d.left]) - yScale(d.hash_val)} //
}) //
.attr("height", d => { //
if ("right" in d) { return yScale(dataset[d.right])}
else if ("hash_val" in d) { return yScale(d.hash_val)}
}) //
.attr("fill", 'black')
.attr("stroke", "white")
.attr("stroke-width", 4.2 - dataset.length/100 - matches.length/200);
// .attr("stroke-width", (d) => {
// if (dataset.length < 100) {return '4'} //
// else if (dataset.length < 200) {return '3'}
// else if (dataset.length < 300) {return '2'}
// else if (dataset.length < 400) {return '2'}
// else {return '0'}
// });

// current combo
svg.append("rect")
// .attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth() + buffer_1)
.attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth() + buffer_1)
.attr("width", xScale.bandwidth())
.attr("y", height - yScale(dataset[left]))
.attr("height", yScale(dataset[left]))
// .attr("fill", '#8f5825')
.attr("fill", '#FF7F50')
.attr("stroke", "black")
.attr("stroke-width", "3");
svg.append("rect")
// .attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth() + buffer_1)
.attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth() + buffer_1)
.attr("width", xScale.bandwidth())
// .attr("y", height - yScale(dataset[left]) - yScale(dataset[right]))
.attr("y", d => { if (right) {return height - yScale(dataset[left]) - yScale(dataset[right])}
else {return height - yScale(dataset[left]) - yScale(goal-dataset[left])}
}) //
// .attr("height", yScale(dataset[right]))
.attr("height", d => { //
if (right) { return yScale(dataset[right])}
else if (matches.length > 0) { return yScale(goal - dataset[left])}
}) //
.attr("fill", 'grey')
.attr("stroke", "black")
.attr("stroke-width", "3");
// goal

svg.append("rect")
// .attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth())
.attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", height - yScale(goal))
.attr("height", yScale(goal))
// .attr("fill", 'wheat')
.attr("fill", '#2a0254')
.attr("stroke", "black")
.attr("stroke-width", "3");

if (labels.includes("Values") && dataset.length + matches.length < 125) {
svg.selectAll("bar_val_labels")
.data(dataset)
.enter().append("text")
.attr("fill", "red")
.attr("x", (d, i) => xScale(i) + xScale.bandwidth()/2)
.attr("y", d => Math.min(height - 10 - .4*xScale.bandwidth(), height - yScale(d)))
.attr("dy", -6 - 130/size)
.text(d => d)
.style("font-size", (d, i) => .75*xScale.bandwidth() + "px")
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle");
}
if (labels.includes("Original_Index") && dataset.length + matches.length < 125) {
svg.selectAll("bar_idx_labels")
.data(dataset)
.enter().append("text")
.attr("fill", "white")
.attr("x", (d, i) => xScale(i) + xScale.bandwidth()/2)
.attr("y", height-1)
.attr("dy", -6 - 130/size)
.text((d,i) =>
{
if (og_idx.length > 0) {return og_idx[i]}
else {return i}
})
.style("font-size", (d, i) => .75*xScale.bandwidth() + "px")
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle");
}
return immediate ? svg.node() : Promises.delay(speed, svg.node());
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sort_and_narrow_3 = {
var input, goal, matches, finished, og_idx, actions, hash, start;
[input, goal, matches, finished, og_idx, actions, hash] = setup(3);
start = new Date()
yield* pauser(three_display(input, { goal: goal, immediate: true }));

for (let j = 1; j < input.length; j++) {
let i = j - 1
yield three_display(input, {goal: goal, left: i, right: j, matches: matches });
while(i >= 0 && input[i] > input[i + 1]) {
let temp = input[i]
input[i] = input[i+1]
input[i+1] = temp
yield three_display(input, {goal: goal, left: i, right: i + 1, matches: matches });
i = i - 1
}
}

let end = input.length-1
let k
for (k = 0; k < input.length-2; k++) {
let sub_goal = goal - input[k]
let i = k+1;
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:end, finished: finished });
while (end > i && input[k] + input[i] + input[end] > goal) {
finished.push(end)
end = end - 1
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:end, finished: finished });
}
let j = end
while (i < j) {
if (input[i] + input[j] > sub_goal) {
j = j - 1
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:j, finished: finished });
} else if (input[i] + input[j] < sub_goal) {
i = i + 1
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:j, finished: finished });
} else {
let left_val = input[i]
let right_val = input[j]
while (input[i] == left_val) {
let count = 0;
while (input[j-count] == right_val && j-count > i) {
var match = {left:k, middle:i, right:j-count}
matches.push(match)
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:j, finished: finished });
count = count + 1
}
i = i + 1
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:j, finished: finished });
}
right_val = input[j]
while (input[j] == right_val) {
j = j - 1
yield three_display(input, {goal: goal, matches:matches, left:k, middle:i, right:j, finished: finished, hash: hash });
}
}
}
finished.push(k)
}
finished.push(k-1)
finished.push(k)
finished.push(k+1)
// finished.push()
yield three_display(input, {goal: goal, matches:matches, finished: finished, hash: hash});
}
Insert cell
// function show_bars(dataset, {finished, left, right, immediate} = {} ) {
function three_display(dataset, {goal, left, middle, right, extra, active_val = [], active_hash = [], matches = [], finished = [], hash = [], og_idx =[], immediate}) {
// let width = 1152
let height = height_slider
let buffer_1 = 0
let buffer_2 = 8
let buffer_h1 = 30

let arr_width = width*(dataset.length)/(dataset.length+2+matches.length) - buffer_1 - buffer_2
let xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.range([0, arr_width])
.padding(.25/Math.sqrt(dataset.length));

let yScale = d3.scaleLinear()
.domain([0, Math.max(d3.max(dataset), goal/2)])
.range([0, height/2-buffer_h1]);
let svg = d3.select(DOM.svg(width, height));

// background
svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("fill", "#dbefff");

// array bars
svg.selectAll("bars")
.data(dataset)
.enter().append("rect")
.attr("x", (d, i) => xScale(i))
.attr("width", xScale.bandwidth())
.attr("y", d => height - yScale(d))
.attr("height", d => yScale(d))
.attr("fill", (d, i) => { //
if (finished && finished.includes(i)) {return '#233b3b'} //
else if (i == left || i == right || i == middle || i == extra || i in active_val) {return 'lime'} //
else {return 'steelblue'} //
;})
.attr("stroke", "black")
.attr("stroke-width", "1");

// hash bars
svg.selectAll("hash")
.data(hash.keys())
.enter().append("rect")
.attr("x", (d, i) => xScale(i))
.attr("width", xScale.bandwidth())
.attr("y", d => 0)
.attr("height", d => yScale(d))
.attr("fill", (d, i) => { //
if (d in active_hash) {return 'lime'} //
// goal - d == dataset[left])
else {return 'wheat'} //
;})
.attr("stroke", "black")
.attr("stroke-width", "1");

// matches
svg.selectAll("matches_l")
.data(matches)
.enter().append("rect")
.attr("x", (d, i) => arr_width + buffer_2 + i*xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", d => height - yScale(dataset[d.left]))
.attr("height", d => yScale(dataset[d.left]))
.attr("fill", '#710000')
.attr("stroke", "black")
.attr("stroke-width", "3");
svg.selectAll("matches_m")
.data(matches)
.enter().append("rect")
.attr("x", (d, i) => arr_width + buffer_2 + i*xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", d => height - yScale(dataset[d.left]) - yScale(dataset[d.middle]))
.attr("height", d => yScale(dataset[d.middle]))
.attr("fill", 'orange');
// .attr("stroke", "black")
// .attr("stroke-width", "3");
svg.selectAll("matches_r")
.data(matches)
.enter().append("rect")
.attr("x", (d, i) => arr_width + buffer_2 + i*xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", d => { //
if ("right" in d) { return height - yScale(dataset[d.left]) - yScale(dataset[d.middle]) - yScale(dataset[d.right])} //
else if ("hash_val" in d) { return height - yScale(dataset[d.left]) - yScale(d.hash_val)} //
}) //
.attr("height", d => { //
if ("right" in d) { return yScale(dataset[d.right])}
else if ("hash_val" in d) { return yScale(d.hash_val)}
}) //
.attr("fill", 'black')
.attr("stroke", "white")
.attr("stroke-width", 4.2 - dataset.length/100 - matches.length/200);
// .attr("stroke-width", (d) => {
// if (dataset.length < 100) {return '4'} //
// else if (dataset.length < 200) {return '3'}
// else if (dataset.length < 300) {return '2'}
// else if (dataset.length < 400) {return '2'}
// else {return '0'}
// });

// current combo
svg.append("rect")
// .attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth() + buffer_1)
.attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth() + buffer_1)
.attr("width", xScale.bandwidth())
.attr("y", height - yScale(dataset[left]))
.attr("height", yScale(dataset[left]))
.attr("fill", '#8f5825')
.attr("stroke", "black")
.attr("stroke-width", "3");
svg.append("rect")
// .attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth() + buffer_1)
.attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth() + buffer_1)
.attr("width", xScale.bandwidth())
.attr("y", height - yScale(dataset[left]) - yScale(dataset[middle]))
.attr("height", yScale(dataset[middle]))
.attr("fill", '#FF7F50')
.attr("stroke", "black")
.attr("stroke-width", "3");
svg.append("rect")
.attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth() + buffer_1)
.attr("width", xScale.bandwidth())
.attr("y", d => { if (right && middle) {return height - yScale(dataset[left]) - yScale(dataset[middle]) - yScale(dataset[right])}
else if (right) {return height - yScale(dataset[left]) - yScale(dataset[right])}
else {return height - yScale(dataset[left]) - yScale(goal-dataset[left])}
}) //
.attr("height", d => { //
if (right) { return yScale(dataset[right])}
else if (matches.length > 0) { return yScale(goal - dataset[left])}
}) //
.attr("fill", 'grey')
.attr("stroke", "black")
.attr("stroke-width", "3");


svg.append("rect")
// .attr("x", arr_width + buffer_2 + matches.length * xScale.bandwidth())
.attr("x", arr_width + buffer_2 + (1+matches.length) * xScale.bandwidth())
.attr("width", xScale.bandwidth())
.attr("y", height - yScale(goal))
.attr("height", yScale(goal))
// .attr("fill", 'wheat')
.attr("fill", '#2a0254')
.attr("stroke", "black")
.attr("stroke-width", "3");

if (labels.includes("Values") && dataset.length + matches.length < 125) {
svg.selectAll("bar_val_labels")
.data(dataset)
.enter().append("text")
.attr("fill", "red")
.attr("x", (d, i) => xScale(i) + xScale.bandwidth()/2)
.attr("y", d => Math.min(height - 10 - .4*xScale.bandwidth(), height - yScale(d)))
.attr("dy", -6 - 130/size)
.text(d => d)
.style("font-size", (d, i) => .75*xScale.bandwidth() + "px")
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle");
}
if (labels.includes("Original_Indeces") && dataset.length + matches.length < 125) {
svg.selectAll("bar_idx_labels")
.data(dataset)
.enter().append("text")
.attr("fill", "white")
.attr("x", (d, i) => xScale(i) + xScale.bandwidth()/2)
.attr("y", height-1)
.attr("dy", -6 - 130/size)
.text((d,i) =>
{
if (og_idx.length > 0) {return og_idx[i]}
else {return i}
})
.style("font-size", (d, i) => .75*xScale.bandwidth() + "px")
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle");
}
return immediate ? svg.node() : Promises.delay(speed, svg.node());
}
Insert cell
Insert cell
Insert cell
Insert cell
function output_data(algo, num_sum, actions, matches, start) {
let end = new Date()
let time = end - start
var result = {"algo": algo, "num_sum": num_sum, "size": size, "list_type": list_type, "actions": actions, "matches": matches, "time": time, "speed": speed, }
metadata.push(result)
}
Insert cell
function setup(num_sum) {
var input, goal, matches, finished, og_idx, actions, hash, date1
[input, goal] = get_input(num_sum)

matches = []
finished = []
actions = []
hash = new Map()
og_idx = [...Array(input.length).keys()]
date1 = new Date()
return [input, goal, matches, finished, og_idx, actions, hash, date1]
}
Insert cell
function get_input(num_sum) {
var input;
var goal;
if (list_type == "unique") {
input = Array.from({length: size}, () =>
Math.floor(Math.random() * (value_range[1]-value_range[0]+1)) + value_range[0]);
if (num_sum == 2)
goal = Math.floor(Math.random() * (two_goal_range[1]-two_goal_range[0])) + two_goal_range[0]
else if (num_sum == 3)
goal = Math.floor(Math.random() * (three_goal_range[1]-three_goal_range[0])) + three_goal_range[0]
}
else if (list_type == "sorted") {
input = [...sorted]
if (num_sum == 2)
goal = default_two_goal
else if (num_sum == 3)
goal = default_three_goal
} else if (list_type == "reverse sorted") {
input = [...reversed]
if (num_sum == 2)
goal = default_two_goal
else if (num_sum == 3)
goal = default_three_goal
} else {
input = [...default_array];
if (num_sum == 2)
goal = default_two_goal
else if (num_sum == 3)
goal = default_three_goal
}
return [input, goal]
}
Insert cell
function swap(a, b, input, og_idx) {
let temp = input[a]
input[a] = input[b]
input[b] = temp
let i = og_idx[a]
og_idx[a] = og_idx[b]
og_idx[b] = i
return input, og_idx
}
Insert cell
function get_line_color(z) {
let d3_cat_colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00","#ffff33","#a65628","#f781bf","#999999"]
if (z == "hash_match") {return d3_cat_colors[0]}
else if (z == "better_sort") {return d3_cat_colors[1]}
else if (z == "naive_sort") {return d3_cat_colors[2]}
else {return d3_cat_colors[6]}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more