Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
(Undo revision 65088 by 66.194.217.221 (Talk) Vandalism)
m (Whitespace removal)
Line 44: Line 44:
{{works with|GCC|4.1.2}}
{{works with|GCC|4.1.2}}


<lang ada> generic
<lang ada>generic
type Element is private;
type Element is private;
with function "=" (E1, E2 : Element) return Boolean is <>;
with function "=" (E1, E2 : Element) return Boolean is <>;
with function "<" (E1, E2 : Element) return Boolean is <>;
with function "<" (E1, E2 : Element) return Boolean is <>;
type Index is (<>);
type Index is (<>);
type Arr is array (Index range <>) of Element;
type Arr is array (Index range <>) of Element;
procedure Bubble_Sort (A : in out Arr);
procedure Bubble_Sort (A : in out Arr);

procedure Bubble_Sort (A : in out Arr) is
procedure Bubble_Sort (A : in out Arr) is
Finished : Boolean;
Finished : Boolean;
Temp : Element;
Temp : Element;
begin
begin
loop
loop
Finished := True;
Finished := True;
for J in A'First .. Index'Pred (A'Last) loop
for J in A'First .. Index'Pred (A'Last) loop
if A (Index'Succ (J)) < A (J) then
if A (Index'Succ (J)) < A (J) then
Finished := False;
Finished := False;
Temp := A (Index'Succ (J));
Temp := A (Index'Succ (J));
A (Index'Succ (J)) := A (J);
A (Index'Succ (J)) := A (J);
A (J) := Temp;
A (J) := Temp;
end if;
end if;
end loop;
exit when Finished;
end loop;
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 Sort is new
procedure Main is
type Arr is array (Positive range <>) of Integer;
Bubble_Sort
procedure Sort is new
(Element => Integer,
Bubble_Sort
Index => Positive,
Arr => Arr);
(Element => 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)));
end loop;
for J in A'Range loop
Put (Integer'Image (A (J)));
New_Line;
end Main;</lang>
end loop;
New_Line;
end Main;</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>MODE DATA = INT;
<lang algol68>MODE DATA = INT;

PROC swap = (REF[]DATA slice)VOID:
PROC swap = (REF[]DATA slice)VOID:
(
(
Line 189: Line 188:
=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
{{trans|Java}}


{{trans|Java}}
Assume numbers are in a DIM of size "size" called "nums".
Assume numbers are in a DIM of size "size" called "nums".
<lang qbasic>DO
<lang qbasic>DO
Line 344: Line 343:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Bubble sort an sequence in-place, using the < operator for comparison if no comaprison function is provided
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 #'<))
<lang lisp>(defun bubble-sort (sequence &optional (compare #'<))
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
Line 616: Line 614:


=={{header|Lisaac}}==
=={{header|Lisaac}}==
<lang Lisaac>
<lang Lisaac>Section Header
Section Header


+ name := BUBBLE_SORT;
+ name := BUBBLE_SORT;
Line 659: Line 656:
};
};
}.do_while {!sorted};
}.do_while {!sorted};
);
);</lang>
</lang>


=={{header|Lucid}}==
=={{header|Lucid}}==
Line 682: Line 678:


=={{header|M4}}==
=={{header|M4}}==
<lang M4>
<lang M4>divert(-1)
divert(-1)


define(`randSeed',141592653)
define(`randSeed',141592653)
Line 727: Line 722:
show(`a')
show(`a')
bubblesort(`a')
bubblesort(`a')
show(`a')
show(`a')</lang>
</lang>


Output:
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
9 17 25 38 55 63 71 80 88 90</pre>
</pre>


=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 980: Line 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.
{{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
<lang ruby>class Array
def bubblesort1!
def bubblesort1!
length.times do |j|
length.times do |j|
for i in 1...(length - j)
for i in 1...(length - j)
if self[i] < self[i - 1]
if self[i] < self[i - 1]
self[i], self[i - 1] = self[i - 1], self[i]
self[i], self[i - 1] = self[i - 1], self[i]
end
end
end
end
end
end
self
self
end
end

def bubblesort2!
def bubblesort2!
each_index do |index|
each_index do |index|
(length - 1).downto( index ) do |i|
(length - 1).downto( index ) do |i|
a, b = self[i-1], self[i]
a, b = self[i-1], self[i]
a, b = b, a if b < a
a, b = b, a if b < a
end
end
end
end
self
self
end
end
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}}==
=={{header|Scheme}}==
Line 1,026: Line 1,016:


This solution iteratively finds the fixed point of sort-step. A comparison function must be passed to bubblesort. Example usages:
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) >)
<lang scheme>(bubble-sort (list 1 3 5 9 8 6 4 2) >)
(bubble-sort (string->list "Monkey") char<?)</lang>
(bubble-sort (string->list "Monkey") char<?)</lang>


Here is a recursive bubble sort which sorts list 'l' using the comparator 'f':
Here is a recursive bubble sort which sorts list 'l' using the comparator 'f':
Line 1,045: Line 1,035:
=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>const proc: bubbleSort (inout array integer: arr) is func
<lang seed7>const proc: bubbleSort (inout array integer: arr) is func
local
local
var integer: i is 0;
var integer: i is 0;
var integer: j is 0;
var integer: j is 0;
var integer: help is 0;
var integer: help is 0;
begin
begin
for i range 1 to length(arr) do
for i range 1 to length(arr) do
for j range succ(i) to length(arr) do
for j range succ(i) to length(arr) do
if arr[i] < arr[j] then
if arr[i] < arr[j] then
help := arr[i];
help := arr[i];
arr[i] := arr[j];
arr[i] := arr[j];
arr[j] := help;
arr[j] := help;
end if;
end if;
end for;
end for;
end for;
end for;
end func;
end func;
var array integer: arr is [] (3, 78, 4, 23, 6, 8, 6);
var array integer: arr is [] (3, 78, 4, 23, 6, 8, 6);
bubbleSort(arr);</lang>
bubbleSort(arr);</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Line 1,179: Line 1,169:
=={{header|Ursala}}==
=={{header|Ursala}}==
The bubblesort function is parameterized by a relational predicate.
The bubblesort function is parameterized by a relational predicate.
<lang Ursala>
<lang Ursala>#import nat
#import nat


bubblesort "p" = @iNX ^=T ^llPrEZryPrzPlrPCXlQ/~& @l ~&aitB^?\~&a "p"?ahthPX/~&ahPfatPRC ~&ath2fahttPCPRC
bubblesort "p" = @iNX ^=T ^llPrEZryPrzPlrPCXlQ/~& @l ~&aitB^?\~&a "p"?ahthPX/~&ahPfatPRC ~&ath2fahttPCPRC
Line 1,189: Line 1,178:
output:
output:
<pre><140,212,247,270,315,362,449,532,567,677></pre>
<pre><140,212,247,270,315,362,449,532,567,677></pre>



=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==