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
(nums)
| 22 | ***********************************************************************/ |
| 23 | |
| 24 | function sort(nums) { |
| 25 | // 1. Check the base case: If `nums` is empty, then return [] |
| 26 | if(!nums.length) return []; |
| 27 | |
| 28 | // 2. Otherwise, find the smallest element in `nums` |
| 29 | // let largestNum = -Infinity; |
| 30 | // let largestI = 0; |
| 31 | // for(let i = 0; i < nums.length; i++){ |
| 32 | // let num = nums[i]; |
| 33 | // // console.log(num); |
| 34 | // if(largestNum < num) { |
| 35 | // largestNum = num; |
| 36 | // largestI = i |
| 37 | // } |
| 38 | // } |
| 39 | let largestNum = Math.max(...nums); |
| 40 | let index = nums.indexOf(largestNum); |
| 41 | |
| 42 | nums.splice(index, 1) |
| 43 | let largest = []; |
| 44 | largest.push(largestNum) |
| 45 | |
| 46 | sort(nums) + sort(nums) |
| 47 | return sort(nums).concat(largest) |
| 48 | // return [...sort(nums), largestNum]; |
| 49 | } |
| 50 | /* |
| 51 | |
| 52 | sort([]) => [] |