Created
April 24, 2025 18:44
-
-
Save paulohfev/ba8b493ff0a0066c05d1d1126ab8a050 to your computer and use it in GitHub Desktop.
Quicksort using recursion
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
| function quicksort(list) { | |
| if (list.length < 2) { | |
| return list | |
| } else { | |
| let pivot = list[0] | |
| let restOfList = list.slice(1) | |
| let listGreaterThanPivot = [] | |
| let listSmallerThanPivot = [] | |
| for (let i = 0; i < restOfList.length; i++) { | |
| if (restOfList[i] > pivot) { | |
| listGreaterThanPivot.push(restOfList[i]) | |
| } else { | |
| listSmallerThanPivot.push(restOfList[i]) | |
| } | |
| } | |
| return quicksort(listSmallerThanPivot).concat([pivot], quicksort(listGreaterThanPivot)); | |
| } | |
| } | |
| console.log('sort', quicksort([1, 15, 10, 33, 2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment