Skip to content

Commit

Permalink
[New] sort simulator 2
Browse files Browse the repository at this point in the history
  • Loading branch information
HoangTran0410 committed May 9, 2022
1 parent 3c44705 commit 85268df
Show file tree
Hide file tree
Showing 14 changed files with 541 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
30 changes: 30 additions & 0 deletions 2022/sort-simulator-2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8" />

</head>

<body>
<script src="js/utils.js"></script>
<script src="js/bubbleSort.js"></script>
<script src="js/binaryInsertionSort.js"></script>
<script src="js/insertionSort.js"></script>
<script src="js/quickSort.js"></script>
<script src="js/radixSort.js"></script>
<script src="js/selectionSort.js"></script>
<script src="js/shakerSort.js"></script>
<script src="js/shellSort.js"></script>
<script src="js/mergeSort.js"></script>
<script src="sketch.js"></script>
</body>

</html>
38 changes: 38 additions & 0 deletions 2022/sort-simulator-2/js/binaryInsertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// https://www.geeksforgeeks.org/binary-insertion-sort/
function binarySearch(a, item, low, high) {
if (high <= low) return item > a[low] ? low + 1 : low;
mid = Math.floor((low + high) / 2);
if (item == a[mid]) return mid + 1;
if (item > a[mid]) return binarySearch(a, item, mid + 1, high);

return binarySearch(a, item, low, mid - 1);
}

async function binaryInsertionSort(array) {
for (let i = 1; i < array.length; i++) {
highlightIndexes[0] = i;
let j = i - 1;
let x = array[i];

// Find location to insert
// using binary search
let loc = Math.abs(binarySearch(array, x, 0, j));

// Shifting array to one
// location right

while (j >= loc) {
array[j + 1] = array[j];
j--;

highlightIndexes[1] = loc;
highlightIndexes[2] = j;
await sleep(sleepTime);
}

// Placing element at its
// correct location
array[j + 1] = x;
}
highlightIndexes = [];
}
20 changes: 20 additions & 0 deletions 2022/sort-simulator-2/js/bubbleSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://viblo.asia/p/javascript-algorithms-bubble-sort-djeZ1RRolWz
async function bubbleSort(array) {
let isOrdered;
for (let i = 0; i < array.length; i++) {
isOrdered = true;
for (let x = 0; x < array.length - 1 - i; x++) {
if (array[x] > array[x + 1]) {
swap(array, x, x + 1);
isOrdered = false;

highlightIndexes[1] = x + 1;
await sleep(sleepTime);
}
}
if (isOrdered) break;
}

highlightIndexes = [];
return array;
}
22 changes: 22 additions & 0 deletions 2022/sort-simulator-2/js/insertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// https://stackabuse.com/insertion-sort-in-javascript/
async function insertionSort(array) {
let n = array.length;
for (let i = 1; i < n; i++) {
highlightIndexes[0] = i;

// Choosing the first element in our unsorted subarray
let current = array[i];
// The last element of our sorted subarray
let j = i - 1;
while (j > -1 && current < array[j]) {
array[j + 1] = array[j];
j--;

highlightIndexes[1] = j;
await sleep(sleepTime);
}
array[j + 1] = current;
}
highlightIndexes = [];
return array;
}
57 changes: 57 additions & 0 deletions 2022/sort-simulator-2/js/mergeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// https://www.geeksforgeeks.org/iterative-merge-sort/
async function mergeSort(array) {
if (array == null) {
return;
}

if (array.length > 1) {
var mid = parseInt(array.length / 2);

// Split left part
var left = Array(mid).fill(0);
for (i = 0; i < mid; i++) {
left[i] = array[i];
}

// Split right part
var right = Array(array.length - mid).fill(0);
for (i = mid; i < array.length; i++) {
right[i - mid] = array[i];
}
mergeSort(left);
mergeSort(right);

var i = 0;
var j = 0;
var k = 0;

// Merge left and right arrays
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
array[k] = left[i];
i++;
} else {
array[k] = right[j];
j++;
}
k++;

highlightIndexes[0] = k;
await sleep(sleepTime);
}

// Collect remaining elements
while (i < left.length) {
array[k] = left[i];
i++;
k++;
}
while (j < right.length) {
array[k] = right[j];
j++;
k++;
}
}

highlightIndexes = [];
}
58 changes: 58 additions & 0 deletions 2022/sort-simulator-2/js/quickSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// https://learnersbucket.com/examples/algorithms/quick-sort-iterative/
async function partitionHigh(array, low, high) {
//Pick the first element as pivot
let pivot = array[high];
let i = low;

//Partition the array into two parts using the pivot
for (let j = low; j < high; j++) {
highlightIndexes[2] = j;
await sleep(sleepTime);
if (array[j] <= pivot) {
swap(array, i, j);
i++;
highlightIndexes[3] = i;
}
}

swap(array, i, high);

//Return the pivot index
return i;
}

async function quickSort(array) {
//Stack for storing start and end index
let stack = [];

//Get the start and end index
let start = 0;
let end = array.length - 1;

//Push start and end index in the stack
stack.push({ x: start, y: end });

//Iterate the stack
while (stack.length) {
//Get the start and end from the stack
const { x, y } = stack.shift();

highlightIndexes[0] = x;
highlightIndexes[1] = y;

//Partition the array along the pivot
const i = await partitionHigh(array, x, y);

//Push sub array with less elements than pivot into the stack
if (i - 1 > x) {
stack.push({ x: x, y: i - 1 });
}

//Push sub array with greater elements than pivot into the stack
if (i + 1 < y) {
stack.push({ x: i + 1, y: y });
}
}

highlightIndexes = [];
}
96 changes: 96 additions & 0 deletions 2022/sort-simulator-2/js/radixSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// https://stackoverflow.com/a/38979903
// ONLY int value
async function radixSort(array) {
var idx1, idx2, idx3, len1, len2, radix, radixKey;
var radices = {},
buckets = {},
curr;
var currLen, currBucket;

len1 = array.length;
len2 = 10; // radix sort uses ten buckets

// find the relevant radices to process for efficiency
for (idx1 = 0; idx1 < len1; idx1++) {
radices[array[idx1].toString().length] = 0;
}

// loop for each radix. For each radix we put all the items
// in buckets, and then pull them out of the buckets.
for (radix in radices) {
// put each array item in a bucket based on its radix value
len1 = array.length;
for (idx1 = 0; idx1 < len1; idx1++) {
await sleep(sleepTime);
highlightIndexes[0] = idx1;

curr = array[idx1];
// item length is used to find its current radix value
currLen = curr.toString().length;
// only put the item in a radix bucket if the item
// key is as long as the radix
if (currLen >= radix) {
// radix starts from beginning of key, so need to
// adjust to get redix values from start of stringified key
radixKey = curr.toString()[currLen - radix];
// create the bucket if it does not already exist
if (!buckets.hasOwnProperty(radixKey)) {
buckets[radixKey] = [];
}
// put the array value in the bucket
buckets[radixKey].push(curr);
} else {
if (!buckets.hasOwnProperty("0")) {
buckets["0"] = [];
}
buckets["0"].push(curr);
}
}
// for current radix, items are in buckets, now put them
// back in the array based on their buckets
// this index moves us through the array as we insert items
idx1 = 0;
// go through all the buckets
for (idx2 = 0; idx2 < len2; idx2++) {
// only process buckets with items
if (buckets[idx2] != null) {
currBucket = buckets[idx2];
// insert all bucket items into array
len1 = currBucket.length;
for (idx3 = 0; idx3 < len1; idx3++) {
array[idx1++] = currBucket[idx3];

await sleep(sleepTime);
highlightIndexes[0] = idx1;
highlightIndexes[1] = idx3;
}
}
}
buckets = {};
}

highlightIndexes = [];
}

// https://stackoverflow.com/a/3817440
async function radixSort2(array) {
for (var div = 1, radix = 16; div < 65536 * 65536; div *= radix) {
var piles = [];

for (var i = 0; i < array.length; ++i) {
var p = Math.floor(array[i] / div) % radix;
(piles[p] || (piles[p] = [])).push(array[i]);
}

for (var i = 0, ai = 0; i < piles.length; ++i) {
if (!piles[i]) continue;
for (var pi = 0; pi < piles[i].length; ++pi) {
array[ai++] = piles[i][pi];

highlightIndexes[0] = ai;
await sleep(sleepTime);
}
}
}
highlightIndexes = [];
}
27 changes: 27 additions & 0 deletions 2022/sort-simulator-2/js/selectionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://stackabuse.com/selection-sort-in-javascript/
async function selectionSort(array) {
let n = array.length;

for (let i = 0; i < n; i++) {
highlightIndexes[0] = i;
// Finding the smallest number in the subarray
let min = i;
for (let j = i + 1; j < n; j++) {
await sleep(sleepTime);
highlightIndexes[1] = j;
if (array[j] < array[min]) {
highlightIndexes[2] = j;
min = j;
}
}
if (min != i) {
// Swapping the elements
let tmp = array[i];
array[i] = array[min];
array[min] = tmp;
}
}

highlightIndexes = [];
return array;
}
27 changes: 27 additions & 0 deletions 2022/sort-simulator-2/js/shakerSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://www.stdio.vn/giai-thuat-lap-trinh/bubble-sort-va-shaker-sort-01Si3U
async function shakerSort(array) {
let left = 0;
let right = array.length - 1;
let k = 0;
while (left < right) {
for (let i = left; i < right; i++) {
if (array[i] > array[i + 1]) {
swap(array, i, i + 1);
k = i;
}
highlightIndexes[1] = i + 1;
await sleep(sleepTime);
}
right = k;
for (i = right; i > left; i--) {
if (array[i] < array[i - 1]) {
swap(array, i, i - 1);
k = i;
}
highlightIndexes[1] = i - 1;
await sleep(sleepTime);
}
left = k;
}
highlightIndexes = [];
}
Loading

0 comments on commit 85268df

Please sign in to comment.