Skip to content

Instantly share code, notes, and snippets.

@gitlarryf
Last active October 3, 2022 20:48
Show Gist options
  • Save gitlarryf/5de18edf0f5231d76e8c7a343d3a8024 to your computer and use it in GitHub Desktop.
Save gitlarryf/5de18edf0f5231d76e8c7a343d3a8024 to your computer and use it in GitHub Desktop.
Sample Quicksort algorithm in Neon (https://github.com/ghewgill/neon-lang/neon-lang.git)
VAR words := [ "dog", "cat", "hamster", "parrot", "chinchilla", "rat", "turtle", "snake" ]
quickSort(INOUT words, 0, words.size() - 1)
FOREACH w IN words DO
print(w)
END FOREACH
--= cat
--= chinchilla
--= dog
--= hamster
--= parrot
--= rat
--= snake
--= turtle
FUNCTION partition(INOUT arr: Array<String>, low, high: Number): Number
LET pivot := arr[high]
VAR i := low - 1
FOR j := low TO high - 1 DO
IF arr[j] < pivot THEN
INC i
LET t1 := arr[i]
arr[i] := arr[j]
arr[j] := t1
END IF
END FOR
LET t2 := arr[i+1]
arr[i+1] := arr[high]
arr[high] := t2
RETURN i + 1
END FUNCTION
FUNCTION quickSort(INOUT arr: Array<String>, low, high: Number)
IF low < high THEN
LET pi := partition(INOUT arr, low, high)
quickSort(INOUT arr, low, pi - 1)
quickSort(INOUT arr, pi + 1, high)
END IF
END FUNCTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment