Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Custom sort method: Bubble vs Insertion vs Selection
(version: 3)
Comparing performance of:
Bubble sort vs Insertion sort vs Selection sort
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.users = [ { name: 'Ваня', level: '3' }, { name: 'Саша', level: '1' }, { name: 'Маша', level: '2' }, { name: 'Ника', level: '3' }, { name: 'Вера', level: '1' }, { name: 'Саша', level: '2' }, { name: 'Ваня', level: '2' } ]; function swap(items, left, right) { var temp = []; if (left !== right) { temp = items[left]; items[left] = items[right]; items[right] = temp; } } function getItem(item, fieldName) { if (fieldName) { item = item[fieldName] } return item; } function bubbleSort(array, fieldName) { let { length } = array, items = [], i; while (length--) { items[length] = array[length]; } let swapped = true; while (swapped) { swapped = false; for (i = 1; i < length; i++) { if (getItem(items[i - 1], fieldName) > getItem(items[i], fieldName) && getItem(items[i - 1], fieldName) !== getItem(items[i], fieldName)) { swap(items, i - 1, i); swapped = true; } } } return items; } function insertionSort(array, fieldName) { let { length } = array, items = []; let i, j, current; while (length--) { items[length] = array[length]; } for (i = 1; i < length; i++) { current = items[i]; j = i; while (j > 0 && getItem(items[j - 1], fieldName) > getItem(current, fieldName)) { items[j] = items[j - 1]; j--; } items[j] = current; } return items; } function selectionSort(array, fieldName) { let { length } = array, items = []; let i, j, indexMin; while (length--) { items[length] = array[length]; } for (i = 0; i < length - 1; i++) { indexMin = i; for (j = i + 1; j < length; j++) { if (getItem(items[indexMin], fieldName) > getItem(items[j], fieldName)) { indexMin = j; } } swap(items, indexMin, i); } return items; }
Tests:
Bubble sort
bubbleSort(users, "level")
Insertion sort
insertionSort(users, "level")
Selection sort
selectionSort(users, "level")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Bubble sort
Insertion sort
Selection sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 122 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Bubble sort
396331.7 Ops/sec
Insertion sort
399615.7 Ops/sec
Selection sort
403559.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark definition is a JSON object that represents the script to be executed on MeasureThat.net. It contains three functions: `bubbleSort`, `insertionSort`, and `selectionSort`. These functions are designed to sort an array of objects based on a specified field (`level` in this case). **Options being compared:** The options being compared are: 1. Bubble sort 2. Insertion sort 3. Selection sort These three algorithms are all used to sort arrays, but they have different approaches and performance characteristics. **Pros and cons of each approach:** * **Bubble sort:** This algorithm is simple to implement and works by repeatedly swapping adjacent elements if they are in the wrong order. However, it has a worst-case time complexity of O(n^2), which makes it inefficient for large datasets. * **Insertion sort:** This algorithm is also simple to implement and works by iterating through the array one element at a time, inserting each element into its proper position in the sorted portion of the array. It has an average time complexity of O(n^2) but performs better than bubble sort for smaller arrays. * **Selection sort:** This algorithm works by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It has a worst-case time complexity of O(n^2), but its performance is generally better than bubble sort. **Library usage:** The `swap` function is used in all three sorting algorithms. This function takes two arguments, `items` and `left`/`right`, and swaps the values at these indices. The `getItem` function is also used to extract a value from an object based on its field name. **Special JS features or syntax:** There are no special JavaScript features or syntax used in this benchmark. **Benchmark preparation code:** The script preparation code defines an array of user objects with a `level` field and three sorting functions. The `swap` function is defined to swap two elements, and the `getItem` function is defined to extract a value from an object based on its field name. **Individual test cases:** Each individual test case represents one of the sorting algorithms being compared. For example, the first test case runs the bubble sort algorithm using the `bubbleSort` function, passing in the `users` array and the `level` field as arguments. **Other alternatives:** There are other sorting algorithms that could be used instead of these three (bubble, insertion, and selection). Some examples include: * **Merge sort:** This algorithm works by dividing the array into smaller subarrays, sorting each subarray recursively, and then merging the sorted subarrays back together. * **Quick sort:** This algorithm works by selecting a pivot element from the array, partitioning the other elements around it, and then recursively sorting the subarrays on either side of the pivot. * **Heap sort:** This algorithm works by building a heap data structure from the array, removing the largest element from the heap repeatedly, and then restoring the heap property after each removal. Each of these algorithms has its own strengths and weaknesses, and some may be more suitable for certain use cases than others.
Related benchmarks:
Javascript native sort vs quick-insertion-sort
Test Quick Insertion Sort3
native sort vs insertion vs selection
Sort Algo
Sorting Algo
Comments
Confirm delete:
Do you really want to delete benchmark?