Any tips for turning iterative into recursive?

let me know if the question is too vague, but i was trying to turn iterative into recursive function (sorting algo) and i keep getting confused with declaring variables that i then used for iteration, and now want to use for a return/termination condition within the same func. i suppose i could embed a second function in the function but thought i’m probably missing a trick here

function selectionSort(array) {
  let arrSlice = array
  let i=
  //for(let i=0;i<array.length-1;i++){
    arrSlice = array.slice(i)
    const min = Math.min(...arrSlice);
    const index = arrSlice.indexOf(min, 0);
    [array[i],array[index+i]] = [array[index+i],array[i]];
  //selectionSort(?)
    i++;
  //}
  return array;
}
console.log(  selectionSort([4,3,2,8,345,123,43,32,5643,63,123,43,55,1,234,92])  )

You know that i needs to start at 0. Think of a way to define an extra function parameter where i would default to 0 if no value was specified. Then, all you need to do after the i++; line is return the array when you know that each value of i has been used or return a recursive call to the function again specifying the applicable value of i as an argument.

you mean like this:

function selectionSort(array, eye) {
  if(!eye){i=0}
  if(eye>=array.length-1){return array}
  let arrSlice = array
  //let i=
  //for(let i=0;i<array.length-1;i++){
    arrSlice = array.slice(i)
    const min = Math.min(...arrSlice);
    const index = arrSlice.indexOf(min, 0);
    [array[i],array[index+i]] = [array[index+i],array[i]];
  
    i++;
    selectionSort(array, i)
  //}
  return array;
}
console.log(  selectionSort([4,3,2,8,345,123,43,32,5643,63,123,43,55,1,234,92])  )

regardless, it is working perfectly. thank you.
i am a little bemused that i don’t seem to be declaring i in this code tho, but i’m getting no complaints in vsCode/chrome.

You are implicitly declaring i, making it a global variable. Given how common i is as a variable name, you probably don’t want to do that.

1 Like

I was thinking more along the lines of creating a second parameter i that has a default value of 0.

EDIT: The following two lines really should be one line.

  let arrSlice = array
  arrSlice = array.slice(i)

do you mean more like this, which works fine (the other one failed in FCC editor):

function selectionSort(array, eye) {
 // if(!eye){let i=0}else{let i=eye}
 let i = eye || 0;

[edit noted, thanks]

which i could then do like this???:

function selectionSort(array, i) {
 // if(!eye){let i=0}else{let i=eye}
 i = i || 0;
  if(i>=array.length-1){return array}

(bit worried if i’ve done another implicit, global declaration)

I would research default values for function parameters. You are still not taking advantage of them here.

function selectionSort(array, i=0) {
 //i = i || 0;
  if(i >= array.length-1){return array}
  //let i=

???

1 Like

and because it’s a function parameter (with default value) i don’t worry about declaring a variable in the recursive function? i suppose that is a common trick?

Default parameter values are a pretty common trick, yeah. You’ll see it for recursion sometimes.

It’s good to practice, but this is a somewhat awkward problem for recursion in my opinion. Recursion is a nice fit when you have to repeat a task an unknown number of times, but you have a fixed length arry here, so recursion doesn’t buy you anything. It just makes the logic a bit harder to read at a glance.

It is a useful learning tool for exploring function calls, scope, and return values, but it’s a pretty rare case when you need recursion in the wild. Some sort of tree traversal is the usual best use case for recursion.

1 Like