Skip to content

quicksort probabilistic guarantee #13

Closed

Description

@miczal

Hi,
I think your implementation of quicksort lacks randomizing of the input. If you shuffle it, you are almost guaranteed to not have O(n^2) time scenario.

Activity

  1. AnupKumarPanwar commented on Aug 9, 2016

    @AnupKumarPanwar
    Member

    What does randomization of input means? We are taking data from user input and of course that will be random. Please elaborate your suggestion.

  2. miczal commented on Aug 9, 2016

    @miczal
    ContributorAuthor

    Before quicksort you should just shuffle the input array, so that you avoid worst-case scenario.
    Consider this - If you have already sorted input, then you encounter O(n^2) scenario, but if you shuffle the array before passing it to the sorting algorithm, then it is MUCH more likely to be O(n log n).

    I think that this should be fine.

    from random import shuffle
    def quick_sort(collection):
        shuffle(collection)
        quick_sort_impl(collection)
    
  3. harshildarji commented on Aug 11, 2016

    @harshildarji
    Member

    @miczal
    Hi, This is an open-source repository and anyone can make a pull request with the changes that can improve the performance of the algorithm. If you find a way to improve the performance, we request you to make a pull request.

  4. AnupKumarPanwar commented on Aug 13, 2016

    @AnupKumarPanwar
    Member

    Thanks for appreciating the Organisation

  5. added a commit that references this issue on Jul 8, 2019
  6. added a commit that references this issue on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions