Custom Sort
Compare
Swap
Flip
Shuffle
Done
Your Own Custom Sort:
// Your code goes here
// Follow the instructions below to implement your code within this sort visualizer
// Here is an example of a bubble sort implementation
async function sort() {
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array.length - i - 1; j++) {
if (await compare(j, j + 1)) {
swap(j, j + 1);
await sleep();
if (firstRun) return;
}
}
}
}
To utilize this page, follow the instructions below and write your sorting algorithm code. After that, click 'Run' to visualize it. The page auto-evaluates your code without a 'Save' or 'Reset' button. Here are the essential functions:
- sort() : Your primary code should be within this function. It should be an asynchronous function, using async/await syntax. No parameters are required for this function.
- compare() : Use this function to compare two array elements. It takes two parameters, representing the indexes of the elements to be compared. this require a await keyword to be used.
- swap() : Use this function to swap two array elements. It takes two parameters, representing the indexes of the elements to be swapped.
- flip() : This function flips the array from a index of the array to another index. This function takes two parameters, the start and end indexes.
- shuffle() : This function shuffles the array randomly. No parameters are needed.
Other important aspects include:
- 'array' variable : The predefined array to be sorted. You should not create your own, use this for comparison.
- sleep() function : Delays the code execution. No parameters are required. use this everytime you alter the array. Use the await keyword in front of it.
- if (firstRun) return : Stops the loop if the user clicks 'Shuffle' during a paused sort. Use this right after the sleep() function.
For further guidance or custom sort creation, review the sort.js file in the console (accessible by pressing F12 or 'Inspect' button when right-clicking).
IMPORTANT NOTE: Use the console for viewing, testing, and debugging your code. Open the console by pressing F12 or 'Inspect' button.