0

Why no output?

I made one with loop: not working now with recursion : no outputđŸ˜‘đŸ˜¶đŸ˜„ https://code.sololearn.com/cesAOsP31kz9/?ref=app https://code.sololearn.com/cu4RUDsDw927/?ref=app

1st Oct 2018, 5:10 AM
Akib
Akib - avatar
7 Answers
+ 2
It's an infinite loop because you call bst() with the same values again and again. Change mid++ in line 24 to ++mid, same with mid-- in line 26. This way mid is in/decreased before the recursive function call.
1st Oct 2018, 6:03 AM
Anna
Anna - avatar
+ 1
Thanks a lot Anna. Can you look into the second one?
1st Oct 2018, 6:58 AM
Akib
Akib - avatar
+ 1
I tried the second one a couple of times, all I see is "an error occurred, check your internet connection". What kind of output do you get? The sorting algorithm in lines 35-44 with two nested loops might be wrong. With bubble sort, you only need to check if any item is bigger than the next one and swap them if necessary. You can do that in a single loop, for(i=0; i<items-1;i++) { if(item[i] > item[i+1]) swap() }. Maybe the nested loops lead to an endless loop as well because you compare every item with every other item đŸ€”
1st Oct 2018, 12:03 PM
Anna
Anna - avatar
+ 1
I was able to run the code now. Same problem as with the first one: change mid++/-- to ++/--mid in lines 79/82.
1st Oct 2018, 12:11 PM
Anna
Anna - avatar
+ 1
Thanks a lot for your help😊.
1st Oct 2018, 5:23 PM
Akib
Akib - avatar
0
Where do you print the output?
1st Oct 2018, 5:16 AM
Daniele Bonomi
Daniele Bonomi - avatar
0
bst causes an infinite loop (just put a cout inside of it and you'll see). Usually in a function that you want to call recursively there should be a "terminating" statement, which will return a fixed value for a particular value that will end the recursion, but bst doesn't have such thing and keeps calling itself on and on.
1st Oct 2018, 5:49 AM
fra
fra - avatar