+ 1

Why is my code not working?

https://code.sololearn.com/c44pEUV042h9/?ref=app I am trying to sort an array using the technique of Selection Sorting but my program is not working. It always returns one garbage value and the rest of the values are in the same order as i input them(ie:- it is not being sorted) Could anyone explain whats wrong with the program. Thanks!!

10th Nov 2018, 10:29 AM
Yusha Arif
Yusha Arif - avatar
9 Respuestas
+ 4
It's not invisible in web version. Probably occurred due to copy/paste from a document or sth! 8D
10th Nov 2018, 11:01 AM
Babak
Babak - avatar
+ 3
Taste Look at the weird characters as space for indentation! 8D http://cpp.sh/2huf
10th Nov 2018, 10:56 AM
Babak
Babak - avatar
+ 3
Why not ? for(i=start;i<size;i++) if(arr[min]>arr[i]) min=i I mean its kinda pointless to create a new array while you still has one, its not going to be modified after all
10th Nov 2018, 11:28 AM
Taste
Taste - avatar
+ 2
Taste is right. Check this out. http://cpp.sh/2oz4d
10th Nov 2018, 11:41 AM
Babak
Babak - avatar
+ 1
I'm sorry, but somehow i cant run your code with no error But start the loop from 0 to less than size, it should get rid of the garbage value Your line min[start]=arr[start] doesnt makes much sense to me, why only applied value to 1 index in newly created array ? You can always use arr for comparison than re create new array with same value
10th Nov 2018, 10:45 AM
Taste
Taste - avatar
+ 1
Oh right, so thats why. Btw how did he comes with that character.. Wierd And its entirely invisible in SL
10th Nov 2018, 10:59 AM
Taste
Taste - avatar
0
Yes those dots as spaces were not part of the program. I tried to copy paste my code which is why they showed up. Anyways, thats not the problem. I guess my logic here is not correct. Does anyone know where i am wrong?
10th Nov 2018, 11:09 AM
Yusha Arif
Yusha Arif - avatar
0
Taste i wrote the line min[i] = arr[i] for the sake of comparing each element in arr[ ] with min[ ] . I began with the assumption that the 1st element in arr[ ] was the minimum one. Then i compared it to check wheather or not that was true and if it was false, i would store that value of arr[ ] in min[ ].
10th Nov 2018, 11:15 AM
Yusha Arif
Yusha Arif - avatar
0
Well yes your code should also work. I'll try using your method and see if that works.
10th Nov 2018, 11:35 AM
Yusha Arif
Yusha Arif - avatar