Implementation
How to run
Save the code to a file calledquicksort.walrus and run it:
Expected output
How it works
- Base case: If the list has 0 or 1 elements, it’s already sorted
- Partition: Choose the first element as the pivot and partition the list into three groups:
less: Elements smaller than the pivotequal: Elements equal to the pivotgreater: Elements greater than the pivot
- Recursion: Recursively sort the
lessandgreaterlists - Combine: Concatenate the sorted sublists with
+operator
Key concepts
- Recursion: The function calls itself to sort sublists
- List operations: Creating empty lists, pushing elements, and concatenation
- List iteration: Using
for x in arrto iterate over elements - Pattern matching: Using if/else chains to categorize elements