September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,14 @@
|
|||
10 LET S$="FIRE BURN AND CAULDRON BUBBLE"
|
||||
20 PRINT S$
|
||||
30 LET L=LEN S$-1
|
||||
40 LET C=0
|
||||
50 FOR I=1 TO L
|
||||
60 IF S$(I)<=S$(I+1) THEN GOTO 120
|
||||
70 LET T$=S$(I)
|
||||
80 LET S$(I)=S$(I+1)
|
||||
90 LET S$(I+1)=T$
|
||||
100 PRINT AT 0,I-1;S$(I TO I+1)
|
||||
110 LET C=1
|
||||
120 NEXT I
|
||||
130 LET L=L-1
|
||||
140 IF C THEN GOTO 40
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
import StdEnv
|
||||
|
||||
bsort :: *(a e) -> *(a e) | Array a e & < e
|
||||
bsort array
|
||||
# (done, array) = sweep 1 True array
|
||||
= if done array (bsort array)
|
||||
where
|
||||
sweep :: !Int !Bool !*(a e) -> (!Bool, !*(a e)) | Array a e & < e
|
||||
sweep i done array
|
||||
| i >= size array = (done, array)
|
||||
# (e1, array) = array![i - 1]
|
||||
(e2, array) = array![i]
|
||||
| e1 > e2 = sweep (i + 1) False {array & [i - 1] = e2, [i] = e1}
|
||||
= sweep (i + 1) done array
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
Start :: {Int}
|
||||
Start = bsort {x \\ x <- [100,99..1]}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import system'routines.
|
||||
import extensions.
|
||||
|
||||
extension $op
|
||||
{
|
||||
bubbleSort
|
||||
[
|
||||
var list := self clone.
|
||||
|
||||
bool madeChanges := true.
|
||||
int itemCount := list length.
|
||||
while (madeChanges)
|
||||
[
|
||||
madeChanges := false.
|
||||
itemCount -= 1.
|
||||
0 till:itemCount do(:i)
|
||||
[
|
||||
if (list[i] > list[i + 1])
|
||||
[
|
||||
list exchange(i,i+1).
|
||||
madeChanges := true.
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
^ list
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := (3, 7, 3, 2, 1, -4, 10, 12, 4).
|
||||
console printLine(list bubbleSort).
|
||||
].
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
fun <T> bubbleSort(a : Array<T>, c: Comparator<T>) {
|
||||
var changed : Boolean
|
||||
import java.util.Comparator
|
||||
|
||||
fun <T> bubbleSort(a: Array<T>, c: Comparator<T>) {
|
||||
var changed: Boolean
|
||||
do {
|
||||
changed = false
|
||||
for (i in 0 .. a.size - 2) {
|
||||
for (i in 0..a.size - 2) {
|
||||
if (c.compare(a[i], a[i + 1]) > 0) {
|
||||
val tmp = a[i]
|
||||
a[i] = a[i + 1]
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
function list = bubbleSort(list)
|
||||
|
||||
hasChanged = true;
|
||||
itemCount = numel(list);
|
||||
|
||||
while(hasChanged)
|
||||
|
||||
hasChanged = false;
|
||||
itemCount = itemCount - 1;
|
||||
|
||||
for index = (1:itemCount)
|
||||
|
||||
if(list(index) > list(index+1))
|
||||
list([index index+1]) = list([index+1 index]); %swap
|
||||
hasChanged = true;
|
||||
end %if
|
||||
|
||||
end %for
|
||||
end %while
|
||||
end %bubbleSort
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
bubbleSort([5 3 8 4 9 7 6 2 1])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/* Rexx */
|
||||
Do
|
||||
placesList = sampleData()
|
||||
call show placesList
|
||||
say
|
||||
sortedList = bubbleSort(placesList)
|
||||
call show sortedList
|
||||
say
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
bubbleSort:
|
||||
procedure
|
||||
Do
|
||||
il = arg(1)
|
||||
sl = il~copy
|
||||
|
||||
listLen = sl~size
|
||||
loop i_ = 1 to listLen
|
||||
loop j_ = i_ + 1 to listLen
|
||||
cmpi = sl[i_]
|
||||
cmpj = sl[j_]
|
||||
if cmpi > cmpj then do
|
||||
sl[i_] = cmpj
|
||||
sl[j_] = cmpi
|
||||
end
|
||||
end j_
|
||||
end i_
|
||||
return sl
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
show:
|
||||
procedure
|
||||
Do
|
||||
al = arg(1)
|
||||
|
||||
loop e_ over al
|
||||
say e_
|
||||
end e_
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
sampleData:
|
||||
procedure
|
||||
Do
|
||||
placesList = .array~of( ,
|
||||
"UK London", "US New York", "US Boston", "US Washington", ,
|
||||
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
|
||||
)
|
||||
|
||||
return placesList
|
||||
End
|
||||
Exit
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
/* Rexx */
|
||||
Do
|
||||
placesList = sampleData()
|
||||
call show placesList
|
||||
say
|
||||
sortedList = bubbleSort(placesList)
|
||||
call show sortedList
|
||||
say
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
bubbleSort:
|
||||
procedure
|
||||
Do
|
||||
il = arg(1)
|
||||
sl = il~copy
|
||||
itemCount = sl~size
|
||||
|
||||
loop label c_ until \hasChanged
|
||||
hasChanged = isFalse()
|
||||
itemCount = itemCount - 1
|
||||
loop i_ = 1 to itemCount
|
||||
if sl[i_] > sl[i_ + 1] then do
|
||||
t_ = sl[i_]
|
||||
sl[i_] = sl[i_ + 1]
|
||||
sl[i_ + 1] = t_
|
||||
hasChanged = isTrue()
|
||||
end
|
||||
end i_
|
||||
end c_
|
||||
|
||||
return sl
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
show:
|
||||
procedure
|
||||
Do
|
||||
al = arg(1)
|
||||
|
||||
loop e_ over al
|
||||
say e_
|
||||
end e_
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
sampleData:
|
||||
procedure
|
||||
Do
|
||||
placesList = .array~of( ,
|
||||
"UK London", "US New York", "US Boston", "US Washington", ,
|
||||
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
|
||||
)
|
||||
|
||||
return placesList
|
||||
End
|
||||
Exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
isTrue: procedure
|
||||
return (1 == 1)
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
isFalse: procedure
|
||||
return \isTrue()
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/* Rexx */
|
||||
Do
|
||||
placesList. = ''
|
||||
sortedList. = ''
|
||||
call sampleData
|
||||
call bubbleSort
|
||||
|
||||
do i_ = 1 to placesList.0
|
||||
say placesList.i_
|
||||
end i_
|
||||
say
|
||||
|
||||
do i_ = 1 to sortedList.0
|
||||
say sortedList.i_
|
||||
end i_
|
||||
say
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
bubbleSort:
|
||||
procedure expose sortedList. placesList.
|
||||
Do
|
||||
/* Copy list */
|
||||
do !_ = 0 to placesList.0
|
||||
sortedList.!_ = placesList.!_
|
||||
end !_
|
||||
|
||||
listLen = sortedList.0
|
||||
do i_ = 1 to listLen
|
||||
do j_ = i_ + 1 to listLen
|
||||
if sortedList.i_ > sortedList.j_ then do
|
||||
!_ = sortedList.j_
|
||||
sortedList.j_ = sortedList.i_
|
||||
sortedList.i_ = !_
|
||||
end
|
||||
end j_
|
||||
end i_
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
sampleData:
|
||||
procedure expose placesList.
|
||||
Do
|
||||
! = 0
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "UK London"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "US New York"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "US Boston"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "US Washington"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "UK Washington"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "US Birmingham"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "UK Birmingham"
|
||||
! = ! + 1; placesList.0 = !; placesList.! = "UK Boston"
|
||||
|
||||
return
|
||||
End
|
||||
Exit
|
||||
|
|
@ -10,4 +10,5 @@ function bubbleSort(array &$array) {
|
|||
}
|
||||
}
|
||||
} while ($swapped);
|
||||
return $array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
/*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.*/
|
||||
say copies('▒', 70) /*show a separator line (before/after).*/
|
||||
call bSort # /*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
|
||||
bSort: procedure expose @.; parse arg n; m=n-1 /*N: is the number of @ array elements.*/
|
||||
do m=m for m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
|
||||
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */
|
||||
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/
|
||||
end /*j*/
|
||||
end /*m*/; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
|
||||
@.2 = '====================================' ; @.14= "lamed"
|
||||
|
|
@ -26,7 +25,7 @@ gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph
|
|||
@.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 #*/
|
||||
do #=1 until @.#==''; end; #=#-1 /*determine #elements in list; adjust #*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: w=length(#); do j=1 for #; say 'element' right(j,w) arg(1)":" @.j; end; return
|
||||
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)":" @.j; end; return
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
|
||||
parse arg N .; if N=='' | N=="," then N=30 /*obtain optional size of array from CL*/
|
||||
call gen N /*generate the array elements (items). */
|
||||
call show 'before sort:' /*show the before array elements. */
|
||||
call bSort N /*invoke the bubble sort with N items.*/
|
||||
call show ' after sort:' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
bSort: procedure expose @.; parse arg n; m=n-1 /*N: is the number of @ array elements.*/
|
||||
do m=m for m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
|
||||
do j=1 for m; k=j+1; if @.j>@.k then parse value @.j @.k 0 with @.k @.j ok
|
||||
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
|
||||
end /*m*/; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: w=1; do j=1 for N; @.j=random(min(N+N,1e5)); w=max(w,length(@.j)); end; return
|
||||
show: parse arg $; do k=1 for N; $=$ right(@.k, w); end; say $; return
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
Call random ,,1000
|
||||
Do i=1 To 10
|
||||
a.i=random(20)
|
||||
End
|
||||
a.0=i-1
|
||||
Call show 'vorher '
|
||||
Call bubble_sort
|
||||
Call show 'nachher'
|
||||
Exit
|
||||
bubble_sort: Procedure Expose a.
|
||||
Do Until no_more_swaps
|
||||
no_more_swaps=1
|
||||
Do i=1 To a.0-1
|
||||
i1=i+1
|
||||
if a.i > a.i1 Then Do
|
||||
temp=a.i; a.i=a.i1; a.i1=temp
|
||||
no_more_swaps=0
|
||||
End
|
||||
End
|
||||
End
|
||||
Return
|
||||
show:
|
||||
l=''; Do i=1 To a.0; l=l a.i; End; Say arg(1)':'l
|
||||
Return
|
||||
|
|
@ -2,11 +2,11 @@ func bubble_sort(arr) {
|
|||
loop {
|
||||
var swapped = false
|
||||
{ |i|
|
||||
if (arr[i-1] > arr[i]) {
|
||||
arr[i-1, i] = arr[i, i-1]
|
||||
if (arr[i] > arr[i+1]) {
|
||||
arr[i, i+1] = arr[i+1, i]
|
||||
swapped = true
|
||||
}
|
||||
} * arr.end
|
||||
} << ^arr.end
|
||||
swapped || break
|
||||
}
|
||||
return arr
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
BEGIN
|
||||
|
||||
PROCEDURE BUBBLESORT(A); NAME A; INTEGER ARRAY A;
|
||||
BEGIN
|
||||
INTEGER LOW, HIGH, I;
|
||||
BOOLEAN SWAPPED;
|
||||
|
||||
PROCEDURE SWAP(I, J); INTEGER I, J;
|
||||
BEGIN
|
||||
INTEGER TEMP;
|
||||
TEMP := A(I); A(I) := A(J); A(J) := TEMP;
|
||||
END**OF**SWAP;
|
||||
|
||||
LOW := LOWERBOUND(A, 1);
|
||||
HIGH := UPPERBOUND(A, 1);
|
||||
SWAPPED := TRUE;
|
||||
WHILE SWAPPED DO
|
||||
BEGIN
|
||||
SWAPPED := FALSE;
|
||||
FOR I := LOW + 1 STEP 1 UNTIL HIGH DO
|
||||
BEGIN
|
||||
COMMENT IF THIS PAIR IS OUT OF ORDER ;
|
||||
IF A(I - 1) > A(I) THEN
|
||||
BEGIN
|
||||
COMMENT SWAP THEM AND REMEMBER SOMETHING CHANGED ;
|
||||
SWAP(I - 1, I);
|
||||
SWAPPED := TRUE;
|
||||
END;
|
||||
END;
|
||||
END;
|
||||
END**OF**BUBBLESORT;
|
||||
|
||||
INTEGER ARRAY A(1:10);
|
||||
INTEGER I, N;
|
||||
I := 1;
|
||||
FOR N := 6, 8, 5, 9, 3, 2, 2, 1, 4, 7 DO
|
||||
BEGIN
|
||||
A(I) := N; I := I + 1;
|
||||
END;
|
||||
BUBBLESORT(A);
|
||||
FOR I:= 1 STEP 1 UNTIL 10 DO
|
||||
OUTINT(A(I), 5);
|
||||
OUTIMAGE;
|
||||
|
||||
END;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
fcn bubbleSort(list){
|
||||
itemCount := list.len();
|
||||
do{
|
||||
hasChanged := False;
|
||||
foreach index in (itemCount -= 1){
|
||||
if (list[index] > list[index + 1]){
|
||||
list.swap(index,index + 1);
|
||||
hasChanged = True;
|
||||
}
|
||||
}
|
||||
}while(hasChanged);
|
||||
list
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
fcn bubbleSort(list){
|
||||
foreach n,index in ([list.len()-1..0,-1],n){
|
||||
if (list[index] > list[index + 1]) list.swap(index,index + 1);
|
||||
}
|
||||
list
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
bubbleSort("This is a test".split("")).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue