Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native sort vs insertion vs selection
(version: 0)
Comparing performance of:
sort vs insertion sort vs select sort
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a=Array.from({length:100},()=>Math.random()) function insertionSort(arr){ //Start from the second element. for(let i = 1; i < arr.length;i++){ //Go through the elements behind it. for(let j = i - 1; j > -1; j--){ //value comparison using ascending order. if(arr[j + 1] < arr[j]){ //swap [arr[j+1],arr[j]] = [arr[j],arr[j + 1]]; } } }; return arr; } function selectionSort(arr) { let min; //start passes. for (let i = 0; i < arr.length; i++) { //index of the smallest element to be the ith element. min = i; //Check through the rest of the array for a lesser element for (let j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { min = j; } } //compare the indexes if (min !== i) { //swap [arr[i], arr[min]] = [arr[min], arr[i]]; } } return arr; }
Tests:
sort
var b = a.slice(); b.sort();
insertion sort
var b = a.slice(); insertionSort(b)
select sort
var b = a.slice(); selectionSort(b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
sort
insertion sort
select 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):
The provided JSON represents a benchmark test that compares the performance of three sorting algorithms: Native JavaScript Sort, Insertion Sort, and Selection Sort. **What is tested?** * The benchmark tests the time taken by each sorting algorithm to sort an array of 100 random numbers. * Each test case uses a slightly different approach: + "sort" uses the native JavaScript `sort()` method, which likely uses a hybrid sorting algorithm (e.g., Timsort). + "insertion sort" manually implements the Insertion Sort algorithm using two nested loops. + "selection sort" manually implements the Selection Sort algorithm using three nested loops. **Options compared** The benchmark compares the performance of these three algorithms on an array of 100 random numbers. The goal is to identify which algorithm is the fastest, most efficient, and suitable for specific use cases. **Pros and Cons of each approach:** * **Native JavaScript Sort (Timsort):** + Pros: - Generally considered one of the fastest sorting algorithms. - Robust and reliable implementation in modern browsers. + Cons: - May not be suitable for very small or very large datasets, as it can be less efficient than other algorithms in those cases. * **Insertion Sort:** + Pros: - Simple to implement and understand. - Can be a good choice for small datasets or specific use cases where stability is important. + Cons: - Generally slower than native JavaScript sort or Selection Sort for large datasets. * **Selection Sort:** + Pros: - Also simple to implement and understand, similar to Insertion Sort. - Can be a good choice for small datasets or specific use cases where stability is important. + Cons: - Generally slower than native JavaScript sort or Insertion Sort for large datasets. **Other considerations:** * The benchmark results are influenced by the specific implementation of each algorithm, as well as the characteristics of the input data (e.g., random numbers). * The test cases use a relatively small dataset, which may not be representative of real-world scenarios where larger datasets are common. * The benchmark does not consider other factors that might impact performance, such as caching, memory allocation, or parallel processing. **Library used:** None explicitly mentioned in the provided JSON. However, it is likely that the native JavaScript `sort()` method uses a library or implementation under the hood (e.g., Timsort). **Special JS feature/syntax:** The benchmark does not use any special JavaScript features or syntax that would affect its execution. The scripts are written in vanilla JavaScript and do not rely on any modern ES6+ features. **Alternatives:** Other sorting algorithms that could be tested include: * Merge Sort * Quick Sort * Heap Sort * Radix Sort (for numerical data) * Bucket Sort These algorithms might offer better performance or efficiency for specific use cases, such as: * Large datasets * Specific data types (e.g., numerical vs. string) * Real-time sorting requirements * Memory-constrained environments
Related benchmarks:
Javascript native sort vs quick-insertion-sort
Test Quick Insertion Sort3
Sorting Test: Tim Sort
Javascript Sorting Algorithms (Best case, almost ordered)
Comments
Confirm delete:
Do you really want to delete benchmark?