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> |
<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; |
end loop; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
procedure Main is |
|||
⚫ | |||
⚫ | |||
⚫ | |||
(Element => Integer, |
|||
⚫ | |||
⚫ | |||
(Element => Integer, |
|||
⚫ | |||
⚫ | |||
Arr => Arr); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
for J in A'Range loop |
|||
⚫ | |||
⚫ | |||
for J in A'Range loop |
|||
⚫ | |||
⚫ | |||
end |
end loop; |
||
⚫ | |||
⚫ | |||
=={{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}} |
||
⚫ | |||
⚫ | |||
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> |
||
⚫ | |||
=={{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> |
|||
⚫ | |||
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> |
<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! |
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{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> |
<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': |
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 |
|||
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}}== |
=={{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}}== |