Skip to content

quicksort probabilistic guarantee #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
miczal opened this issue Aug 3, 2016 · 4 comments
Closed

quicksort probabilistic guarantee #13

miczal opened this issue Aug 3, 2016 · 4 comments

Comments

@miczal
Copy link
Contributor

miczal commented Aug 3, 2016

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.

@AnupKumarPanwar
Copy link
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.

@miczal
Copy link
Contributor Author

miczal commented Aug 9, 2016

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)

@harshildarji
Copy link
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.

@AnupKumarPanwar
Copy link
Member

Thanks for appreciating the Organisation

poyea pushed a commit that referenced this issue Jul 8, 2019
* Create text file for numbers

* Create sol2.py

* Pythonic version of Problem #16 solution

* Update sol2.py

* Valid Python code for Python version 2-3

* Update sol2.py
stokhos pushed a commit to stokhos/Python that referenced this issue Jan 3, 2021
…#935)

* Create text file for numbers

* Create sol2.py

* Pythonic version of Problem TheAlgorithms#16 solution

* Update sol2.py

* Valid Python code for Python version 2-3

* Update sol2.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants