Skip to content
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

Quicksort #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Quicksort #144

wants to merge 1 commit into from

Conversation

jjcnn
Copy link
Contributor

@jjcnn jjcnn commented Jul 12, 2023

A non-randomized quicksort algorithm.

The algorithm could probably be made more robust. Currently, the assumption is that the n input elements are located in the first n memory addresses, which won't be true in general if called from another program.

I have not looked into how to pick the pivot element at random.

@jjcnn jjcnn requested a review from Dominik1999 July 12, 2023 21:02
@jjcnn jjcnn force-pushed the jjcnn-quicksort branch 2 times, most recently from 51f26e4 to 538e552 Compare July 18, 2023 11:48
Copy link
Contributor

@Dominik1999 Dominik1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good; thanks a lot. I played around with it manually and read the code. I couldn't find a bug.

Two questions:

  • Did you test it in Rust with some more inputs?
  • Can you add it to the std lib if @bobbinth agrees?

#! Input stack: [ n ]
#! Input advice: [ element_1, element_2, ..., element_n ]
#!
#! Output stack: The 16 highest elements in descending order
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it also outputs more elements if there are any, right? At least in the playground, I get more results and the same should be the case if I use the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@jjcnn
Copy link
Contributor Author

jjcnn commented Jul 25, 2023

* Did you test it in Rust with some more inputs?

I tested it on the command line with various combinations of input. I haven't done any performance testing on it, though - I don't think I went above 16 elements.

* Can you add it to the std lib if @bobbinth agrees?

Sure, though we should change it to be more flexible if it is to be added to the standard library. For one thing, it should be able to sort an array (i.e., a sequential block of memory) starting from any address, not just from address 0.

I'm also not sure we can add randomization when choosing the pivot element, so the algorithm runs in O(n^2) time. This means that we could just as well just use bubblesort, which is a simpler algorithm.

@sonarcloud
Copy link

sonarcloud bot commented Oct 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

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 this pull request may close these issues.

2 participants