0

Please Check out this programme, it will help you gain idea . I have made it to arrange numbers in asending order.

I believe that everything is correctly written in this programme. But still the output is struk. Please check out. https://code.sololearn.com/cPws4tcs9e1u/?ref=app

24th May 2017, 6:34 PM
Harsh
Harsh - avatar
2 Answers
+ 2
I think at every iteration( for(i=0; i<a; i++) ), you didn't initialized the variable loc. I used 5,3,9,5,1,8 as input(array will start as {3,9,5,1,8}). At the first iteration, 3 and 9's location is swapped({9,3,5,1,8}). At the second iteration, 3 and 8's location is swapped({9,8,5,1,3}). At the third iteration, Swap is not needed because 5 is the largest in (5,1,3). But the variable loc was set to 4 by the second iteration. It caused 5 and 3 to swap, and descending-ordered array became {9,8,3,5,1}. So I think you have to declare a new value for loc at every iteration(I think it should be i). Try adding the line 'loc=i;' just below the for statement.
24th May 2017, 10:40 PM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 1
thanxx @Orb_h
25th May 2017, 3:17 AM
Harsh
Harsh - avatar