Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function BubbleSort() {
this.sorter =
function* (arr, colors) {
for (let n = arr.length; n > 0; --n) {
passes++;
for (let i = 0; i < n - 1; ++i) {
// Select two elements and swap if in wrong order
myDelay(100);
Expand All @@ -14,8 +15,10 @@ function BubbleSort() {
yield colors;
colors[i] = color('white');
colors[i + 1] = color('white');
compares++;
if (arr[i] > arr[i + 1]) {
swap(arr, i, i + 1);
swaps++;
}
}
colors[n - 1] = color(128, 255, 51);
Expand Down
3 changes: 3 additions & 0 deletions insertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function InsertionSort() {
this.representation = 1;
this.sorter = function* (arr, colors) {
for (let i = 1; i < arr.length; ++i) {
passes++;
let j = i - 1;

while (j >= 0 && arr[j] > arr[i]) {
Expand All @@ -12,6 +13,8 @@ function InsertionSort() {
yield colors;
colors[i] = color('white');
swap(arr, i, j);
swaps++;
compares++;
swap(colors, i--, j--);
}
}
Expand Down
3 changes: 3 additions & 0 deletions merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ function MergeSort() {
let j = middle + 1;

while (i <= middle && j <= end) {
passes++;
myDelay(100);
colors[i] = color('red');
colors[j] = color('red');
yield colors;
colors[i] = color(128, 255, 51);
colors[j] = color(128, 255, 51);
compares++;
if (arr[i] > arr[j]) {
for (let k = i; k <= j; ++k) {
swap(arr, k, j);
swaps++;
swap(colors, k, j);
}

Expand Down
4 changes: 3 additions & 1 deletion quickHoare.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ function QuickSortHoare() {
while (true) {
do {
++i;
compares++;
} while (arr[i] < pivot);


do {
--j[''];
compares++;
} while (arr[j['']] > pivot);

colors[i] = color('red');
Expand All @@ -43,8 +45,8 @@ function QuickSortHoare() {
if (i >= j[''])
break;


swap(arr, i, j['']);
swaps++;
myDelay(100);
}
}
Expand Down
4 changes: 4 additions & 0 deletions quickLomuto.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ function QuickSortLomuto() {

for (let j = i['']; j < end; ++j) {
myDelay(100);
passes++;
compares++;
if (arr[j] < pivot) {
colors[i['']] = color('red');
colors[j] = color('red');
yield colors;
colors[i['']] = color('white');
colors[j] = color('white');
swap(arr, i['']++, j);
swaps++;
}
}

swap(arr, i[''], end);
swaps++;
colors[i['']] = color(128, 255, 51);
}
}
Expand Down
4 changes: 3 additions & 1 deletion selectionMax.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ function SelectionSortMax() {
for (let i = arr.length - 1; i >= 0; --i) {
// Find maximum element
let index = i;

passes++;
for (let j = 0; j <= i; ++j) {
myDelay(100);
colors[j] = color('red');
colors[index] = color('blue');
yield colors;
colors[j] = color('white');
colors[index] = color('white');
compares++;
if (arr[j] > arr[index]) {
index = j;
}
Expand All @@ -22,6 +23,7 @@ function SelectionSortMax() {
// Swap with current element
if (index !== i) {
swap(arr, i, index);
swaps++;
}

// Current element is correctly sorted
Expand Down
4 changes: 3 additions & 1 deletion selectionMin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ function SelectionSortMin() {
for (let i = 0; i < arr.length; ++i) {
// Find minimum element
let index = i;

passes++;
for (let j = i; j < arr.length; ++j) {
myDelay(100);
colors[j] = color('red');
colors[index] = color('blue');
yield colors;
colors[j] = color('white');
colors[index] = color('white');
compares++;
if (arr[j] < arr[index]) {
index = j;
}
Expand All @@ -22,6 +23,7 @@ function SelectionSortMin() {
// Swap with current element
if (index !== i) {
swap(arr, i, index);
swaps++;
}

// Current element is correctly sorted
Expand Down
15 changes: 12 additions & 3 deletions sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ let array,
sorter,
representation,
paused = true,
w;
w,
passes,
swaps,
compares;
let pageNo;
const bubbleSort = new BubbleSort();
const selectionSortMax = new SelectionSortMax();
Expand Down Expand Up @@ -178,7 +181,9 @@ function setup() {
function init(algo, length) {
paused = true;
w = width / length;

passes=0;
swaps=0;
compares=0;
representation = algo['representation'];

// Generation of array
Expand Down Expand Up @@ -221,13 +226,13 @@ function draw() {
colors = next.value;
paused = next.done;
}

for (let i = 0; i < array.length; ++i) {
fill(colors[i]);
if (representation === LINE) {
stroke(0);
rect(i * w, height - array[i], w, array[i]);
textAlign(LEFT, TOP);
textSize(map(array.length,1,100,40,12));
fill(0);
text(int(array[i]/10),(i+0.1)*w,height-array[i]+(height*.005));
} else if (representation === DOT) {
Expand All @@ -236,6 +241,10 @@ function draw() {
// stroke(colors[i]);
// point(i * w, height - array[i]);
}
stroke(0);
fill(255);
textSize(40);
text("Passes="+passes+" Swaps="+swaps+" Compares="+compares,width*0.2,50);
}
}

Expand Down