Algorithm Implementation/Sorting/Bubble sort

Bubble Sort
The bubble sort is also known as the ripple sort. The bubble sort is probably the first, reasonably complex module that any beginning programmer has to write. It is a very simple construct which introduces the student to the fundamentals of how sorting works.

A bubble sort makes use of an array and some sort of "swapping" mechanism. Most programming languages have a built-in function to swap elements of an array. Even if a swapping function does not exist, only a couple of extra lines of code are required to store one array element in a temporary field in order to swap a second element into its place. Then the first element is moved out of the temporary field and back into the array at the second element's position.

Here is a simple example of how a bubble sort works: Suppose you have a row of children's toy blocks with letters on them. They are in random order and you wish to arrange them in alphabetical order from left to right.


 * Step 1. Begin with the first block. In this case, the letter G. (Fig. 1.)

Fig. 1


 * Step 2. Look at the block just to the right of it.


 * Step 3. If the block to the right should come before the block on the left, swap them so that they are in order (Fig. 2.)

Fig. 2


 * If you were doing this by hand, you might just pick the blocks to be moved with one in each hand and cross your arms to swap them. Or you might move the first one out of its position temporarily, move the second one in its place, then move the first one to the now empty position (this is the difference between having a single function to do the swap, or writing some code to do it).


 * Step 4. Compare the next block in line with the first, and repeat step 3. Do this until you run out of blocks. Then begin step one again with the second block. (Fig. 3,4,5,6)

Fig. 3 - Pass #2 Fig. 4 - Pass #3 Fig. 5 - Pass #4 Fig. 6 - Completed Sort

Why is it called a bubble sort?
The bubble sort gets its name because elements tend to move up into the correct order like bubbles rising to the surface.

C++
C++ can use the C bubble sort above, or use this for random-access iterators of generic containers and a generic comparison operator:

Ada
Sorts an array of integers.

AutoIt
Sorts an array of variant data types

Go
Go implementation with package sort

Haskell
bubbleSort []=[] bubbleSort x=   (iterate swapPass x) !! ((length x)-1) where swapPass [x]=[x] swapPass (x:y:zs) | x>y = y:swapPass (x:zs) | otherwise = x:swapPass (y:zs)

OCaml
This version checks for changes in a separate step for simplicity.

Pascal
A simplified version:

Rust
Rust function for generic types that implement Copy and PartialOrd traits (e.g. numeric types or strings).

VBA
see discussion for possible mistake

COBOL
If you are dealing with a large volume of data, use the COBOL SORT using sequential files. For sorting a WORKING STORAGE table, the following example assumes that the table is already loaded. The literals "a" indicates the size of the row, and "b" how many rows in the table.

Implementazioni di algoritmi/Bubble sort