window.arr = [5,3,8,4,6]
window.bubbleSort = (arr) => {
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length - i - 1; j++){
if(arr[j + 1] < arr[j]){
[arr[j + 1],arr[j]] = [arr[j],arr[j + 1]]
}
}
};
return arr;
}
window.insertionSort = (arr) => {
for(let i = 1; i < arr.length;i++){
for(let j = i - 1; j > -1; j--){
if(arr[j + 1] < arr[j]){
[arr[j+1],arr[j]] = [arr[j],arr[j + 1]];
}
}
};
return arr;
}
window.bubbleSort(window.arr);
window.insertionSort(window.arr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
bubbleSort | |
Insertion Sort |
Test name | Executions per second |
---|---|
bubbleSort | 1878384.5 Ops/sec |
Insertion Sort | 1738188.5 Ops/sec |
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared options, pros and cons, and other considerations.
Benchmark Definition
The benchmark measures two sorting algorithms: Bubble Sort and Insertion Sort.
Bubble Sort
bubbleSort
function that takes an array as input.Insertion Sort
insertionSort
function that also takes an array as input.Comparison
The benchmark tests both sorting algorithms on the same array (window.arr
) and measures their performance.
Pros and Cons
Other Considerations
Library and Special JS Features
Alternatives
Other sorting algorithms that could be tested alongside Bubble Sort and Insertion Sort include:
These algorithms may be more efficient than Bubble Sort and Insertion Sort for large datasets, but their implementation can be more complex.
If you want to explore these alternatives, the script preparation code would need to be modified accordingly. For example, QuickSort's implementation would require a way to recursively divide the array into smaller subarrays.