Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comb Sort vs. Native Sort
(version: 0)
Comparing performance of:
Native Sort DESC vs Native Sort ASC vs Comb Sort DESC vs Comb Sort ASC
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var sampleData = []; for (var i=0; i < 100; i++){ sampleData.push({value: (Math.floor(Math.random() * 1000))}); } var selectorFn = function(x) { return x.value; } var combSort = function (source, selectorFn, sortDirection) { var result = source.slice(); var count = result.length; var gap = count; var swapped = true; while (gap > 1 || swapped) { if (gap > 1) { gap = Math.floor(gap / 1.24733); } var i = 0; swapped = false; while ((i + gap) < count) { var item1 = result[i]; var item2 = result[i + gap]; if (shouldBeSwaped(selectorFn(item1), selectorFn(item2), selectorFn, sortDirection)) { result[i] = item2; result[i + gap] = item1; swapped = true; } i++; } } return result; } var shouldBeSwaped = function(value, valueToCompare, selectorFn, sortDirection) { let less = value > valueToCompare; return sortDirection === "asc" ? less : !less; }
Tests:
Native Sort DESC
var order = "desc"; sampleData.sort(function(a,b){ var x = selectorFn(a); var y = selectorFn(b); var res = ((x < y) ? -1 : ((x > y) ? 1 : 0)); return order === "desc" ? -1*res : res; })
Native Sort ASC
var order = "asc"; sampleData.sort(function(a,b){ var x = selectorFn(a); var y = selectorFn(b); var res = ((x < y) ? -1 : ((x > y) ? 1 : 0)); return order === "desc" ? -1*res : res; })
Comb Sort DESC
var order = "asc"; combSort(sampleData, selectorFn, order);
Comb Sort ASC
var order = "asc"; combSort(sampleData, selectorFn, order);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Native Sort DESC
Native Sort ASC
Comb Sort DESC
Comb Sort ASC
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):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark measures the performance of two sorting algorithms: Native Sort and Comb Sort, on an array of 100 random integers. The benchmark is run in descending order (DESC) and ascending order (ASC). **Native Sort** Native Sort uses the built-in `sort()` method in JavaScript, which sorts the elements of an array in place and returns the sorted array. The benchmark definition for Native Sort contains a lambda function that compares two elements using the `selectorFn` function, which extracts the value from each object. The comparison is done in ascending order if the "order" variable is set to "asc", and in descending order if it's set to "desc". **Comb Sort** Comb Sort is a hybrid sorting algorithm derived from both Insertion Sort and Selection Sort. It starts with a gap of 1 and then gradually decreases the gap by a fixed amount (in this case, 1.24733) until the gap reaches 1. The benchmark definition for Comb Sort contains a call to the `combSort` function, which takes three arguments: the array to sort (`sampleData`), the selector function (`selectorFn`), and the sorting direction ("asc" or "desc"). **Library** The `selectorFn` function is used as a library to extract the value from each object in the array. This function is not part of the standard JavaScript library, but rather a custom implementation that returns the value associated with each element. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. **Pros and Cons** Here are some pros and cons of using Native Sort versus Comb Sort: Native Sort: Pros: * Fast and efficient for large datasets * Well-documented and widely supported Cons: * May not perform well on small or partially sorted datasets * Can be slower than Comb Sort for certain types of data Comb Sort: Pros: * Good performance for both small and large datasets * Simple implementation Cons: * May not be as efficient as Native Sort for very large datasets * Requires a custom implementation (in this case, the `combSort` function) **Other Alternatives** Some other sorting algorithms that could have been used in this benchmark include: * Bubble Sort: A simple sorting algorithm that works by repeatedly iterating through the array and swapping adjacent elements if they are in the wrong order. * Merge Sort: A divide-and-conquer algorithm that splits the array into smaller chunks, sorts each chunk recursively, and then merges the sorted chunks back together. * Quick Sort: Another divide-and-conquer algorithm that selects a pivot element, partitions the array around the pivot, and recursively sorts the subarrays. These algorithms might have been more suitable for certain types of data or performance requirements. However, Native Sort is generally considered to be one of the fastest and most efficient sorting algorithms in JavaScript.
Related benchmarks:
Comb Sort vs. Native Sort
Comb Sort vs. Native Sort
Comb Sort vs. Native Sort
Javascript Sorting Algorithms (Best case, almost ordered)
Comments
Confirm delete:
Do you really want to delete benchmark?