sort() function

How exactly does it work? If you had the list of numbers 4, 16, 3, 5, 21 then the image I have in my head would be the sort function going through the list a certain number of times until they are in ascending order, comparing each 2 adjacent elements as it goes.

So, upon the first time through the list it would be:
4 < 16
16 > 3 so swap them
16 > 5 so swap them
16 < 21

which would yield:
4, 3, 5, 16, 21 but then it would have to go through the list again. Is this how it works? Does it keep going through the list from start to finish until it has it right or does it somehow do it in one go?
Sorting is a subject in it's own right and there are a different number of sorting alorithms.
What you are thinking of is called the 'bubble sort' - it is what 99% of hyman beings would comeup with if asked to sort something - but it is abominably slooow.

here are some links:
http://dzone.com/snippets/sorting-algorithms
http://www.concentric.net/~ttwang/sort/sort.htm
http://betterexplained.com/articles/sorting-algorithms/
closed account (4z0M4iN6)
You could also lookup this topic:

Bubble sort in assembly

http://cplusplus.com/forum/beginner/70422/
Topic archived. No new replies allowed.