Created
April 21, 2023 15:43
-
-
Save MattRix/83b29e82c94e44d409e9ff56acbd5091 to your computer and use it in GitHub Desktop.
Parmetric Quicksort for Arrays in Verse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#> 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