Anonymous user
Sorting algorithms/Bubble sort: Difference between revisions
m
Spelling/grammar/aesthetics.
m (→{{header|Perl}}: Add link.) |
m (Spelling/grammar/aesthetics.) |
||
Line 6:
=Algorithm=
The '''bubble sort''' is generally considered to be the simplest sorting algorithm.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it.
This can be expressed in pseudocode as follows (assuming 1-based indexing):
Line 211:
| e1 > e2 = sweep (i + 1) False {array & [i - 1] = e2, [i] = e1}
= sweep (i + 1) done array
Using it to sort an array of a
Start :: {Int}
Start = bsort {x \\ x <- [100,99..1]}
Line 234:
=={{header|Forth}}==
Sorts the 'cnt' cells stored at 'addr' using the test stored in the deferred word 'bubble-test'.
defer bubble-test
Line 289:
| otherwise = x:(_bsort (x2:xs))
_bsort s = s
This version uses the polymorphic <tt>Maybe</tt> type to designate unchanged lists.
bsort :: Ord a => [a] -> [a]
bsort s = case _bsort s of
Line 303:
=={{header|Java}}==
Bubble sorting (ascending) an array of any <tt>Comparable</tt> type:
for(int a = 0; a < comparable.length - 1; a++){
for(int b = a + 1; b < comparable.length; b++){
Line 441:
=={{header|Ruby}}==
Although the native Ruby sort method for Arrays if much faster (O(n*log(n)) versus O(n**2)), you can find a Ruby version of Bubble sort hereunder. It adds the bubblesort! method to the Array object.
class Array
|