For example, say you have a row of books that you want to put in alphabetical order, but some joker has written all the titles in Sanskrit. You need to look up each title in your English-Sanskrit dictionary whenever you want to make a comparison, but once you know the order they should be in, it's a simple matter to put them there.

However, say you have a group of elephants and you want to put them in order of size. It's fairly easy to see which of two elephants is the bigger one, but if they need to be swapped then it'd be fairly tricky to manoeuvre them without getting trunked.

That answer isn't very specific though, so it's probably not what you were thinking of.

]]>A. Count the number of list comparisons.

B. Count the number of swaps.

Both can be applied to bubble sort. And we have the best and worst cases as follows:

| A | B

-------------------------------

Worst | n^2 | n^2

-------------------------------

Best | n | 0

Now the question becomes which is a better model, A or B? There is a very specific answer to this question. But it is a more of a think-about-it-yourself-for-a-bit-then-I'll-tell-you-the-right-answer sort of question.

]]>]]>

Do you mean the total number of swaps, best and worst case?]]>

Now do the same analysis for bubblesort, but this time by counting a single swap as a cost of 1, everything else counts as 0.

]]>btw, do you think this should be moved to Coders Corner?

]]>]]>

Just one more thing Ricky...

Subtract one more at the end of the inner loop so when the comparison is made on the last two elements, you don't overstep the array boundary.

Isn't it neat the way items get moved along and dropped when a more significant value is found and then that is moved along and dropped off?? Pretty cool and easy to write.]]>

Well i'll work on it when i get some time. I've been teaching myself linnear algebra and C++ over the summer and its consuming a lot of my freetime. I'll tackle this next chance I get.

Question, i'm not sure what language you are using. Do I need to consider that a for loop makes a comparison for the entry condition on every itteration of the loop?

]]>