Created
February 23, 2021 23:57
-
-
Save commanderh/e9c285df5575573090f0819b66ad75a1 to your computer and use it in GitHub Desktop.
This file contains 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
/*********************************************************************** | |
Write a recursive function called `sort` that takes an array of integers, `nums` | |
and returns an array containing those integers sorted from least to greatest. | |
Your function should accept a default argument called `sorted` which | |
holds the currently sorted elements. Each recursive step should add | |
the smallest number in the `nums` array to the end of `sorted`. | |
There are many ways to accomplish this task but here's a simple algorithm: | |
1. Check the base case: If `nums` is empty, then return `sorted` | |
2. Otherwise, find the smallest element in `nums` | |
3. Add the smallest element to the end of `sorted` | |
4. Remove the smallest element from `nums` | |
5. Recursively call `sort()` with updated `sorted` and `nums` | |
Examples: | |
sort([4,1,6,3,1,7]); // [1, 1, 3, 4, 6, 7] | |
sort([0, 1, -3]); // [-3, 0, 1] | |
sort([]); // [] | |
***********************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment