Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sort Algo
(version: 0)
Comparing performance of:
bubbleSort vs Insertion Sort
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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; }
Tests:
bubbleSort
window.bubbleSort(window.arr);
Insertion Sort
window.insertionSort(window.arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
bubbleSort
Insertion Sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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** * The script preparation code defines a `bubbleSort` function that takes an array as input. * The implementation uses a nested loop to compare adjacent elements in the array. If a pair of adjacent elements is out of order, they are swapped. * The function returns the sorted array. **Insertion Sort** * The script preparation code defines an `insertionSort` function that also takes an array as input. * The implementation uses a similar nested loop structure to compare and swap elements in the array. However, instead of comparing adjacent elements, it compares each element with its predecessor to determine if it's out of order. * The function returns the sorted array. **Comparison** The benchmark tests both sorting algorithms on the same array (`window.arr`) and measures their performance. **Pros and Cons** * Bubble Sort: + Pros: Simple implementation, relatively fast for small arrays. + Cons: Not efficient for large arrays (O(n^2) complexity), may not perform well with duplicate elements or negative numbers. * Insertion Sort: + Pros: More efficient than Bubble Sort for large arrays (O(n^2) complexity), handles duplicate elements and negative numbers better. + Cons: May be slower than other sorting algorithms like QuickSort or MergeSort, which have average-case complexities of O(n log n). **Other Considerations** * Both algorithms are compared in terms of their execution speed, measured as the number of executions per second. * The benchmark may not account for other factors that could affect performance, such as cache locality, branch prediction, or memory allocation. **Library and Special JS Features** * Neither Bubble Sort nor Insertion Sort rely on any external libraries. They are pure JavaScript implementations. * There are no special JavaScript features (e.g., async/await, generators) used in these algorithms. **Alternatives** Other sorting algorithms that could be tested alongside Bubble Sort and Insertion Sort include: 1. QuickSort: A divide-and-conquer algorithm with an average-case complexity of O(n log n). 2. MergeSort: Another divide-and-conquer algorithm with an average-case complexity of O(n log n). 3. HeapSort: A comparison-based sorting algorithm with a time complexity of O(n log n). 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.
Related benchmarks:
Sorting of array
native sort vs insertion vs selection
Sorting Algo
Sorting Test: Tim Sort
Comments
Confirm delete:
Do you really want to delete benchmark?