Skip to content

Update the QuickSort Algorithm to minimise chance of O(n^2) #6095

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
faiqali1 opened this issue Apr 12, 2022 · 8 comments
Closed

Update the QuickSort Algorithm to minimise chance of O(n^2) #6095

faiqali1 opened this issue Apr 12, 2022 · 8 comments

Comments

@faiqali1
Copy link

faiqali1 commented Apr 12, 2022

According to this question on StackOverflow it is better to use the median or a random value as a pivot.

Currently the implementation uses the last element as the pivot.

def quick_sort(collection: list) -> list:
    if len(collection) < 2:
        return collection
    pivot = collection.pop()  # <-- Use the last element as the first pivot
    greater: list[int] = []  
    lesser: list[int] = []  
    for element in collection:
        (greater if element > pivot else lesser).append(element)
    return quick_sort(lesser) + [pivot] + quick_sort(greater)
@lt77777
Copy link

lt77777 commented Apr 28, 2022

I can fix this

@lt77777 lt77777 mentioned this issue Apr 28, 2022
14 tasks
@stale
Copy link

stale bot commented Jul 10, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Jul 10, 2022
@mahi072
Copy link

mahi072 commented Sep 9, 2022

Hi @faiqali1, I would like to contribute in this issue. Is it still open?
Please guide me through this.

@stale stale bot removed the stale Used to mark an issue or pull request stale. label Sep 9, 2022
@Ashutosh2613
Copy link

I would like to work on this issue.

@faiqali1
Copy link
Author

Hi @mahi072 And @Ashutosh2613,

Yeah, the issue is still open. Apologies for the late reply.

The current issue, as stated in the title is that, it is better to choose the pivot at a random point then choosing the last element every time.

So using the Random module, choose a Random point between 0 & the length of the array and use that as a pivot.

@Kate-Way
Copy link

Kate-Way commented Sep 18, 2022

HI @faiqali1! I'm new to contribution process. I can't find how fork this question, but here is the code:

import random

def quick_sort(collection: list) -> list:
    if len(collection) < 2:
        return collection
    pivot = random.choice(collection)  
    greater: list[int] = []
    lesser: list[int] = []
    for element in collection:
        (greater if element > pivot else lesser).append(element)
    return quick_sort(lesser) + quick_sort(greater)

@Ashutosh2613
Copy link

@faiqali1 Changes are made, please do review the changes!

@ruchikayadav1408
Copy link

@faiqali1 heyyy shall i work on this?

jeertmans added a commit to jeertmans/Python that referenced this issue Oct 4, 2022
As described in TheAlgorithms#6095, this reduces the chances to observe a O(n^2) complexity.

Here, `collection.pop(pivot_index)` is avoided for performance reasons.
jeertmans added a commit to jeertmans/Python that referenced this issue Oct 4, 2022
As described in TheAlgorithms#6095, this reduces the chances to observe a O(n^2) complexity.

Here, `collection.pop(pivot_index)` is avoided for performance reasons.

Fixes TheAlgorithms#6095
jeertmans added a commit to jeertmans/Python that referenced this issue Oct 4, 2022
As described in TheAlgorithms#6095, this reduces the chances to observe a O(n^2) complexity.

Here, `collection.pop(pivot_index)` is avoided for performance reasons.

Fixes: TheAlgorithms#6095
@poyea poyea closed this as completed in 8cce0d4 Oct 5, 2022
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.

6 participants