0

Selection sort

I am not getting the array in sorted format I guess logic is correct . If not please help me out https://code.sololearn.com/ca5a15A18a22

17th Apr 2021, 1:01 PM
Pankaj Rajani
Pankaj Rajani - avatar
4 ответов
+ 4
First of all, you forgot to reset "flag" to false at the start of the outer loop, and for the outer loop it is sufficient to check if 'i' is smaller than size - 1. The main issue is that you forgot to update "min" inside the if statement. Right now, you would swap the current element with the last number smaller than it, which is not necessarily the smallest number in the sequence left (consider [3, 1, 2] as an example). Inside the conditional, you need to assign "min" the value of arr[ j ], to reflect the currently smallest value.
17th Apr 2021, 1:21 PM
Shadow
Shadow - avatar
+ 3
Because you are not resetting "flag" to false at the start of the outer loop, as mentioned earlier.
17th Apr 2021, 1:34 PM
Shadow
Shadow - avatar
+ 1
Shadow Thanks for your help But still the 44 is not getting swapped with 35
17th Apr 2021, 1:33 PM
Pankaj Rajani
Pankaj Rajani - avatar
+ 1
Thank you so much for your help Need to clear some basics
17th Apr 2021, 1:37 PM
Pankaj Rajani
Pankaj Rajani - avatar