Sorting algorithms/Bubble sort: Difference between revisions

m
Whitespace removal
(Undo revision 65088 by 66.194.217.221 (Talk) Vandalism)
m (Whitespace removal)
Line 44:
{{works with|GCC|4.1.2}}
 
<lang ada> generic
type Element is private;
with function "=" (E1, E2 : Element) return Boolean is <>;
with function "<" (E1, E2 : Element) return Boolean is <>;
type Index is (<>);
type Arr is array (Index range <>) of Element;
procedure Bubble_Sort (A : in out Arr);
 
procedure Bubble_Sort (A : in out Arr) is
Finished : Boolean;
Temp : Element;
begin
loop
Finished := True;
for J in A'First .. Index'Pred (A'Last) loop
if A (Index'Succ (J)) < A (J) then
Finished := False;
Temp := A (Index'Succ (J));
A (Index'Succ (J)) := A (J);
A (J) := Temp;
end if;
end loop;
exit when Finished;
end loop;
exit when Finished;
end Bubble_Sort;
end loop;
end Bubble_Sort;
-- Example of usage:
 
with Ada.Text_IO; use Ada.Text_IO;
-- Example of usage:
with Bubble_Sort;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
with Bubble_Sort;
type Arr is array (Positive range <>) of Integer;
procedure SortMain is new
type Arr is array (Positive range <>) of Integer;
Bubble_Sort
procedure MainSort is new
(Element => Integer,
Bubble_Sort
Index => Positive,
Arr (Element => Arr);Integer,
Index => Positive,
A : Arr := (1, 3, 256, 0, 3, 4, -1);
Arr => Arr);
begin
A : Arr := (1, 3, 256, 0, 3, 4, -1);
Sort (A);
begin
for J in A'Range loop
Sort (A);
Put (Integer'Image (A (J)));
for J endin A'Range loop;
Put (Integer'Image (A (J)));
New_Line;
end Mainloop;</lang>
New_Line;
end Main;</lang>
 
=={{header|ALGOL 68}}==
<lang algol68>MODE DATA = INT;
 
PROC swap = (REF[]DATA slice)VOID:
(
Line 189 ⟶ 188:
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{trans|Java}}
 
{{trans|Java}}
Assume numbers are in a DIM of size "size" called "nums".
<lang qbasic>DO
Line 344 ⟶ 343:
=={{header|Common Lisp}}==
Bubble sort an sequence in-place, using the < operator for comparison if no comaprison function is provided
 
<lang lisp>(defun bubble-sort (sequence &optional (compare #'<))
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
Line 616 ⟶ 614:
 
=={{header|Lisaac}}==
<lang Lisaac>Section Header
Section Header
 
+ name := BUBBLE_SORT;
Line 659 ⟶ 656:
};
}.do_while {!sorted};
);</lang>
</lang>
 
=={{header|Lucid}}==
Line 682 ⟶ 678:
 
=={{header|M4}}==
<lang M4>divert(-1)
divert(-1)
 
define(`randSeed',141592653)
Line 727 ⟶ 722:
show(`a')
bubblesort(`a')
show(`a')</lang>
</lang>
 
Output:
<pre>17 63 80 55 90 88 25 9 71 38
<pre>
17 63 80 55 90 88 25 9 71 38
 
9 17 25 38 55 63 71 80 88 90</pre>
</pre>
 
=={{header|Mathematica}}==
Line 980 ⟶ 972:
{{eff note|Ruby|Array.sort!}}This example adds the bubblesort! method to the Array object. Below are two different methods that show four different iterating constructs in ruby.
 
<lang ruby> class Array
def bubblesort1!
length.times do |j|
for i in 1...(length - j)
if self[i] < self[i - 1]
self[i], self[i - 1] = self[i - 1], self[i]
end
end
end
self
end
 
def bubblesort2!
each_index do |index|
(length - 1).downto( index ) do |i|
a, b = self[i-1], self[i]
a, b = b, a if b < a
end
end
self
end
end
ary = [3, 78, 4, 23, 6, 8, 6]
 
ary.bubblesort1!
ary = [3, 78, 4, 23, 6, 8, 6]
p ary
ary.bubblesort1!
# => [3, 4, 6, 6, 8, 23, 78]</lang>
p ary
# => [3, 4, 6, 6, 8, 23, 78]</lang>
 
=={{header|Scheme}}==
Line 1,026 ⟶ 1,016:
 
This solution iteratively finds the fixed point of sort-step. A comparison function must be passed to bubblesort. Example usages:
<lang scheme> (bubble-sort (list 1 3 5 9 8 6 4 2) >)
(bubble-sort (string->list "Monkey") char<?)</lang>
 
Here is a recursive bubble sort which sorts list 'l' using the comparator 'f':
Line 1,045 ⟶ 1,035:
=={{header|Seed7}}==
<lang seed7>const proc: bubbleSort (inout array integer: arr) is func
local
var integer: i is 0;
var integer: j is 0;
var integer: help is 0;
begin
for i range 1 to length(arr) do
for j range succ(i) to length(arr) do
if arr[i] < arr[j] then
help := arr[i];
arr[i] := arr[j];
arr[j] := help;
end if;
end for;
end for;
end func;
var array integer: arr is [] (3, 78, 4, 23, 6, 8, 6);
bubbleSort(arr);</lang>
 
=={{header|Smalltalk}}==
Line 1,179 ⟶ 1,169:
=={{header|Ursala}}==
The bubblesort function is parameterized by a relational predicate.
<lang Ursala>#import nat
#import nat
 
bubblesort "p" = @iNX ^=T ^llPrEZryPrzPlrPCXlQ/~& @l ~&aitB^?\~&a "p"?ahthPX/~&ahPfatPRC ~&ath2fahttPCPRC
Line 1,189 ⟶ 1,178:
output:
<pre><140,212,247,270,315,362,449,532,567,677></pre>
 
 
=={{header|Visual Basic .NET}}==
Anonymous user