Anonymous user
Sorting algorithms/Bubble sort: Difference between revisions
m
Changed over to headers.
(→[[Java]]: Use Java header instead.) |
m (Changed over to headers.) |
||
Line 22:
=Examples=
==
'''Compiler:''' GCC 4.1.2
Line 144 ⟶ 143:
}
==
Bubble sorting an array in-situ (using destructive updates), using Clean's uniqueness typing. We specified the type of <tt>sweep</tt> using strictness annotations to improve performance.
import StdEnv
Line 165 ⟶ 163:
Start = bsort {x \\ x <- [100,99..1]}
==
def bubbleSort(target) {
Line 184 ⟶ 181:
(Uses the primitive __loop directly because it happens to map to the termination test for this algorithm well.)
==
Sorts the 'cnt' cells stored at 'addr' using the test stored in the deferred word 'bubble-test'. Uses forth local variables for clarity.
Line 232 ⟶ 228:
test tcnt cells dump
==
This version checks for changes in a separate step for simplicity, because Haskell has no variables to track them with.
bsort :: Ord a => [a] -> [a]
Line 272 ⟶ 267:
}
==
Array.prototype.bubblesort = function() {
Line 289 ⟶ 283:
}
==
fn bubbleSort arr =
(
Line 312 ⟶ 305:
myArr = bubbleSort myArr
==
'''Interpreter:''' perl 5.8.8
Line 361 ⟶ 353:
print join(",", Bubble_Sort(@test));
==
define bubble_sort(v);
Line 384 ⟶ 375:
ar =>
==
<pre>
def bubble_sort(seq):
Line 398 ⟶ 388:
</pre>
==
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. Below are two different methods that show four different iterating constructs in ruby.
Line 431 ⟶ 420:
# => [3, 4, 6, 6, 8, 23, 78]
==
const proc: bubbleSort (inout array integer: arr) is func
local
Line 454 ⟶ 442:
bubbleSort(arr);
==
Toka does not have a bubble sort predefined, but it is easy to code a simple one:
|