Rushow, you forgot the terms & conditions of the problem
only one operation ( Flip ) is available and that is you can exchange two adjacent terms
but your algorithm didn't perform like this.
think about second sample input-
2 3 1
in your algo, first interchange occurs between 2 and 1, but 2 and 1 is not adjacent. always swap between two adjacent (consecutive positioned) number (when needed).
so your algo is not flip (bubble) sort, actually replacement sort.
recently, our teacher taught us bubble sort, but you mistook. so bad.
let observe step by step, how flip sort will work
(currently comparing numbers will be shown in red)-
3 2 1 //the initial position of the array n=3 elements
3 2 1 //swap. after swapping: 2 3 1
2 3 1 //swap
2 1 3 //after n-1 comparison the largest element will in last position. so no eed to compare with it latter. again start from the beginning)
2 1 3 //swap
1 2 3 //after n-2 comparison the 2nd largest element will in 2nd last position. so no need to compare with it latter. there has only one element. so sorting was completed)
we swapped for 3 times. so ans=3.
to clear more, u can also get help from:
http://www.comp.nus.edu.sg/~stevenha/programming/prog_sorting.html
// N.B. while writing code, use BBcode tags. visit
BBcode Guide