quickselect | Go implementation for finding the smallest k elements | Learning library

 by   wangjohn Go Version: Current License: MIT

kandi X-RAY | quickselect Summary

kandi X-RAY | quickselect Summary

quickselect is a Go library typically used in Tutorial, Learning applications. quickselect has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. You can download it from GitHub.

I’m glad you asked. Actually, QuickSelect is on average about 20x faster than a naive sorting implementation. It’s also faster than any selection algorithm alone, since it combines the best of many algorithms and figures out which algorithm to use for a given input. You can see the benchmark results below (run on an Intel Core i7-4600U CPU @ 2.10GHz × 4 with 7.5 GiB of memory). Note that the name of the benchmark shows the inputs to the QuickSelect function where size denotes the length of the array and K denotes k as the integer input. If you’d like to run the benchmarks yourself, you can run them by doing go test -bench . inside of a checkout of the quickselect source.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              quickselect has a low active ecosystem.
              It has 25 star(s) with 6 fork(s). There are 1 watchers for this library.
              OutlinedDot
              It had no major release in the last 6 months.
              There are 2 open issues and 1 have been closed. There are 1 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of quickselect is current.

            kandi-Quality Quality

              quickselect has 0 bugs and 0 code smells.

            kandi-Security Security

              quickselect has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              quickselect code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              quickselect is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              quickselect releases are not available. You will need to build from source code and install.
              Installation instructions are not available. Examples and code snippets are available.
              It has 504 lines of code, 87 functions and 6 files.
              It has high code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed quickselect and discovered the below as its top functions. This is intended to give you an instant insight into quickselect implemented functionality, and help decide if they suit your requirements.
            • ap heapSelectionFinding finds the k elements in the heap
            • randomSelectionFinding shuffles a random selection based on the provided data .
            • naiveSelectionFinding finds the smallest element in the data sorted order
            • QuickSelect shuffles the data based on the number of elements .
            • resetLargestIndex resets the largest index in the slice .
            • partition returns the partition index of data .
            • in insertion sort .
            • Float64Select mutates float64 values .
            • IntQuickSelect performs a quick selection of int values .
            • StringQuickSelect performs a quick selection of strings .
            Get all kandi verified functions for this library.

            quickselect Key Features

            No Key Features are available at this moment for quickselect.

            quickselect Examples and Code Snippets

            No Code Snippets are available at this moment for quickselect.

            Community Discussions

            QUESTION

            how does median of medians work with quickselect
            Asked 2022-Apr-11 at 08:12

            From my understanding, the medians of medians algorithm calls quickselect recursively. What i'm having trouble understanding is what median of medians should return. The idea is that it returns a good pivot value, but to perform quickselect we need a pivot index not a pivot value. Is there gap in my understanding? I've looked at online resources and still dont get it.

            ...

            ANSWER

            Answered 2022-Apr-11 at 08:12

            Your understanding is flawed.

            Quickselect (and quicksort) start with a pivot value, not a pivot index. The partition function returns a pivot index, which is where the pivot value ended up after the partition. There's no way to predict where that will be.

            Median-of-medians finds a pivot value guaranteed to be not too far from the middle, which is enough to guarantee linear time complexity (for quickselect).

            It's really only of theoretical interest. Selecting the pivot at random is much faster, which more than compensates for the occasional bad pivot selection.

            Source https://stackoverflow.com/questions/71823172

            QUESTION

            Fast selection algorithm for duplicate-heavy inputs?
            Asked 2021-Oct-01 at 19:33

            I've familiarized myself with quickselect and median-of-medians for fast selection of the k-th element in an unsorted array. If you try hard enough, you can guarantee the worst case time complexity to lie in O(n).

            My problem is a little different. I'd like to select the k-th number from an unsorted array which contains a very large amount of unpredictable duplicates. What I wonder, is whether there's an approach which is both memory and time efficient with respect to the amount of unique values u as opposed to the total size of the input n. The catch is that sometimes u << n and sometimes u ~ n. (In practice, u is almost constant, while n fluctuates heavily.)

            Bad approach 1 (excuse my python pseudocode, the problem is not related to python specifically):

            ...

            ANSWER

            Answered 2021-Oct-01 at 19:33

            For memory, yes.

            Create a hash mapping values to count. This hash will have size O(u). And then you can do a quickselect giving each value a weight equal to the count.

            But for time, you have to read the whole array which is O(n). Unless you are happy with an approximate answer. In which case you can take a random selection from the array, figure out a hash of approximate counts, and quickselect that. Depending on the purpose, that may be close enough.

            Source https://stackoverflow.com/questions/69409617

            QUESTION

            if/elsif/else return behavior difference between Ruby and Python
            Asked 2021-Aug-16 at 16:24

            I am trying to convert a quickselect from Ruby to Python. The Ruby code is as follows:

            ...

            ANSWER

            Answered 2021-Aug-16 at 16:24

            The Ruby code isn't very idiomatic. The last return in the if-elsif-else branch is a red herring and inconsistent with the other branches in the conditional chain. There's an implicit return on the other branches in the if-elsif-else chain as well.

            To generalize the above idea, in all Ruby functions and methods, if control reaches the end of the function without encountering a return, the return value is that of the last expression evaluated. In a conditional chain, that'd be whichever branch was taken.

            A minimal example is:

            Source https://stackoverflow.com/questions/68797106

            QUESTION

            Find kth largest element using quick select algorithm
            Asked 2021-Aug-06 at 22:15
            #include 
            using namespace std;
            class Solution {
            public:
                int partition(vector &nums, int low,int high,int pivotElement)
                {
                    int pivotPoint=low;
                    for(int i=low;i<=high-1;i++)
                    {
                        if(nums[i]<=pivotElement)
                        {
                            swap(nums[i],nums[pivotPoint]);
                            pivotPoint++;
                        }
                    }
                    swap(nums[pivotPoint],pivotElement);
                    return pivotPoint;
                    
                }
                int quickSelect(vector &nums, int k,int low,int high)
                {
                    int pivotElement=nums[high];
                    int pivot=partition(nums,low,high,pivotElement);
                    if(pivot==k)
                    {   
                        return nums[pivot];
                    }
                    else if(pivot>k)
                        return quickSelect(nums,k,low,pivot-1);
                    else
                    {   
                        return quickSelect(nums,k,pivot+1,high);
                    }
                }
                int findKthLargest(vector& nums, int k) {
                 return quickSelect(nums,nums.size()-k,0,nums.size()-1);
                }
            };
            int main(){
            vector v={3,2,1,5,6,4};
            Solution ob;
            cout<
            ...

            ANSWER

            Answered 2021-Aug-06 at 22:15

            The swap in partition changes the value of the local variable pivotElement, which will not be propogated back to the caller. (You'd get the same effect with nums[pivotPoint] = pivotElement;.)

            You need to pass pivotElement by reference, and pass nums[high] directly in order for nums[high] to be updated:

            Source https://stackoverflow.com/questions/68687828

            QUESTION

            Find K-th largest element with QuickSelect in Python
            Asked 2021-Jun-13 at 12:58

            I am new in Python and I exercising in writing codes, but I am having some troubles.

            I am trying to implement QuickSelect in order to extract the K largest element.

            This is my code;

            ...

            ANSWER

            Answered 2021-Jun-13 at 12:52

            You are having problem with both of your functions. You are setting pivot as in partition function

            Source https://stackoverflow.com/questions/67958233

            QUESTION

            Correct conditions for quickselect
            Asked 2021-Jun-13 at 06:59

            I am implementing the quick-select algorithm to get the kth element in an array, and I am stuck at a place where I don't know how to resolve. Here is my code that doesn't work:

            ...

            ANSWER

            Answered 2021-Jun-13 at 06:59

            If k < partitionIndex, only check the left partition, else only check the right partition.

            Source https://stackoverflow.com/questions/67954168

            QUESTION

            "Stable" k-largest elements algorithm
            Asked 2020-Oct-06 at 19:54

            Related: priority queue with limited space: looking for a good algorithm

            I am looking for an algorithm which returns the k-largest elements from a list, but does not change the order of the k-largest elements, e.g. for k=4 and given 5,9,1,3,7,2,8,4,6, the algorithm should return 9,7,8,6.

            More background, my input data are approximately 200 pairs (distance,importance) which are ordered w.r.t distance, and I need to select the 32 most important of them. Performance is crucial here, since I have to run this selection algorithm a few thousand times.

            Up to now I have the two following ideas, but both of them seem not to be the best possible.

            1. Remove the minimum element iteratively until 32 elements are left (i.e. do selection sort)
            2. Use quickselect or median-of-medians to search for the 32nd largest element. Afterwards, sort the remaining 31 elements again w.r.t. distance.

            I need to implement this in C++, so if anybody wants to write some code and does not know which language to use, C++ would be an option.

            ...

            ANSWER

            Answered 2020-Oct-06 at 17:05

            Use the heap-based algorithm for finding the k largest value, i.e. use a min heap (not a max heap) that never exceeds a size of k. Once it exceeds that size, keep pulling the root from it to restore it to a size of k.

            At the end the heap's root will be k largest value. Let's call it m.

            You could then scan the original input again to collect all values that are at least equal to m. This way you'll have them in their original order.

            When that m is not unique, you could have collected too many values. So check the size of the result and determine how much longer it is than k. Go backwards through that list and mark the ones that have value m as deleted until you have reached the right size. Finally collect the non-deleted items.

            All these scans are O(n). The most expensive step is the first one: O(nlogk).

            Source https://stackoverflow.com/questions/64229488

            QUESTION

            Quickselect algorithm for singly linked list C++
            Asked 2020-Apr-29 at 11:05

            I need an algorithm which can find the median of a singly linked list in linear time complexity O(n) and constant space complexity O(1).

            EDIT: The singly linked list is a C-style singly linked list. No stl allowed (no container, no functions, everything stl is forbidden, e.g no std::forward_list). Not allowed to move the numbers in any other container (like an array). It's acceptable to have a space complexity of O(logn) as this will be actually even under 100 for my lists. Also I am not allowed to use the STL functions like the nth_element

            Basically I have linked list with like 3 * 10^6 elements and I need to get the median in 3 seconds, so I can't use a sorting algoritm to sort the list (that will be O(nlogn) and will take something like 10-14 seconds maybe).

            I've done some search online and I've found that it's posibile to find the median of an std::vector in O(n) and O(1) space compleity with quickselect (the worst case is in O(n^2), but it is rare), example: https://www.geeksforgeeks.org/quickselect-a-simple-iterative-implementation/

            But I can't find any algoritm that does this for a linked list. The issue is that I can use the array index to randomly acces the vectorIf I want to modify that algoritm the complexity will be much bigger, because. For example when I change the pivotindex to the left I actually need to traverse the list to get that new element and go further (this will get me at least O(kn) with a big k for my list, even aproching O(n^2)...).

            EDIT 2:

            I know I have too many variables but I've been testing different stuff and I am still working on my code... My current code:

            ...

            ANSWER

            Answered 2020-Mar-20 at 20:31

            So what you can do is use iterators to hold the position. I have written the algorithm above to work with the std::forward_list. I know this isn't perfect, but wrote this up quickly and hope it helps.

            Source https://stackoverflow.com/questions/60776300

            QUESTION

            Quick-Select worst-case scenario (Θ(n^2) or O(n^2)?)
            Asked 2020-Mar-08 at 22:39

            I have been trying to understand the Quick-Select algorithm and I have found two different values for the complexity of the worst-case running time.

            For example, This website claims that worst-case time complexity is Θ(n^2), whilst GeeksforGeeks claims that it's O(n^2).

            Can someone help me understand which one is correct and why this is the case?

            ...

            ANSWER

            Answered 2020-Mar-08 at 22:39

            Both are correct, but using Θ is a stronger statement. Big O notation gives an asymptotic upper bound, whereas big Theta notation gives the actual asymptotic growth rate.

            As an analogy, imagine Alice and Bob are both counting somebody's legs. Alice says legs = 2, and Bob says legs ≤ 2. Alice and Bob are both correct, but Alice's statement is stronger.

            In informal use, it's quite common to write O when you could have written the stronger statement with Θ, just because most people's keyboards don't have a Θ key.

            Source https://stackoverflow.com/questions/60592691

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install quickselect

            You can download it from GitHub.

            Support

            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/wangjohn/quickselect.git

          • CLI

            gh repo clone wangjohn/quickselect

          • sshUrl

            git@github.com:wangjohn/quickselect.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link