Sorting algorithms/Bubble sort: Difference between revisions

→‎{{header|Euphoria}}: Euphoria example added
(→‎{{header|C}}: n^2/2 instead of n^2)
(→‎{{header|Euphoria}}: Euphoria example added)
Line 796:
 
<code lang="eiffel">TWO_WAY_SORTED_SET</code> is implemented internally as a list. For this example, we use the feature <code lang="eiffel">put_front</code> which explicitly adds each new element to the beginning of the list, allowing us to show that the elements are unordered until we sort them. It also causes, in the "Before" output, the elements to be printed in the reverse of the order in which they were added. Under normal circumstances, we would use the feature <code lang="eiffel">extend</code> (rather than <code lang="eiffel">put_front</code>) to add elements to the list. This would assure that the order was maintained even as elements were added.
 
=={{header|Euphoria}}==
<lang euphoria>function bubble_sort(sequence s)
object tmp
integer changed
for j = length(s) to 1 by -1 do
changed = 0
for i = 1 to j-1 do
if compare(s[i], s[i+1]) > 0 then
tmp = s[i]
s[i] = s[i+1]
s[i+1] = tmp
changed = 1
end if
end for
if not changed then
exit
end if
end for
return s
end function
 
include misc.e
constant s = {4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}
 
puts(1,"Before: ")
pretty_print(1,s,{2})
puts(1,"\nAfter: ")
pretty_print(1,bubble_sort(s),{2})</lang>
 
Output:
<pre>Before: {
4,
15,
"delta",
2,
-31,
0,
"alfa",
19,
"gamma",
2,
13,
"beta",
782,
1
}
After: {
-31,
0,
1,
2,
2,
4,
13,
15,
19,
782,
"alfa",
"beta",
"delta",
"gamma"
}</pre>
 
=={{header|Factor}}==
Anonymous user