2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,15 +1,19 @@
|
|||
{{Sorting Algorithm}}
|
||||
In this task, the goal is to sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
|
||||
;Task:
|
||||
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
|
||||
|
||||
The bubble sort is generally considered to be the simplest sorting algorithm.
|
||||
|
||||
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
|
||||
|
||||
Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
|
||||
|
||||
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
|
||||
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
|
||||
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
|
||||
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
|
||||
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
|
||||
|
||||
This can be expressed in pseudocode as follows (assuming 1-based indexing):
|
||||
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
|
||||
'''repeat'''
|
||||
hasChanged := false
|
||||
'''decrement''' itemCount
|
||||
|
|
@ -19,6 +23,8 @@ This can be expressed in pseudocode as follows (assuming 1-based indexing):
|
|||
hasChanged := true
|
||||
'''until''' hasChanged = '''false'''
|
||||
|
||||
|
||||
;References:
|
||||
* The article on [[wp:Bubble_sort|Wikipedia]].
|
||||
* Dance [http://www.youtube.com/watch?v=lyZQPjUT5B4&feature=youtu.be interpretation].
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,83 +1,59 @@
|
|||
* Bubble Sort
|
||||
* Bubble Sort 01/11/2014 & 23/06/2016
|
||||
BUBBLE CSECT
|
||||
USING BUBBLE,R13,R12
|
||||
SAVEAREA B STM-SAVEAREA(R15) skip savearea
|
||||
DC 17F'0'
|
||||
DC CL8'BUBBLE'
|
||||
STM STM R14,R12,12(R13) save calling context
|
||||
ST R13,4(R15)
|
||||
ST R15,8(R13)
|
||||
LR R13,R15 set addessability
|
||||
LA R12,4095(R13)
|
||||
LA R12,1(R12)
|
||||
MORE EQU *
|
||||
LA R8,0 R8=no more
|
||||
LA R1,A R1=Addr(A(I))
|
||||
LA R2,2(R1) R2=Addr(A(I+1))
|
||||
LA R4,0 to start at 1
|
||||
LA R6,1 increment
|
||||
L R7,N R7=N
|
||||
BCTR R7,0 R7=N-1
|
||||
LOOP BXH R4,R6,ENDLOOP for R4=1 to N-1
|
||||
LH R3,0(R1) R3=A(I)
|
||||
CH R3,0(R2) A(I)::A(I+1)
|
||||
BNH NOSWAP if A(I)<=A(I+1) then goto NOSWAP
|
||||
LH R9,0(R1) R9=A(I)
|
||||
LH R3,0(R2) R3=A(I+1)
|
||||
STH R3,0(R1) A(I)=R3
|
||||
STH R9,0(R2) A(I+1)=R9
|
||||
LA R8,1 R8=more
|
||||
NOSWAP EQU *
|
||||
LA R1,2(R1) next A(I)
|
||||
LA R2,2(R2) next A(I+1)
|
||||
B LOOP
|
||||
ENDLOOP EQU *
|
||||
LTR R8,R8
|
||||
BNZ MORE
|
||||
LA R3,A R3=Addr(A(I))
|
||||
LA R4,0 to start at 1
|
||||
LA R6,1 increment
|
||||
L R7,N
|
||||
PRNT BXH R4,R6,ENDPRNT for R4=1 to N
|
||||
LH R5,0(R3) R5=A(I)
|
||||
CVD R4,P Store I to packed P
|
||||
UNPK Z,P Z=P
|
||||
MVC C,Z C=Z
|
||||
OI C+L'C-1,X'F0' ZAP SIGN
|
||||
MVC BUFFER(4),C+12
|
||||
CVD R5,P Store A(I) to packed P
|
||||
UNPK Z,P Z=P
|
||||
MVC C,Z C=Z
|
||||
OI C+L'C-1,X'F0' ZAP SIGN
|
||||
MVC BUFFER+10(6),C+10
|
||||
WTO MF=(E,WTOMSG)
|
||||
LA R3,2(R3) next A(I)
|
||||
B PRNT
|
||||
ENDPRNT EQU *
|
||||
CNOP 0,4
|
||||
L R13,4(0,R13)
|
||||
LM R14,R12,12(R13) restore context
|
||||
XR R15,R15 set return code to 0
|
||||
BR R14 return to caller
|
||||
N DC A((AEND-A)/2) number of items in A, so N=F'80'
|
||||
A DC H'223',H'356',H'018',H'820',H'664',H'845',H'927',H'198' 8
|
||||
DC H'261',H'802',H'523',H'982',H'242',H'192',H'913',H'230' 16
|
||||
DC H'353',H'565',H'195',H'174',H'665',H'807',H'050',H'539' 24
|
||||
DC H'436',H'249',H'848',H'010',H'006',H'794',H'100',H'433' 32
|
||||
DC H'782',H'728',H'259',H'358',H'206',H'081',H'701',H'997' 40
|
||||
DC H'880',H'520',H'780',H'293',H'861',H'942',H'735',H'091' 48
|
||||
DC H'503',H'582',H'716',H'836',H'135',H'653',H'856',H'142' 56
|
||||
DC H'919',H'498',H'303',H'894',H'536',H'211',H'539',H'986' 64
|
||||
DC H'356',H'796',H'644',H'552',H'771',H'443',H'035',H'780' 72
|
||||
DC H'474',H'278',H'332',H'949',H'351',H'282',H'558',H'904' 80
|
||||
AEND EQU *
|
||||
P DS PL8 packed
|
||||
Z DS ZL16 zoned
|
||||
C DS CL16 character
|
||||
WTOMSG CNOP 0,4
|
||||
DC H'80' length of WTO buffer
|
||||
DC H'0' must be binary zeroes
|
||||
BUFFER DC 80C' '
|
||||
USING BUBBLE,R13,R12 establish base registers
|
||||
SAVEAREA B STM-SAVEAREA(R15) skip savearea
|
||||
DC 17F'0' my savearea
|
||||
STM STM R14,R12,12(R13) save calling context
|
||||
ST R13,4(R15) link mySA->prevSA
|
||||
ST R15,8(R13) link prevSA->mySA
|
||||
LR R13,R15 set mySA & set 4K addessability
|
||||
LA R12,2048(R13) .
|
||||
LA R12,2048(R12) set 8K addessability
|
||||
L RN,N n
|
||||
BCTR RN,0 n-1
|
||||
DO UNTIL=(LTR,RM,Z,RM) repeat ------------------------+
|
||||
LA RM,0 more=false |
|
||||
LA R1,A @a(i) |
|
||||
LA R2,4(R1) @a(i+1) |
|
||||
LA RI,1 i=1 |
|
||||
DO WHILE=(CR,RI,LE,RN) for i=1 to n-1 ------------+ |
|
||||
L R3,0(R1) a(i) | |
|
||||
IF C,R3,GT,0(R2) if a(i)>a(i+1) then ---+ | |
|
||||
L R9,0(R1) r9=a(i) | | |
|
||||
L R3,0(R2) r3=a(i+1) | | |
|
||||
ST R3,0(R1) a(i)=r3 | | |
|
||||
ST R9,0(R2) a(i+1)=r9 | | |
|
||||
LA RM,1 more=true | | |
|
||||
ENDIF , end if <---------------+ | |
|
||||
LA RI,1(RI) i=i+1 | |
|
||||
LA R1,4(R1) next a(i) | |
|
||||
LA R2,4(R2) next a(i+1) | |
|
||||
ENDDO , end for <------------------+ |
|
||||
ENDDO , until not more <---------------+
|
||||
LA R3,PG pgi=0
|
||||
LA RI,1 i=1
|
||||
DO WHILE=(C,RI,LE,N) do i=1 to n -------+
|
||||
LR R1,RI i |
|
||||
SLA R1,2 . |
|
||||
L R2,A-4(R1) a(i) |
|
||||
XDECO R2,XDEC edit a(i) |
|
||||
MVC 0(4,R3),XDEC+8 output a(i) |
|
||||
LA R3,4(R3) pgi=pgi+4 |
|
||||
LA RI,1(RI) i=i+1 |
|
||||
ENDDO , end do <-----------+
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) restore caller savearea
|
||||
LM R14,R12,12(R13) restore context
|
||||
XR R15,R15 set return code to 0
|
||||
BR R14 return to caller
|
||||
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
|
||||
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
|
||||
N DC A((N-A)/L'A) number of items of a *
|
||||
PG DC CL80' '
|
||||
XDEC DS CL12
|
||||
LTORG
|
||||
YREGS
|
||||
RI EQU 6 i
|
||||
RN EQU 7 n-1
|
||||
RM EQU 8 more
|
||||
END BUBBLE
|
||||
|
|
|
|||
|
|
@ -1,25 +1,22 @@
|
|||
function bubblesort{T}(a::AbstractArray{T,1})
|
||||
b = copy(a)
|
||||
isordered = false
|
||||
span = length(b)
|
||||
while !isordered && span > 1
|
||||
isordered = true
|
||||
for i in 2:span
|
||||
if b[i] < b[i-1]
|
||||
t = b[i]
|
||||
b[i] = b[i-1]
|
||||
b[i-1] = t
|
||||
isordered = false
|
||||
end
|
||||
end
|
||||
span -= 1
|
||||
function bubblesort!{T}(x::AbstractArray{T})
|
||||
|
||||
for i in 2:length(x)
|
||||
for j in 1:length(x)-1
|
||||
if x[j] > x[j+1]
|
||||
tmp = x[j]
|
||||
x[j] = x[j+1]
|
||||
x[j+1] = tmp
|
||||
end
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
return x
|
||||
end
|
||||
|
||||
|
||||
a = [rand(-100:100) for i in 1:20]
|
||||
println("Before bubblesort:")
|
||||
println(a)
|
||||
a = bubblesort(a)
|
||||
a = bubblesort!(a)
|
||||
println("\nAfter bubblesort:")
|
||||
println(a)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
function bubbleSort( array &$array )
|
||||
{
|
||||
do
|
||||
{
|
||||
$swapped = false;
|
||||
for( $i = 0, $c = count( $array ) - 1; $i < $c; $i++ )
|
||||
{
|
||||
if( $array[$i] > $array[$i + 1] )
|
||||
{
|
||||
list( $array[$i + 1], $array[$i] ) =
|
||||
array( $array[$i], $array[$i + 1] );
|
||||
$swapped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
while( $swapped );
|
||||
function bubbleSort(array &$array) {
|
||||
$c = count($array) - 1;
|
||||
do {
|
||||
$swapped = false;
|
||||
for ($i = 0; $i < $c; ++$i) {
|
||||
if ($array[$i] > $array[$i + 1]) {
|
||||
list($array[$i + 1], $array[$i]) =
|
||||
array($array[$i], $array[$i + 1]);
|
||||
$swapped = true;
|
||||
}
|
||||
}
|
||||
} while ($swapped);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
sub bubble_sort (@a is rw) {
|
||||
sub bubble_sort (@a) {
|
||||
for ^@a -> $i {
|
||||
for $i ^..^ @a -> $j {
|
||||
@a[$j] < @a[$i] and @a[$i, $j] = @a[$j, $i];
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ def bubble_sort(seq):
|
|||
if seq[i] > seq[i+1]:
|
||||
seq[i], seq[i+1] = seq[i+1], seq[i]
|
||||
changed = True
|
||||
return None
|
||||
return seq
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Sample usage and simple test suite"""
|
||||
|
|
|
|||
|
|
@ -1,39 +1,32 @@
|
|||
/*REXX program sorts an array (of any items) using the bubble-sort algorithm.*/
|
||||
call gen /*generate the array elements (items).*/
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('─',79) /*show a separator line (before/after).*/
|
||||
call bubbleSort # /*invoke the bubble sort with # items.*/
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n /*N: number of array elements.*/
|
||||
m=n-1 /*use this as a handy variable for sort*/
|
||||
do until done; done=1 /*keep sorting the array until done. */
|
||||
do j=1 for m; k=j+1 /*search for an element out─of─order. */
|
||||
if @.j>@.k then do; _=@.j /*Out of order? Then swap two elements*/
|
||||
@.j=@.k /*swap current element with the next···*/
|
||||
@.k=_ /* ··· and the next with _ */
|
||||
done=0 /*indicate that the sorting isn't done,*/
|
||||
end /* (1≡true, 0≡false). */
|
||||
end /*j*/
|
||||
end /*until ··· */
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: @. = /*assign a default value to all of @. */
|
||||
@.1 = '---letters of the Hebrew alphabet---' ; @.13 = 'kaph [kaf]'
|
||||
@.2 = '====================================' ; @.14 = 'lamed'
|
||||
@.3 = 'aleph [alef]' ; @.15 = 'mem'
|
||||
@.4 = 'beth [bet]' ; @.16 = 'nun'
|
||||
@.5 = 'gimel' ; @.17 = 'samekh'
|
||||
@.6 = 'daleth [dalet]' ; @.18 = 'ayin'
|
||||
@.7 = 'he' ; @.19 = 'pe'
|
||||
@.8 = 'waw [vav]' ; @.20 = 'sadhe [tsadi]'
|
||||
@.9 = 'zayin' ; @.21 = 'qoph [qof]'
|
||||
@.10 = 'heth [het]' ; @.22 = 'resh'
|
||||
@.11 = 'teth [tet]' ; @.23 = 'shin'
|
||||
@.12 = 'yod' ; @.24 = 'taw [tav]'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*find how many elements in list.*/
|
||||
w=length(#) /*the maximum width of any index.*/
|
||||
/*REXX program sorts an array (of any kind of items) using the bubble─sort algorithm.*/
|
||||
call gen /*generate the array elements (items).*/
|
||||
call show 'before sort' /*show the before array elements. */
|
||||
say copies('─', 79) /*show a separator line (before/after).*/
|
||||
call bubbleSort # /*invoke the bubble sort with # items.*/
|
||||
call show ' after sort' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n; m=n-1 /*N: number of array elements. */
|
||||
do m=m for m by -1 until ok; ok=1 /*keep sorting array until done.*/
|
||||
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*Not out─of─order?*/
|
||||
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap 2 elements; flag as ¬done*/
|
||||
end /*j*/
|
||||
end /*m*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
|
||||
@.2 = '====================================' ; @.14= "lamed"
|
||||
@.3 = 'aleph [alef]' ; @.15= "mem"
|
||||
@.4 = 'beth [bet]' ; @.16= "nun"
|
||||
@.5 = 'gimel' ; @.17= "samekh"
|
||||
@.6 = 'daleth [dalet]' ; @.18= "ayin"
|
||||
@.7 = 'he' ; @.19= "pe"
|
||||
@.8 = 'waw [vav]' ; @.20= "sadhe [tsadi]"
|
||||
@.9 = 'zayin' ; @.21= "qoph [qof]"
|
||||
@.10= 'heth [het]' ; @.22= "resh"
|
||||
@.11= 'teth [tet]' ; @.23= "shin"
|
||||
@.12= 'yod' ; @.24= "taw [tav]"
|
||||
do #=1 while @.#\==''; end; #=#-1 /*determine #elements in list; adjust #*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: w=length(#); do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue