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

Add Vect merge sort #334

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

Add Vect merge sort #334

wants to merge 1 commit into from

Conversation

shmish111
Copy link
Contributor

No description provided.

@gallais
Copy link
Collaborator

gallais commented Apr 27, 2020

This looks like insertion sort to me.

@rgrover
Copy link
Contributor

rgrover commented Apr 28, 2020

The following works:

export
sortBy : {n : Nat} -> (elem -> elem -> Ordering) -> Vect n elem -> Vect n elem
sortBy _ [] = []
sortBy _ [x] = [x]
sortBy order xs with (parity n)
  sortBy {n = j + j} order xs | Even =
    let (left, right) = splitAt j xs in
      mergeBy order (sortBy order left) (sortBy order right)
  sortBy {n = S (j + j)} order xs | Odd =
    let (left, right) = splitAt (S j) xs in
      mergeBy order (sortBy order left) (sortBy order right)
  where

    data Parity : Nat -> Type where
       Even : {n : _} -> Parity (n + n)
       Odd  : {n : _} -> Parity (S (n + n))

    parity : (n:Nat) -> Parity n
    parity 0 = Even {n = Z}
    parity (S k) with (parity k)
      parity (S (j + j)) | Even {n = j} = Odd
      parity (S (S (j + j))) | Odd {n = j} =
        rewrite (plusSuccRightSucc j j) in Even {n = S j}

@shmish111
Copy link
Contributor Author

Sorry I got confused because this uses the existing mergeBy function. I would like a sort function in Vect and I felt like this was the most sensible way to do it given that someone has written mergeBy already.

@edwinb
Copy link
Owner

edwinb commented May 9, 2020

This would be good to have, but it is indeed insertion sort. I'll merge once it's updated.

@shmish111
Copy link
Contributor Author

@edwinb updated in what way? You mean you would prefer a merge sort rather than an insertion sort?

@edwinb
Copy link
Owner

edwinb commented May 13, 2020

I think merge sort would be better, yes, just for the generally better performance - I think that is what you intended though isn't it?

@shmish111
Copy link
Contributor Author

No, I just needed a sort on Vect but I can have a go at merge sort if I have time, good learning.

@shmish111
Copy link
Contributor Author

I've added a merge sort and left the insertion sort but changed the default to merge sort.

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.

4 participants