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 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

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)

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

so, i found one example where we don’t know how many iterations there will be and i eventually realised i don’t need default values for parameters and i still can ‘declare’ them this way avoiding a recursive assignment.
(e.g. i had originally written the below code with this function declaration:
function binarySearch(searchList, value, mid=-1, midIndex=-1, arrayPath=[], list=searchList) {
when i don’t need the -1 at all.)

but my question is, would this be considered a reasonable implementation in the wild? many parameters, and one given a default assignment to another parameter, seems somewhat convoluted.

function binarySearch(searchList, value, mid, midIndex, arrayPath=[], list=searchList) {
  if(list.length <= 0){return 'Value Not Found'}
  else if(mid == value){return arrayPath}

  if(value > mid){ list = list.slice(midIndex+1)
  }else if(value < mid){ list = list.slice(0, midIndex) }

  midIndex = Math.floor((list.length-1)/2)
  mid = list[midIndex]
  arrayPath.push(mid)
  return binarySearch(list, value, mid, midIndex, arrayPath)
}

const testArray = [
  0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  23, 49, 70
]; 
console.log('result:',  binarySearch(testArray, 5)  )   

EDIT: I just realised i don’t need the list=searchList at all and can just change to:
function binarySearch(list, value, mid, midIndex, arrayPath=[]) {
Oh well, i will leave my post as is for what it’s worth. And i still don’t think i get anything out of recursion here, as a do-while loop seems just fine.

no, i belive its supposed to return the values (13,5)

they are the center values of the array as given on previous step
ie 13 is the middle value on original array, then 5 in next step

i didn’t name it as such, but you might want to tell FCC

sorry, maybe i should have linked that originally

although the indices are in the code. look like it’s just matter of the ouput

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.