In C++ Data Stucture
What is the sinificance of a Bubble Sort?
This is actually posted in the Zoology section . . . anyway, here is an answer:
There are many ways to implement so-called bubble-sort algorithms - I give one below.
Suppose you are sorting a list of numbers in the order largest to smallest.
A bubble sort picks the first number in the list and stores it as "biggest". It then compares "biggest" with the next number in the list - if this next number is larger, then it swaps "biggest" with that value so that "biggest" still contains the largest value encountered thus far.
"Biggest" is then compared with the third number in the list, and so on.
By the end of the list, "biggest" will contain the largest value in the list - this value can be stored as the first number (the original list can be used, and the first value over-written)
The same process is now carried out with the second number in the list: it is stored as "biggest", and compared with the third number, fourth number, . . . swapping if necessary. By the end of the list, "biggest" will contain the next-largest number - this can be stored as the second number in the sorted list.
This whole routine is carried out with each number in the list, the 3rd, 4th, . . . numbers, and so on, until the list has been traversed.
A bubble sort is ok for short lists, but it is one of the slowest ways to sort a list of numbers. It is even worse for lists of names (strings), but can be made slightly more efficient by eliminating the need to swap strings - this can be accomplished by swapping a list of pointers (so-called 'keys'), each of which points to one of the names in the list.
There are tons of sites which give demos of sorting algorithms, and many even include code.
{just google "sorting algorithms" to get some}
Reply:It is quicker, and less processor intensive than a linear sort, but will need to be repeated depending on the level of disorder.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment