Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created April 21, 2023 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/83b29e82c94e44d409e9ff56acbd5091 to your computer and use it in GitHub Desktop.
Save MattRix/83b29e82c94e44d409e9ff56acbd5091 to your computer and use it in GitHub Desktop.
Parmetric Quicksort for Arrays in Verse
<#> Usage:
SortedItems := Sort(Items, true, CompareInt)
Sort(Items : []t, IsAscending : logic, Comparer: type{_(:t, :t)<computes> : int} where t : type)<computes> : []t =
if (Items.Length > 1, Pivot := Items[Floor(Items.Length/2)]):
Left := for(Item : Items, Comparer(Item, Pivot) = -1) do Item
Middle := for(Item : Items, Comparer(Item, Pivot) = 0) do Item
Right := for(Item : Items, Comparer(Item, Pivot) = 1) do Item
if(IsAscending?):
Sort(Left, IsAscending, Comparer) + Middle + Sort(Right, IsAscending, Comparer)
else:
Sort(Right, IsAscending, Comparer) + Middle + Sort(Left, IsAscending, Comparer)
else:
Items
CompareInt(A : int, B : int)<computes>: int =
{
if(A < B) then -1
else if(A > B) then 1
else 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment