Skip to content

Conversation

@increscent
Copy link
Contributor

Here's my first pass at the sort! implementation for issue #9

Feel free to improve the lisp code, because I'm not very good at writing lisp yet.

This is the algorithm I used: https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme
You might be able to make it faster using the Hoare partition scheme. The problem I ran into was that we don't have a > operator. We only have the operator the user gives us, which could be <, in which case we only have < and >=. The workaround I used for the Lomuto partition scheme was to compare the pivot value based on index for equality to get <= to work. I thought I could use the inverse of that to get >, but it didn't work for some reason.

Also, I think lisp_subvector had a bug where it would start at 0 no matter what you put for the start. Maybe my assumptions are wrong though.

@justinmeiners
Copy link
Owner

justinmeiners commented Dec 5, 2021

Thanks! I'll take a closer look.

All ordering operators are derivable from <:
a > b <=> b < a
a >= b <=> !(a < b)
a <= b <=> !(b < a)

Vector literals can be defined like so: #(1 2 3) so you don't have to use list->vector.

@justinmeiners justinmeiners merged commit d603088 into justinmeiners:master Dec 5, 2021
@justinmeiners
Copy link
Owner

justinmeiners commented Dec 5, 2021

I merged it in and made changes based on it. See #28

Here are my Lisp style recommendations:

  • The variables pivot and i defined in the outer let could probably be defined as additional variables on do. For example: (pivot hi pivot) in the do list will initialize pivot to hi and then never modify it.
  • I think you should make a separate vector-swap! function. It could even be implemented in C? (This also makes me want to make a swap! macro).

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