Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
quickselect_median
(version: 0)
Comparing performance of:
arrTest1 vs arrTest2
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function quickselect_median(arr) { const L = arr.length, halfL = L/2; if (L % 2 == 1) return quickselect(arr, halfL); else return 0.5 * (quickselect(arr, halfL - 1) + quickselect(arr, halfL)); } function quickselect(arr, k) { // Select the kth element in arr // arr: List of numerics // k: Index // return: The kth element (in numerical order) of arr if (arr.length == 1) return arr[0]; else { const pivot = arr[0]; const lows = arr.filter((e)=>(e<pivot)); const highs = arr.filter((e)=>(e>pivot)); const pivots = arr.filter((e)=>(e==pivot)); if (k < lows.length) // the pivot is too high return quickselect(lows, k); else if (k < lows.length + pivots.length)// We got lucky and guessed the median return pivot; else // the pivot is too low return quickselect(highs, k - lows.length - pivots.length); } }
Tests:
arrTest1
[13, 13, 14, 14, 14, 15, 15, 16]
arrTest2
[7,3,5, 2300,5,4,0,123,2,76,768,28]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
arrTest1
arrTest2
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 benchmark and explain what is tested, compared, and the pros/cons of different approaches. **Benchmark Definition** The provided JSON defines two JavaScript benchmarks: `quickselect_median`. This function is used to find the median value in an array. The implementation uses the QuickSelect algorithm, which is a variation of the QuickSort algorithm that selects a pivot element and partitions the array around it. **Options Compared** In this benchmark, we have two options compared: 1. **QuickSelect**: The original implementation using the QuickSelect algorithm. 2. **Naive Approach**: A simpler approach that iterates through the array to find the median value (not implemented in this example). **Pros and Cons** **QuickSelect** Pros: * Efficient: QuickSelect has an average time complexity of O(n), making it suitable for large datasets. * Simple implementation: The algorithm is relatively straightforward to understand and implement. Cons: * Worst-case scenario: In the worst case, QuickSelect can have a time complexity of O(n^2). However, this occurs very rarely in practice. **Naive Approach** Pros: * Simple to implement: The naive approach requires only basic iteration through the array. * Easy to understand: The algorithm is straightforward and easy to comprehend. Cons: * Inefficient: The naive approach has a time complexity of O(n), which can be slower than QuickSelect for large datasets. **Other Considerations** * **Memory Usage**: Both implementations have a linear memory usage, as they work with the entire array. However, QuickSelect is generally more efficient in terms of memory usage due to its ability to partition the array. * **Stability**: QuickSelect is not stable, meaning that it may swap equal elements during the partitioning process. The naive approach is also not stable. **Library/Function** In this benchmark, there is no explicit library or function used. However, the QuickSelect algorithm relies on a few built-in JavaScript functions: * `Array.prototype.filter()`: Used to filter elements based on conditions. * `Array.prototype.indexOf()` (not explicitly shown): Could be used to find the index of the median element if needed. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The implementation is straightforward and uses standard JavaScript language constructs. **Alternative Implementations** If you're interested in exploring alternative implementations, consider the following: * **Merge Sort**: Another sorting algorithm that can be used to find the median value. * **Heap-based approach**: Using a heap data structure to find the median value more efficiently. * **Iterative approach**: Implementing an iterative version of QuickSelect or using other algorithms like Binary Search. Keep in mind that these alternatives might have different trade-offs and performance characteristics compared to the original implementation.
Related benchmarks:
Versions of Quick Sort
Quick Sort
Versions of Quick Sort vs. .sort
Javascript Sorting Algorithms 2
Comments
Confirm delete:
Do you really want to delete benchmark?