diff --git a/bubble.js b/bubble.js index db1d5b2..5699754 100644 --- a/bubble.js +++ b/bubble.js @@ -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); @@ -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); diff --git a/insertion.js b/insertion.js index 1dfb275..78b1ffd 100644 --- a/insertion.js +++ b/insertion.js @@ -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]) { @@ -12,6 +13,8 @@ function InsertionSort() { yield colors; colors[i] = color('white'); swap(arr, i, j); + swaps++; + compares++; swap(colors, i--, j--); } } diff --git a/merge.js b/merge.js index dc1144a..b848638 100644 --- a/merge.js +++ b/merge.js @@ -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); } diff --git a/quickHoare.js b/quickHoare.js index 255e190..f7ce1ec 100644 --- a/quickHoare.js +++ b/quickHoare.js @@ -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'); @@ -43,8 +45,8 @@ function QuickSortHoare() { if (i >= j['']) break; - swap(arr, i, j['']); + swaps++; myDelay(100); } } diff --git a/quickLomuto.js b/quickLomuto.js index 993a863..f22f8b5 100644 --- a/quickLomuto.js +++ b/quickLomuto.js @@ -25,6 +25,8 @@ 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'); @@ -32,10 +34,12 @@ function QuickSortLomuto() { colors[i['']] = color('white'); colors[j] = color('white'); swap(arr, i['']++, j); + swaps++; } } swap(arr, i[''], end); + swaps++; colors[i['']] = color(128, 255, 51); } } diff --git a/selectionMax.js b/selectionMax.js index d906921..afd7cf6 100644 --- a/selectionMax.js +++ b/selectionMax.js @@ -6,7 +6,7 @@ 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'); @@ -14,6 +14,7 @@ function SelectionSortMax() { yield colors; colors[j] = color('white'); colors[index] = color('white'); + compares++; if (arr[j] > arr[index]) { index = j; } @@ -22,6 +23,7 @@ function SelectionSortMax() { // Swap with current element if (index !== i) { swap(arr, i, index); + swaps++; } // Current element is correctly sorted diff --git a/selectionMin.js b/selectionMin.js index 2501156..be14805 100644 --- a/selectionMin.js +++ b/selectionMin.js @@ -6,7 +6,7 @@ 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'); @@ -14,6 +14,7 @@ function SelectionSortMin() { yield colors; colors[j] = color('white'); colors[index] = color('white'); + compares++; if (arr[j] < arr[index]) { index = j; } @@ -22,6 +23,7 @@ function SelectionSortMin() { // Swap with current element if (index !== i) { swap(arr, i, index); + swaps++; } // Current element is correctly sorted diff --git a/sketch.js b/sketch.js index f2662c2..78103e2 100644 --- a/sketch.js +++ b/sketch.js @@ -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(); @@ -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 @@ -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) { @@ -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); } }