From: Fabian Streitel Date: 2009-07-13T01:21:02+09:00 Subject: Re: one line sorting --0015174c15f044788b046e8497c5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit > > It turns out that I was right about having different number of exchanges > but > I was wrong about direction. > If you sort numbers from lowest to biggest you need smaller number of > exchanges than if you sort in opposite direction. > I made a test and in this particullar list [7,4,9,55,98,11,42] it's 11 > exchanges for smallest to biggest number and 18 for biggest to smallest.. Alright buddy, now try it with some other list, e.g. [5,4,3,2,1] When sorting from lowest to biggest, you'll get a lot of exchanges when sorting from biggest to lowest, you'll have no exchanges. No matter what direction you sort in, I'm always able to give you an array of number so you will need the maximum number of exchanges. That's the "beauty" of bubblesort. It is n-squared and it will always be, no matter what direction you sort in. You should probably read more about effective sorting, instead of trying to figure out how to optimize your bubblesort here. It is ineffective, no matter what you do! If you want a more effective sorting algorithm, try Quicksort or Mergesort, but please stop trying to optimize your n-squared bubblesort here... I really can't get what it is you're trying to do here? Greetz! --0015174c15f044788b046e8497c5--