all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
20
Task/Sorting-algorithms-Bubble-sort/0DESCRIPTION
Normal file
20
Task/Sorting-algorithms-Bubble-sort/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{{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.
|
||||
|
||||
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. 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):
|
||||
'''repeat'''
|
||||
hasChanged := false
|
||||
'''decrement''' itemCount
|
||||
'''repeat with''' index '''from''' 1 '''to''' itemCount
|
||||
'''if''' (item '''at''' index) > (item '''at''' (index + 1))
|
||||
swap (item '''at''' index) with (item '''at''' (index + 1))
|
||||
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].
|
||||
2
Task/Sorting-algorithms-Bubble-sort/1META.yaml
Normal file
2
Task/Sorting-algorithms-Bubble-sort/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Sorting Algorithms
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
(defun bubble (xs)
|
||||
(if (endp (rest xs))
|
||||
(mv nil xs)
|
||||
(let ((x1 (first xs))
|
||||
(x2 (second xs)))
|
||||
(if (> x1 x2)
|
||||
(mv-let (_ ys)
|
||||
(bubble (cons x1 (rest (rest xs))))
|
||||
(declare (ignore _))
|
||||
(mv t (cons x2 ys)))
|
||||
(mv-let (has-changed ys)
|
||||
(bubble (rest xs))
|
||||
(mv has-changed (cons x1 ys)))))))
|
||||
|
||||
(defun bsort-r (xs limit)
|
||||
(declare (xargs :measure (nfix limit)))
|
||||
(if (zp limit)
|
||||
xs
|
||||
(mv-let (has-changed ys)
|
||||
(bubble xs)
|
||||
(if has-changed
|
||||
(bsort-r ys (1- limit))
|
||||
ys))))
|
||||
|
||||
(defun bsort (xs)
|
||||
(bsort-r xs (len xs)))
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
MODE DATA = INT;
|
||||
PROC swap = (REF[]DATA slice)VOID:
|
||||
(
|
||||
DATA tmp = slice[1];
|
||||
slice[1] := slice[2];
|
||||
slice[2] := tmp
|
||||
);
|
||||
|
||||
PROC sort = (REF[]DATA array)VOID:
|
||||
(
|
||||
BOOL sorted;
|
||||
INT shrinkage := 0;
|
||||
FOR size FROM UPB array - 1 BY -1 WHILE
|
||||
sorted := TRUE;
|
||||
shrinkage +:= 1;
|
||||
FOR i FROM LWB array TO size DO
|
||||
IF array[i+1] < array[i] THEN
|
||||
swap(array[i:i+1]);
|
||||
sorted := FALSE
|
||||
FI
|
||||
OD;
|
||||
NOT sorted
|
||||
DO SKIP OD
|
||||
);
|
||||
|
||||
main:(
|
||||
[10]INT random := (1,6,3,5,2,9,8,4,7,0);
|
||||
|
||||
printf(($"Before: "10(g(3))l$,random));
|
||||
sort(random);
|
||||
printf(($"After: "10(g(3))l$,random))
|
||||
)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
{ # read every line into an array
|
||||
line[NR] = $0
|
||||
}
|
||||
END { # sort it with bubble sort
|
||||
do {
|
||||
haschanged = 0
|
||||
for(i=1; i < NR; i++) {
|
||||
if ( line[i] > line[i+1] ) {
|
||||
t = line[i]
|
||||
line[i] = line[i+1]
|
||||
line[i+1] = t
|
||||
haschanged = 1
|
||||
}
|
||||
}
|
||||
} while ( haschanged == 1 )
|
||||
# print it
|
||||
for(i=1; i <= NR; i++) {
|
||||
print line[i]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
public function bubbleSort(toSort:Array):Array
|
||||
{
|
||||
var changed:Boolean = false;
|
||||
|
||||
while (!changed)
|
||||
{
|
||||
changed = true;
|
||||
|
||||
for (var i:int = 0; i < toSort.length - 1; i++)
|
||||
{
|
||||
if (toSort[i] > toSort[i + 1])
|
||||
{
|
||||
var tmp:int = toSort[i];
|
||||
toSort[i] = toSort[i + 1];
|
||||
toSort[i + 1] = tmp;
|
||||
|
||||
changed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return toSort;
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
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;
|
||||
end Bubble_Sort;
|
||||
|
||||
-- Example of usage:
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Bubble_Sort;
|
||||
procedure Main is
|
||||
type Arr is array (Positive range <>) of Integer;
|
||||
procedure Sort is new
|
||||
Bubble_Sort
|
||||
(Element => Integer,
|
||||
Index => Positive,
|
||||
Arr => Arr);
|
||||
A : Arr := (1, 3, 256, 0, 3, 4, -1);
|
||||
begin
|
||||
Sort (A);
|
||||
for J in A'Range loop
|
||||
Put (Integer'Image (A (J)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end Main;
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
var =
|
||||
(
|
||||
dog
|
||||
cat
|
||||
pile
|
||||
abc
|
||||
)
|
||||
MsgBox % bubblesort(var)
|
||||
|
||||
bubblesort(var) ; each line of var is an element of the array
|
||||
{
|
||||
StringSplit, array, var, `n
|
||||
hasChanged = 1
|
||||
size := array0
|
||||
While hasChanged
|
||||
{
|
||||
hasChanged = 0
|
||||
Loop, % (size - 1)
|
||||
{
|
||||
i := array%A_Index%
|
||||
aj := A_Index + 1
|
||||
j := array%aj%
|
||||
If (j < i)
|
||||
{
|
||||
temp := array%A_Index%
|
||||
array%A_Index% := array%aj%
|
||||
array%aj% := temp
|
||||
hasChanged = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Loop, % size
|
||||
sorted .= array%A_Index% . "`n"
|
||||
Return sorted
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
DO
|
||||
changed = 0
|
||||
FOR I = 1 to size -1
|
||||
IF nums(I) > nums(I + 1) THEN
|
||||
tmp = nums(I)
|
||||
nums(I) = nums(I + 1)
|
||||
nums(I + 1) = tmp
|
||||
changed = 1
|
||||
END IF
|
||||
NEXT
|
||||
LOOP WHILE(NOT changed)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
Dim a(11): ordered=false
|
||||
print "Original set"
|
||||
For n = 0 to 9
|
||||
a[n]=int(rand*20+1)
|
||||
print a[n]+", ";
|
||||
next n
|
||||
#algorithm
|
||||
while ordered=false
|
||||
ordered=true
|
||||
For n = 0 to 9
|
||||
if a[n]> a[n+1] then
|
||||
x=a[n]
|
||||
a[n]=a[n+1]
|
||||
a[n+1]=x
|
||||
ordered=false
|
||||
end if
|
||||
next n
|
||||
end while
|
||||
|
||||
print
|
||||
print "Ordered set"
|
||||
For n = 1 to 10
|
||||
print a[n]+", ";
|
||||
next n
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
DIM test(9)
|
||||
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
|
||||
PROCbubblesort(test(), 10)
|
||||
FOR i% = 0 TO 9
|
||||
PRINT test(i%) ;
|
||||
NEXT
|
||||
PRINT
|
||||
END
|
||||
|
||||
DEF PROCbubblesort(a(), n%)
|
||||
LOCAL i%, l%
|
||||
REPEAT
|
||||
l% = 0
|
||||
FOR i% = 1 TO n%-1
|
||||
IF a(i%-1) > a(i%) THEN
|
||||
SWAP a(i%-1),a(i%)
|
||||
l% = i%
|
||||
ENDIF
|
||||
NEXT
|
||||
n% = l%
|
||||
UNTIL l% = 0
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
template< typename ARRAY_TYPE, typename INDEX_TYPE >
|
||||
void bubble_sort(ARRAY_TYPE array[], INDEX_TYPE size)
|
||||
{
|
||||
bool done = false;
|
||||
|
||||
while ( ! done)
|
||||
{
|
||||
done = true;
|
||||
for (INDEX_TYPE i = 0 ; i < size-1 ; i++)
|
||||
{
|
||||
if (array[i] > array[i+1])
|
||||
{
|
||||
done = false;
|
||||
std::swap(array[i], array[i+1]);
|
||||
}
|
||||
}
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
template< typename TYPE >
|
||||
void print(TYPE val)
|
||||
{
|
||||
std::cout << val << " ";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int array[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
|
||||
bubble_sort(array, 10);
|
||||
std::for_each (&array[0], &array[10], print<int>);
|
||||
std::cout << std::endl;
|
||||
|
||||
//But in real life...
|
||||
int data[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
|
||||
std::sort(data, data+10);
|
||||
std::for_each (data, data+10, print<int>);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
void bubble_sort(int *a, int n) {
|
||||
int j, t = 1;
|
||||
while (n-- && t)
|
||||
for (j = t = 0; j < n; j++) {
|
||||
if (a[j] <= a[j + 1]) continue;
|
||||
t = a[j], a[j] = a[j + 1], a[j + 1] = t;
|
||||
t=1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
bubble_sort(a, sizeof(a) / sizeof(a[0]));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
# bubble_sort(var [value1 value2...]) sorts a list of integers.
|
||||
function(bubble_sort var)
|
||||
math(EXPR last "${ARGC} - 1") # Prepare to sort ARGV[1]..ARGV[last].
|
||||
set(again YES)
|
||||
while(again)
|
||||
set(again NO)
|
||||
math(EXPR last "${last} - 1") # Decrement last index.
|
||||
foreach(index RANGE 1 ${last}) # Loop for each index.
|
||||
math(EXPR index_plus_1 "${index} + 1")
|
||||
set(a "${ARGV${index}}") # a = ARGV[index]
|
||||
set(b "${ARGV${index_plus_1}}") # b = ARGV[index + 1]
|
||||
if(a GREATER "${b}") # If a > b...
|
||||
set(ARGV${index} "${b}") # ...then swap a, b
|
||||
set(ARGV${index_plus_1} "${a}") # inside ARGV.
|
||||
set(again YES)
|
||||
endif()
|
||||
endforeach(index)
|
||||
endwhile()
|
||||
|
||||
set(answer)
|
||||
math(EXPR last "${ARGC} - 1")
|
||||
foreach(index RANGE 1 "${last}")
|
||||
list(APPEND answer "${ARGV${index}}")
|
||||
endforeach(index)
|
||||
set("${var}" "${answer}" PARENT_SCOPE)
|
||||
endfunction(bubble_sort)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
bubble_sort(result 33 11 44 22 66 55)
|
||||
message(STATUS "${result}")
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. BUBBLESORT.
|
||||
AUTHOR. DAVE STRATFORD.
|
||||
DATE-WRITTEN. MARCH 2010.
|
||||
INSTALLATION. HEXAGON SYSTEMS LIMITED.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SOURCE-COMPUTER. ICL VME.
|
||||
OBJECT-COMPUTER. ICL VME.
|
||||
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT FA-INPUT-FILE ASSIGN FL01.
|
||||
SELECT FB-OUTPUT-FILE ASSIGN FL02.
|
||||
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
|
||||
FD FA-INPUT-FILE.
|
||||
01 FA-INPUT-REC.
|
||||
03 FA-DATA PIC S9(6).
|
||||
|
||||
FD FB-OUTPUT-FILE.
|
||||
01 FB-OUTPUT-REC PIC S9(6).
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WA-IDENTITY.
|
||||
03 WA-PROGNAME PIC X(10) VALUE "BUBBLESORT".
|
||||
03 WA-VERSION PIC X(6) VALUE "000001".
|
||||
|
||||
01 WB-TABLE.
|
||||
03 WB-ENTRY PIC 9(8) COMP SYNC OCCURS 100000
|
||||
INDEXED BY WB-IX-1.
|
||||
|
||||
01 WC-VARS.
|
||||
03 WC-SIZE PIC S9(8) COMP SYNC.
|
||||
03 WC-TEMP PIC S9(8) COMP SYNC.
|
||||
03 WC-END PIC S9(8) COMP SYNC.
|
||||
03 WC-LAST-CHANGE PIC S9(8) COMP SYNC.
|
||||
|
||||
01 WF-CONDITION-FLAGS.
|
||||
03 WF-EOF-FLAG PIC X.
|
||||
88 END-OF-FILE VALUE "Y".
|
||||
03 WF-EMPTY-FILE-FLAG PIC X.
|
||||
88 EMPTY-FILE VALUE "Y".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
A-MAIN SECTION.
|
||||
A-000.
|
||||
PERFORM B-INITIALISE.
|
||||
IF NOT EMPTY-FILE
|
||||
PERFORM C-SORT.
|
||||
PERFORM D-FINISH.
|
||||
|
||||
A-999.
|
||||
STOP RUN.
|
||||
|
||||
B-INITIALISE SECTION.
|
||||
B-000.
|
||||
DISPLAY "*** " WA-PROGNAME " VERSION "
|
||||
WA-VERSION " STARTING ***".
|
||||
|
||||
MOVE ALL "N" TO WF-CONDITION-FLAGS.
|
||||
OPEN INPUT FA-INPUT-FILE.
|
||||
SET WB-IX-1 TO 0.
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG
|
||||
WF-EMPTY-FILE-FLAG.
|
||||
|
||||
PERFORM BA-READ-INPUT UNTIL END-OF-FILE.
|
||||
|
||||
CLOSE FA-INPUT-FILE.
|
||||
|
||||
SET WC-SIZE TO WB-IX-1.
|
||||
|
||||
B-999.
|
||||
EXIT.
|
||||
|
||||
BA-READ-INPUT SECTION.
|
||||
BA-000.
|
||||
SET WB-IX-1 UP BY 1.
|
||||
MOVE FA-DATA TO WB-ENTRY(WB-IX-1).
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG.
|
||||
|
||||
BA-999.
|
||||
EXIT.
|
||||
|
||||
C-SORT SECTION.
|
||||
C-000.
|
||||
DISPLAY "SORT STARTING".
|
||||
|
||||
MOVE WC-SIZE TO WC-END.
|
||||
PERFORM E-BUBBLE UNTIL WC-END = 1.
|
||||
|
||||
DISPLAY "SORT FINISHED".
|
||||
|
||||
C-999.
|
||||
EXIT.
|
||||
|
||||
D-FINISH SECTION.
|
||||
D-000.
|
||||
OPEN OUTPUT FB-OUTPUT-FILE.
|
||||
SET WB-IX-1 TO 1.
|
||||
|
||||
PERFORM DA-WRITE-OUTPUT UNTIL WB-IX-1 > WC-SIZE.
|
||||
|
||||
CLOSE FB-OUTPUT-FILE.
|
||||
|
||||
DISPLAY "*** " WA-PROGNAME " FINISHED ***".
|
||||
|
||||
D-999.
|
||||
EXIT.
|
||||
|
||||
DA-WRITE-OUTPUT SECTION.
|
||||
DA-000.
|
||||
WRITE FB-OUTPUT-REC FROM WB-ENTRY(WB-IX-1).
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
DA-999.
|
||||
EXIT.
|
||||
|
||||
E-BUBBLE SECTION.
|
||||
E-000.
|
||||
MOVE 1 TO WC-LAST-CHANGE.
|
||||
|
||||
PERFORM F-PASS VARYING WB-IX-1 FROM 1 BY 1
|
||||
UNTIL WB-IX-1 = WC-END.
|
||||
|
||||
MOVE WC-LAST-CHANGE TO WC-END.
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
||||
F-PASS SECTION.
|
||||
F-000.
|
||||
IF WB-ENTRY(WB-IX-1) > WB-ENTRY(WB-IX-1 + 1)
|
||||
SET WC-LAST-CHANGE TO WB-IX-1
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WC-TEMP
|
||||
MOVE WB-ENTRY(WB-IX-1 + 1) TO WB-ENTRY(WB-IX-1)
|
||||
MOVE WC-TEMP TO WB-ENTRY(WB-IX-1 + 1).
|
||||
|
||||
F-999.
|
||||
EXIT.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Start :: {Int}
|
||||
Start = bsort {x \\ x <- [100,99..1]}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(ns bubblesort
|
||||
(:import java.util.ArrayList))
|
||||
|
||||
(defn bubble-sort
|
||||
"Sort in-place.
|
||||
arr must implement the Java List interface and should support
|
||||
random access, e.g. an ArrayList."
|
||||
([arr] (bubble-sort compare arr))
|
||||
([cmp arr]
|
||||
(letfn [(swap! [i j]
|
||||
(let [t (.get arr i)]
|
||||
(doto arr
|
||||
(.set i (.get arr j))
|
||||
(.set j t))))
|
||||
(sorter [stop-i]
|
||||
(let [changed (atom false)]
|
||||
(doseq [i (range stop-i)]
|
||||
(if (pos? (cmp (.get arr i) (.get arr (inc i))))
|
||||
(do
|
||||
(swap! i (inc i))
|
||||
(reset! changed true))))
|
||||
@changed))]
|
||||
(doseq [stop-i (range (dec (.size arr)) -1 -1)
|
||||
:while (sorter stop-i)])
|
||||
arr)))
|
||||
|
||||
(println (bubble-sort (ArrayList. [10 9 8 7 6 5 4 3 2 1])))
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(defn- bubble-step
|
||||
"was-changed: whether any elements prior to the current first element
|
||||
were swapped;
|
||||
returns a two-element vector [partially-sorted-sequence is-sorted]"
|
||||
[less? xs was-changed]
|
||||
(if (< (count xs) 2)
|
||||
[xs (not was-changed)]
|
||||
(let [[x1 x2 & xr] xs
|
||||
first-is-smaller (less? x1 x2)
|
||||
is-changed (or was-changed (not first-is-smaller))
|
||||
[smaller larger] (if first-is-smaller [x1 x2] [x2 x1])
|
||||
[result is-sorted] (bubble-step
|
||||
less? (cons larger xr) is-changed)]
|
||||
[(cons smaller result) is-sorted])))
|
||||
|
||||
(defn bubble-sort
|
||||
"Takes an optional less-than predicate and a sequence.
|
||||
Returns the sorted sequence.
|
||||
Very inefficient (O(n²))"
|
||||
([xs] (bubble-sort <= xs))
|
||||
([less? xs]
|
||||
(let [[result is-sorted] (bubble-step less? xs false)]
|
||||
(if is-sorted
|
||||
result
|
||||
(recur less? result)))))
|
||||
|
||||
(println (bubble-sort [10 9 8 7 6 5 4 3 2 1]))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(defun bubble-sort (sequence &optional (compare #'<))
|
||||
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
|
||||
(loop with sorted = nil until sorted do
|
||||
(setf sorted t)
|
||||
(loop for a below (1- (length sequence)) do
|
||||
(unless (funcall compare (elt sequence a)
|
||||
(elt sequence (1+ a)))
|
||||
(rotatef (elt sequence a)
|
||||
(elt sequence (1+ a)))
|
||||
(setf sorted nil)))))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(bubble-sort (list 5 4 3 2 1))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(defun bubble-sort-vector (vector predicate &aux (len (1- (length vector))))
|
||||
(do ((swapped t)) ((not swapped) vector)
|
||||
(setf swapped nil)
|
||||
(do ((i (min 0 len) (1+ i))) ((eql i len))
|
||||
(when (funcall predicate (aref vector (1+ i)) (aref vector i))
|
||||
(rotatef (aref vector i) (aref vector (1+ i)))
|
||||
(setf swapped t)))))
|
||||
|
||||
(defun bubble-sort-list (list predicate)
|
||||
(do ((swapped t)) ((not swapped) list)
|
||||
(setf swapped nil)
|
||||
(do ((list list (rest list))) ((endp (rest list)))
|
||||
(when (funcall predicate (second list) (first list))
|
||||
(rotatef (first list) (second list))
|
||||
(setf swapped t)))))
|
||||
|
||||
(defun bubble-sort (sequence predicate)
|
||||
(etypecase sequence
|
||||
(list (bubble-sort-list sequence predicate))
|
||||
(vector (bubble-sort-vector sequence predicate))))
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void bubbleSort(T)(T[] data) pure nothrow {
|
||||
auto itemCount = data.length;
|
||||
bool hasChanged = false;
|
||||
|
||||
do {
|
||||
hasChanged = false;
|
||||
itemCount--;
|
||||
foreach (immutable i; 0 .. itemCount)
|
||||
if (data[i] > data[i + 1]) {
|
||||
swap(data[i], data[i + 1]);
|
||||
hasChanged = true;
|
||||
}
|
||||
} while (hasChanged);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
|
||||
array.bubbleSort();
|
||||
writeln(array);
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
program TestBubbleSort;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{.$DEFINE DYNARRAY} // remove '.' to compile with dynamic array
|
||||
|
||||
type
|
||||
TItem = Integer; // declare ordinal type for array item
|
||||
{$IFDEF DYNARRAY}
|
||||
TArray = array of TItem; // dynamic array
|
||||
{$ELSE}
|
||||
TArray = array[0..15] of TItem; // static array
|
||||
{$ENDIF}
|
||||
|
||||
procedure BubbleSort(var A: TArray);
|
||||
var
|
||||
Item: TItem;
|
||||
K, L, J: Integer;
|
||||
|
||||
begin
|
||||
L:= Low(A) + 1;
|
||||
repeat
|
||||
K:= High(A);
|
||||
for J:= High(A) downto L do begin
|
||||
if A[J - 1] > A[J] then begin
|
||||
Item:= A[J - 1];
|
||||
A[J - 1]:= A[J];
|
||||
A[J]:= Item;
|
||||
K:= J;
|
||||
end;
|
||||
end;
|
||||
L:= K + 1;
|
||||
until L > High(A);
|
||||
end;
|
||||
|
||||
var
|
||||
A: TArray;
|
||||
I: Integer;
|
||||
|
||||
begin
|
||||
{$IFDEF DYNARRAY}
|
||||
SetLength(A, 16);
|
||||
{$ENDIF}
|
||||
for I:= Low(A) to High(A) do
|
||||
A[I]:= Random(100);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
BubbleSort(A);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def bubbleSort(target) {
|
||||
__loop(fn {
|
||||
var changed := false
|
||||
for i in 0..(target.size() - 2) {
|
||||
def [a, b] := target(i, i + 2)
|
||||
if (a > b) {
|
||||
target(i, i + 2) := [b, a]
|
||||
changed := true
|
||||
}
|
||||
}
|
||||
changed
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
class
|
||||
APPLICATION
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
make
|
||||
-- Create and print sorted set
|
||||
do
|
||||
create my_set.make
|
||||
my_set.put_front (2)
|
||||
my_set.put_front (6)
|
||||
my_set.put_front (1)
|
||||
my_set.put_front (5)
|
||||
my_set.put_front (3)
|
||||
my_set.put_front (9)
|
||||
my_set.put_front (8)
|
||||
my_set.put_front (4)
|
||||
my_set.put_front (10)
|
||||
my_set.put_front (7)
|
||||
print ("Before: ")
|
||||
across my_set as ic loop print (ic.item.out + " ") end
|
||||
print ("%NAfter : ")
|
||||
my_set.sort
|
||||
across my_set as ic loop print (ic.item.out + " ") end
|
||||
end
|
||||
|
||||
my_set: MY_SORTED_SET [INTEGER]
|
||||
-- Set to be sorted
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
class
|
||||
MY_SORTED_SET [G -> COMPARABLE]
|
||||
inherit
|
||||
TWO_WAY_SORTED_SET [G]
|
||||
redefine
|
||||
sort
|
||||
end
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
sort
|
||||
-- Sort with bubble sort
|
||||
local
|
||||
l_unchanged: BOOLEAN
|
||||
l_item_count: INTEGER
|
||||
l_temp: G
|
||||
do
|
||||
from
|
||||
l_item_count := count
|
||||
until
|
||||
l_unchanged
|
||||
loop
|
||||
l_unchanged := True
|
||||
l_item_count := l_item_count - 1
|
||||
across 1 |..| l_item_count as ic loop
|
||||
if Current [ic.item] > Current [ic.item + 1] then
|
||||
l_temp := Current [ic.item]
|
||||
Current [ic.item] := Current [ic.item + 1]
|
||||
Current [ic.item + 1] := l_temp
|
||||
l_unchanged := False
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
function bubble_sort(sequence s)
|
||||
object tmp
|
||||
integer changed
|
||||
for j = length(s) to 1 by -1 do
|
||||
changed = 0
|
||||
for i = 1 to j-1 do
|
||||
if compare(s[i], s[i+1]) > 0 then
|
||||
tmp = s[i]
|
||||
s[i] = s[i+1]
|
||||
s[i+1] = tmp
|
||||
changed = 1
|
||||
end if
|
||||
end for
|
||||
if not changed then
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
include misc.e
|
||||
constant s = {4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}
|
||||
|
||||
puts(1,"Before: ")
|
||||
pretty_print(1,s,{2})
|
||||
puts(1,"\nAfter: ")
|
||||
pretty_print(1,bubble_sort(s),{2})
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
USING: fry kernel locals math math.order sequences
|
||||
sequences.private ;
|
||||
IN: rosetta.bubble
|
||||
|
||||
<PRIVATE
|
||||
|
||||
:: ?exchange ( i seq quot -- ? )
|
||||
i i 1 + [ seq nth-unsafe ] bi@ quot call +gt+ = :> doit?
|
||||
doit? [ i i 1 + seq exchange ] when
|
||||
doit? ; inline
|
||||
|
||||
: 1pass ( seq quot -- ? )
|
||||
[ [ length 1 - iota ] keep ] dip
|
||||
'[ _ _ ?exchange ] [ or ] map-reduce ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: sort! ( seq quot -- )
|
||||
over empty?
|
||||
[ 2drop ] [ '[ _ _ 1pass ] loop ] if ; inline
|
||||
|
||||
: natural-sort! ( seq -- )
|
||||
[ <=> ] sort! ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
10 [ 10000 random ] replicate
|
||||
[ "Before: " write . ]
|
||||
[ "Natural: " write [ natural-sort! ] keep . ]
|
||||
[ "Reverse: " write [ [ >=< ] sort! ] keep . ] tri
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
defer bubble-test
|
||||
' > is bubble-test
|
||||
|
||||
: bubble { addr cnt -- }
|
||||
cnt 1 do
|
||||
addr cnt i - cells bounds do
|
||||
i 2@ bubble-test if i 2@ swap i 2! then
|
||||
cell +loop
|
||||
loop ;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
: bubble ( addr cnt -- )
|
||||
dup 1 do
|
||||
2dup i - cells bounds do
|
||||
i 2@ bubble-test if i 2@ swap i 2! then
|
||||
cell +loop
|
||||
loop ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
: bubble ( addr len -- )
|
||||
begin
|
||||
1- 2dup true -rot ( sorted addr len-1 )
|
||||
cells bounds ?do
|
||||
i 2@ bubble-test if
|
||||
i 2@ swap i 2!
|
||||
drop false ( mark unsorted )
|
||||
then
|
||||
cell +loop ( sorted )
|
||||
until 2drop ;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
SUBROUTINE Bubble_Sort(a)
|
||||
REAL, INTENT(in out), DIMENSION(:) :: a
|
||||
REAL :: temp
|
||||
INTEGER :: i, j
|
||||
LOGICAL :: swapped = .TRUE.
|
||||
|
||||
DO j = SIZE(a)-1, 1, -1
|
||||
swapped = .FALSE.
|
||||
DO i = 1, j
|
||||
IF (a(i) > a(i+1)) THEN
|
||||
temp = a(i)
|
||||
a(i) = a(i+1)
|
||||
a(i+1) = temp
|
||||
swapped = .TRUE.
|
||||
END IF
|
||||
END DO
|
||||
IF (.NOT. swapped) EXIT
|
||||
END DO
|
||||
END SUBROUTINE Bubble_Sort
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
fmt.Println("unsorted:", list)
|
||||
|
||||
bubblesort(list)
|
||||
fmt.Println("sorted! ", list)
|
||||
}
|
||||
|
||||
func bubblesort(a []int) {
|
||||
for itemCount := len(a) - 1; ; itemCount-- {
|
||||
hasChanged := false
|
||||
for index := 0; index < itemCount; index++ {
|
||||
if a[index] > a[index+1] {
|
||||
a[index], a[index+1] = a[index+1], a[index]
|
||||
hasChanged = true
|
||||
}
|
||||
}
|
||||
if hasChanged == false {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
|
||||
fmt.Println("unsorted:", list)
|
||||
|
||||
bubblesort(sort.IntSlice(list))
|
||||
fmt.Println("sorted! ", list)
|
||||
}
|
||||
|
||||
func bubblesort(a sort.Interface) {
|
||||
for itemCount := a.Len() - 1; ; itemCount-- {
|
||||
hasChanged := false
|
||||
for index := 0; index < itemCount; index++ {
|
||||
if a.Less(index+1, index) {
|
||||
a.Swap(index, index+1)
|
||||
hasChanged = true
|
||||
}
|
||||
}
|
||||
if !hasChanged {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def makeSwap = { a, i, j = i+1 -> print "."; a[[i,j]] = a[[j,i]] }
|
||||
|
||||
def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find { it }.each { makeSwap(a, i, j) } }
|
||||
|
||||
def bubbleSort = { list ->
|
||||
boolean swapped = true
|
||||
while (swapped) { swapped = (1..<list.size()).any { checkSwap(list, it-1) } }
|
||||
list
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println bubbleSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4])
|
||||
println bubbleSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1])
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
bsort :: Ord a => [a] -> [a]
|
||||
bsort s = case _bsort s of
|
||||
t | t == s -> t
|
||||
| otherwise -> bsort t
|
||||
where _bsort (x:x2:xs) | x > x2 = x2:(_bsort (x:xs))
|
||||
| otherwise = x:(_bsort (x2:xs))
|
||||
_bsort s = s
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import Data.Maybe (fromMaybe)
|
||||
import Control.Monad
|
||||
|
||||
bsort :: Ord a => [a] -> [a]
|
||||
bsort s = maybe s bsort $ _bsort s
|
||||
where _bsort (x:x2:xs) = if x > x2
|
||||
then Just $ x2 : fromMaybe (x:xs) (_bsort $ x:xs)
|
||||
else liftM (x:) $ _bsort (x2:xs)
|
||||
_bsort _ = Nothing
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
SUBROUTINE Bubble_Sort(a)
|
||||
REAL :: a(1)
|
||||
|
||||
DO j = LEN(a)-1, 1, -1
|
||||
swapped = 0
|
||||
DO i = 1, j
|
||||
IF (a(i) > a(i+1)) THEN
|
||||
temp = a(i)
|
||||
a(i) = a(i+1)
|
||||
a(i+1) = temp
|
||||
swapped = 1
|
||||
ENDIF
|
||||
ENDDO
|
||||
IF (swapped == 0) RETURN
|
||||
ENDDO
|
||||
END
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(bubblesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure bubblesort(X,op) #: return sorted list
|
||||
local i,swapped
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
swapped := 1
|
||||
while \swapped := &null do # the sort
|
||||
every i := 2 to *X do
|
||||
if op(X[i],X[i-1]) then
|
||||
X[i-1] :=: X[swapped := i]
|
||||
return X
|
||||
end
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
invocable all # for op
|
||||
|
||||
procedure sortop(op,X) #: select how to sort
|
||||
|
||||
op := case op of {
|
||||
"string": "<<"
|
||||
"numeric": "<"
|
||||
&null: if type(!X) == "string" then "<<" else "<"
|
||||
default: op
|
||||
}
|
||||
return proc(op, 2) | runerr(123, image(op))
|
||||
end
|
||||
|
||||
procedure cmp(a,b) #: example custom comparison procedure
|
||||
return a < b # Imagine a complex comparison test here!
|
||||
end
|
||||
|
||||
procedure demosort(sortproc,L,s) # demonstrate sort on L and s
|
||||
|
||||
write("Sorting Demo using ",image(sortproc))
|
||||
writes(" on list : ")
|
||||
writex(L)
|
||||
displaysort(sortproc,L) # default string sort
|
||||
displaysort(sortproc,L,"numeric") # explicit numeric sort
|
||||
displaysort(sortproc,L,"string") # explicit string sort
|
||||
displaysort(sortproc,L,">>") # descending string sort
|
||||
displaysort(sortproc,L,">") # descending numeric sort
|
||||
displaysort(sortproc,L,cmp) # ascending custom comparison
|
||||
displaysort(sortproc,L,"cmp") # ascending custom comparison
|
||||
|
||||
writes(" on string : ")
|
||||
writex(s)
|
||||
displaysort(sortproc,s) # sort characters in a string
|
||||
write()
|
||||
return
|
||||
end
|
||||
|
||||
procedure displaysort(sortproc,X,op) #: helper to show sort behavior
|
||||
local t,SX
|
||||
writes(" with op = ",left(image(op)||":",15))
|
||||
X := copy(X)
|
||||
t := &time
|
||||
SX := sortproc(X,op)
|
||||
writex(SX," (",&time - t," ms)")
|
||||
return
|
||||
end
|
||||
|
||||
procedure writex(X,suf[]) #: helper for displaysort
|
||||
if type(X) == "string" then
|
||||
writes(image(X))
|
||||
else {
|
||||
writes("[")
|
||||
every writes(" ",image(!X))
|
||||
writes(" ]")
|
||||
}
|
||||
every writes(!suf)
|
||||
write()
|
||||
return
|
||||
end
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
List do(
|
||||
bubblesort := method(
|
||||
t := true
|
||||
while( t,
|
||||
t := false
|
||||
for( j, 0, self size - 2,
|
||||
if( self at( j ) start > self at( j+1 ) start,
|
||||
self swapIndices( j,j+1 )
|
||||
t := true
|
||||
)
|
||||
)
|
||||
)
|
||||
return( self )
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1 @@
|
|||
bubbleSort=: (([ (<. , >.) {.@]) , }.@])/^:_
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
?. 10 $ 10
|
||||
4 6 8 6 5 8 6 6 6 9
|
||||
bubbleSort ?. 10 $ 10
|
||||
4 5 6 6 6 6 6 8 8 9
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
public static <E extends Comparable<? super E>> void bubbleSort(E[] comparable) {
|
||||
boolean changed = false;
|
||||
do {
|
||||
changed = false;
|
||||
for (int a = 0; a < comparable.length - 1; a++) {
|
||||
if (comparable[a].compareTo(comparable[a + 1]) > 0) {
|
||||
E tmp = comparable[a];
|
||||
comparable[a] = comparable[a + 1];
|
||||
comparable[a + 1] = tmp;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
if (comparable[a].compareTo(comparable[b]) < 0){
|
||||
//same swap code as before
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Array.prototype.bubblesort = function() {
|
||||
var done = false;
|
||||
while (!done) {
|
||||
done = true;
|
||||
for (var i = 1; i<this.length; i++) {
|
||||
if (this[i-1] > this[i]) {
|
||||
done = false;
|
||||
[this[i-1], this[i]] = [this[i], this[i-1]]
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Array.prototype.bubblesort = function() {
|
||||
var done = false;
|
||||
while (! done) {
|
||||
done = true;
|
||||
for (var i = 1; i < this.length; i++) {
|
||||
if (this[i - 1] > this[i]) {
|
||||
done = false;
|
||||
var tmp = this[i - 1];
|
||||
this[i - 1] = this[i];
|
||||
this[i] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var my_arr = ["G", "F", "C", "A", "B", "E", "D"];
|
||||
my_arr.bubblesort();
|
||||
print(my_arr);
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
itemCount = 20
|
||||
dim item(itemCount)
|
||||
for i = 1 to itemCount
|
||||
item(i) = int(rnd(1) * 100)
|
||||
next i
|
||||
print "Before Sort"
|
||||
for i = 1 to itemCount
|
||||
print item(i)
|
||||
next i
|
||||
print: print
|
||||
counter = itemCount
|
||||
do
|
||||
hasChanged = 0
|
||||
for i = 1 to counter - 1
|
||||
if item(i) > item(i + 1) then
|
||||
temp = item(i)
|
||||
item(i) = item(i + 1)
|
||||
item(i + 1) = temp
|
||||
hasChanged = 1
|
||||
end if
|
||||
next i
|
||||
counter =counter -1
|
||||
loop while hasChanged = 1
|
||||
print "After Sort"
|
||||
for i = 1 to itemCount
|
||||
print item(i)
|
||||
next i
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
Section Header
|
||||
|
||||
+ name := BUBBLE_SORT;
|
||||
|
||||
- external := `#include <time.h>`;
|
||||
|
||||
Section Public
|
||||
|
||||
- main <- (
|
||||
+ a : ARRAY(INTEGER);
|
||||
|
||||
a := ARRAY(INTEGER).create 0 to 100;
|
||||
`srand(time(NULL))`;
|
||||
0.to 100 do { i : INTEGER;
|
||||
a.put `rand()`:INTEGER to i;
|
||||
};
|
||||
|
||||
bubble a;
|
||||
|
||||
a.foreach { item : INTEGER;
|
||||
item.print;
|
||||
'\n'.print;
|
||||
};
|
||||
);
|
||||
|
||||
- bubble a : ARRAY(INTEGER) <- (
|
||||
+ lower, size, t : INTEGER;
|
||||
+ sorted : BOOLEAN;
|
||||
lower := a.lower;
|
||||
size := a.upper - lower + 1;
|
||||
{
|
||||
sorted := TRUE;
|
||||
size := size - 1;
|
||||
0.to (size - 1) do { i : INTEGER;
|
||||
(a.item(lower + i + 1) < a.item(lower + i)).if {
|
||||
t := a.item(lower + i + 1);
|
||||
a.put (a.item(lower + i)) to (lower + i + 1);
|
||||
a.put t to (lower + i);
|
||||
sorted := FALSE;
|
||||
};
|
||||
};
|
||||
}.do_while {!sorted};
|
||||
);
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
function bubbleSort(A)
|
||||
local itemCount=#A
|
||||
local hasChanged
|
||||
repeat
|
||||
hasChanged = false
|
||||
itemCount=itemCount - 1
|
||||
for i = 1, itemCount do
|
||||
if A[i] > A[i + 1] then
|
||||
A[i], A[i + 1] = A[i + 1], A[i]
|
||||
hasChanged = true
|
||||
end
|
||||
end
|
||||
until hasChanged == false
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
list = { 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97 }
|
||||
bubbleSort(list)
|
||||
for i, j in pairs(list) do
|
||||
print(j)
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
bsort(a) = if iseod(first a) then a else
|
||||
follow(bsort(allbutlast(b)),last(b)) fi
|
||||
where
|
||||
b = bubble(a);
|
||||
bubble(a) = smaller(max, next a)
|
||||
where
|
||||
max = first a fby larger(max, next a);
|
||||
larger(x,y) = if iseod(y) then y elseif x
|
||||
end;
|
||||
follow(x,y) = if xdone then y upon xdone else x fi
|
||||
where
|
||||
xdone = iseod x fby xdone or iseod x;
|
||||
end;
|
||||
last(x) = (x asa iseod next x) fby eod;
|
||||
allbutlast(x) = if not iseod(next x) then x else eod fi;
|
||||
end
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
divert(-1)
|
||||
|
||||
define(`randSeed',141592653)
|
||||
define(`setRand',
|
||||
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')
|
||||
define(`rand_t',`eval(randSeed^(randSeed>>13))')
|
||||
define(`random',
|
||||
`define(`randSeed',eval((rand_t^(rand_t<<18))&0x7fffffff))randSeed')
|
||||
|
||||
define(`set',`define(`$1[$2]',`$3')')
|
||||
define(`get',`defn(`$1[$2]')')
|
||||
define(`new',`set($1,size,0)')
|
||||
dnl for the heap calculations, it's easier if origin is 0, so set value first
|
||||
define(`append',
|
||||
`set($1,size,incr(get($1,size)))`'set($1,get($1,size),$2)')
|
||||
|
||||
dnl swap(<name>,<j>,<name>[<j>],<k>) using arg stack for the temporary
|
||||
define(`swap',`set($1,$2,get($1,$4))`'set($1,$4,$3)')
|
||||
|
||||
define(`deck',
|
||||
`new($1)for(`x',1,$2,
|
||||
`append(`$1',eval(random%100))')')
|
||||
define(`show',
|
||||
`for(`x',1,get($1,size),`get($1,x) ')')
|
||||
define(`for',
|
||||
`ifelse($#,0,``$0'',
|
||||
`ifelse(eval($2<=$3),1,
|
||||
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
|
||||
|
||||
define(`bubbleonce',
|
||||
`for(`x',1,$2,
|
||||
`ifelse(eval(get($1,x)>get($1,incr(x))),1,
|
||||
`swap($1,x,get($1,x),incr(x))`'1')')0')
|
||||
define(`bubbleupto',
|
||||
`ifelse(bubbleonce($1,$2),0,
|
||||
`',
|
||||
`bubbleupto($1,decr($2))')')
|
||||
define(`bubblesort',
|
||||
`bubbleupto($1,decr(get($1,size)))')
|
||||
|
||||
divert
|
||||
deck(`a',10)
|
||||
show(`a')
|
||||
bubblesort(`a')
|
||||
show(`a')
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
bubbleSort([5 3 8 4 9 7 6 2 1])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
fn bubbleSort arr =
|
||||
(
|
||||
while true do
|
||||
(
|
||||
changed = false
|
||||
for i in 1 to (arr.count - 1) do
|
||||
(
|
||||
if arr[i] > arr[i+1] then
|
||||
(
|
||||
swap arr[i] arr[i+1]
|
||||
changed = true
|
||||
)
|
||||
)
|
||||
if not changed then exit
|
||||
)
|
||||
arr
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
myArr = #(9, 8, 7, 6, 5, 4, 3, 2, 1)
|
||||
myArr = bubbleSort myArr
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
Ja IS $127
|
||||
|
||||
LOC Data_Segment
|
||||
DataSeg GREG @
|
||||
Array IS @-Data_Segment
|
||||
OCTA 999,200,125,1,1020,40,4,5,60,100
|
||||
ArrayLen IS (@-Array-Data_Segment)/8
|
||||
|
||||
NL IS @-Data_Segment
|
||||
BYTE #a,0
|
||||
LOC @+(8-@)&7
|
||||
|
||||
Buffer IS @-Data_Segment
|
||||
|
||||
|
||||
LOC #1000
|
||||
GREG @
|
||||
sorted IS $5
|
||||
i IS $6
|
||||
size IS $1
|
||||
a IS $0
|
||||
t IS $20
|
||||
t1 IS $21
|
||||
t2 IS $22
|
||||
% Input: $0 ptr to array, $1 its length (in octabyte)
|
||||
% Trashed: $5, $6, $1, $20, $21, $22
|
||||
BubbleSort SETL sorted,1 % sorted = true
|
||||
SUB size,size,1 % size--
|
||||
SETL i,0 % i = 0
|
||||
3H CMP t,i,size % i < size ?
|
||||
BNN t,1F % if false, end for loop
|
||||
8ADDU $12,i,a % compute addresses of the
|
||||
ADDU t,i,1 % octas a[i] and a[i+1]
|
||||
8ADDU $11,t,a
|
||||
LDO t1,$12,0 % get their values
|
||||
LDO t2,$11,0
|
||||
CMP t,t1,t2 % compare
|
||||
BN t,2F % if t1<t2, next
|
||||
STO t1,$11,0 % else swap them
|
||||
STO t2,$12,0
|
||||
SETL sorted,0 % sorted = false
|
||||
2H INCL i,1 % i++
|
||||
JMP 3B % next (for loop)
|
||||
1H PBZ sorted,BubbleSort % while sorted is false, loop
|
||||
GO Ja,Ja,0
|
||||
|
||||
% Help function (Print an octabyte)
|
||||
% Input: $0 (the octabyte)
|
||||
BufSize IS 64
|
||||
GREG @
|
||||
PrintInt8 ADDU t,DataSeg,Buffer % get buffer address
|
||||
ADDU t,t,BufSize % end of buffer
|
||||
SETL t1,0 % final 0 for Fputs
|
||||
STB t1,t,0
|
||||
1H SUB t,t,1 % t--
|
||||
DIV $0,$0,10 % ($0,rR) = divmod($0,10)
|
||||
GET t1,rR % get reminder
|
||||
INCL t1,'0' % turn it into ascii digit
|
||||
STB t1,t,0 % store it
|
||||
PBNZ $0,1B % if $0 /= 0, loop
|
||||
OR $255,t,0 % $255 = t
|
||||
TRAP 0,Fputs,StdOut
|
||||
GO Ja,Ja,0 % print and return
|
||||
|
||||
|
||||
Main ADDU $0,DataSeg,Array % $0 = Array address
|
||||
SETL $1,ArrayLen % $1 = Array Len
|
||||
GO Ja,BubbleSort % BubbleSort it
|
||||
SETL $4,ArrayLen % $4 = ArrayLen
|
||||
ADDU $3,DataSeg,Array % $3 = Array address
|
||||
2H BZ $4,1F % if $4 == 0, break
|
||||
LDO $0,$3,0 % $0 = * ($3 + 0)
|
||||
GO Ja,PrintInt8 % print the octa
|
||||
ADDU $255,DataSeg,NL % add a trailing newline
|
||||
TRAP 0,Fputs,StdOut
|
||||
ADDU $3,$3,8 % next octa
|
||||
SUB $4,$4,1 % $4--
|
||||
JMP 2B % loop
|
||||
1H XOR $255,$255,$255
|
||||
TRAP 0,Halt,0 % exit(0)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
bubbleSort[{w___, x_, y_, z___}] /; x > y := bubbleSort[{w, y, x, z}]
|
||||
bubbleSort[sortedList_] := sortedList
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
PROCEDURE BubbleSort(VAR a: ARRAY OF INTEGER);
|
||||
VAR
|
||||
changed: BOOLEAN;
|
||||
temp, count, i: INTEGER;
|
||||
BEGIN
|
||||
count := HIGH(a);
|
||||
REPEAT
|
||||
changed := FALSE;
|
||||
DEC(count);
|
||||
FOR i := 0 TO count DO
|
||||
IF a[i] > a[i+1] THEN
|
||||
temp := a[i];
|
||||
a[i] := a[i+1];
|
||||
a[i+1] := temp;
|
||||
changed := TRUE
|
||||
END
|
||||
END
|
||||
UNTIL NOT changed
|
||||
END BubbleSort;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
MODULE Bubble;
|
||||
|
||||
PROCEDURE Sort(VAR a: ARRAY OF INTEGER) =
|
||||
VAR sorted: BOOLEAN;
|
||||
temp, len: INTEGER := LAST(a);
|
||||
BEGIN
|
||||
WHILE NOT sorted DO
|
||||
sorted := TRUE;
|
||||
DEC(len);
|
||||
FOR i := FIRST(a) TO len DO
|
||||
IF a[i+1] < a[i] THEN
|
||||
temp := a[i];
|
||||
a[i] := a[i + 1];
|
||||
a[i + 1] := temp;
|
||||
sorted := FALSE;
|
||||
END;
|
||||
END;
|
||||
END;
|
||||
END Sort;
|
||||
END Bubble.
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Bubblesort
|
||||
{
|
||||
Bubblesort[T] (x : list[T]) : list[T]
|
||||
where T : IComparable
|
||||
{
|
||||
def isSorted(y)
|
||||
{
|
||||
|[_] => true
|
||||
|y1::y2::ys => (y1.CompareTo(y2) < 0) && isSorted(y2::ys)
|
||||
}
|
||||
|
||||
def sort(y)
|
||||
{
|
||||
|[y] => [y]
|
||||
|y1::y2::ys => if (y1.CompareTo(y2) < 0) y1::sort(y2::ys)
|
||||
else y2::sort(y1::ys)
|
||||
}
|
||||
|
||||
def loop(y)
|
||||
{
|
||||
if (isSorted(y)) y else {def z = sort(y); loop(z)}
|
||||
}
|
||||
|
||||
match(x)
|
||||
{
|
||||
|[] => []
|
||||
|_ => loop(x)
|
||||
}
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def empty = [];
|
||||
def single = [2];
|
||||
def several = [2, 6, 1, 7, 3, 9, 4];
|
||||
WriteLine(Bubblesort(empty));
|
||||
WriteLine(Bubblesort(single));
|
||||
WriteLine(Bubblesort(several));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Bubblesort
|
||||
{
|
||||
public static Bubblesort[T](this x : array[T]) : void
|
||||
where T : IComparable
|
||||
{
|
||||
mutable changed = false;
|
||||
def ln = x.Length;
|
||||
|
||||
do
|
||||
{
|
||||
changed = false;
|
||||
foreach (i in [0 .. (ln - 2)])
|
||||
{
|
||||
when (x[i].CompareTo(x[i + 1]) > 0)
|
||||
{
|
||||
x[i] <-> x[i + 1];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def several = array[2, 6, 1, 7, 3, 9, 4];
|
||||
several.Bubblesort();
|
||||
foreach (i in several)
|
||||
Write($"$i ");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
placesList = [String -
|
||||
"UK London", "US New York" -
|
||||
, "US Boston", "US Washington" -
|
||||
, "UK Washington", "US Birmingham" -
|
||||
, "UK Birmingham", "UK Boston" -
|
||||
]
|
||||
sortedList = bubbleSort(String[] Arrays.copyOf(placesList, placesList.length))
|
||||
|
||||
lists = [placesList, sortedList]
|
||||
loop ln = 0 to lists.length - 1
|
||||
cl = lists[ln]
|
||||
loop ct = 0 to cl.length - 1
|
||||
say cl[ct]
|
||||
end ct
|
||||
say
|
||||
end ln
|
||||
|
||||
return
|
||||
|
||||
method bubbleSort(list = String[]) public constant binary returns String[]
|
||||
|
||||
listLen = list.length
|
||||
loop i_ = 0 to listLen - 1
|
||||
loop j_ = i_ + 1 to listLen - 1
|
||||
if list[i_].compareTo(list[j_]) > 0 then do
|
||||
tmpstor = list[j_]
|
||||
list[j_] = list[i_]
|
||||
list[i_] = tmpstor
|
||||
end
|
||||
end j_
|
||||
end i_
|
||||
|
||||
return list
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
placesList = [String -
|
||||
"UK London", "US New York" -
|
||||
, "US Boston", "US Washington" -
|
||||
, "UK Washington", "US Birmingham" -
|
||||
, "UK Birmingham", "UK Boston" -
|
||||
]
|
||||
sortedList = bubbleSort(String[] Arrays.copyOf(placesList, placesList.length))
|
||||
|
||||
lists = [placesList, sortedList]
|
||||
loop ln = 0 to lists.length - 1
|
||||
cl = lists[ln]
|
||||
loop ct = 0 to cl.length - 1
|
||||
say cl[ct]
|
||||
end ct
|
||||
say
|
||||
end ln
|
||||
|
||||
return
|
||||
|
||||
method bubbleSort(item = String[]) public constant binary returns String[]
|
||||
|
||||
hasChanged = boolean
|
||||
itemCount = item.length
|
||||
loop label h_ until \hasChanged
|
||||
hasChanged = isFalse
|
||||
itemCount = itemCount - 1
|
||||
loop index = 0 to itemCount - 1
|
||||
if item[index].compareTo(item[index + 1]) > 0 then do
|
||||
swap = item[index]
|
||||
item[index] = item[index + 1]
|
||||
item[index + 1] = swap
|
||||
hasChanged = isTrue
|
||||
end
|
||||
end index
|
||||
end h_
|
||||
|
||||
return item
|
||||
|
||||
method isTrue public constant binary returns boolean
|
||||
return 1 == 1
|
||||
|
||||
method isFalse public constant binary returns boolean
|
||||
return \isTrue
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
let rec bsort s =
|
||||
let rec _bsort = function
|
||||
| x :: x2 :: xs when x > x2 ->
|
||||
x2 :: _bsort (x :: xs)
|
||||
| x :: x2 :: xs ->
|
||||
x :: _bsort (x2 :: xs)
|
||||
| s -> s
|
||||
in
|
||||
let t = _bsort s in
|
||||
if t = s then t
|
||||
else bsort t
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
let rec bsort s =
|
||||
let rec _bsort = function
|
||||
| x :: x2 :: xs when x > x2 -> begin
|
||||
match _bsort (x :: xs) with
|
||||
| None -> Some (x2 :: x :: xs)
|
||||
| Some xs2 -> Some (x2 :: xs2)
|
||||
end
|
||||
| x :: x2 :: xs -> begin
|
||||
match _bsort (x2 :: xs) with
|
||||
| None -> None
|
||||
| Some xs2 -> Some (x :: xs2)
|
||||
end
|
||||
| _ -> None
|
||||
in
|
||||
match _bsort s with
|
||||
| None -> s
|
||||
| Some s2 -> bsort s2
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function : Swap(p : Int[]) ~ Nil {
|
||||
t := p[0];
|
||||
p[0] := p[1];
|
||||
p[1] := t;
|
||||
}
|
||||
|
||||
function : Sort(a : Int[]) ~ Nil {
|
||||
do {
|
||||
sorted := true;
|
||||
size -= 1;
|
||||
for (i:=0; i<a->Size(); i+=1;) {
|
||||
if (a[i+1] < a[i]) {
|
||||
swap(a+i);
|
||||
sorted := 0;
|
||||
};
|
||||
};
|
||||
}
|
||||
while (sorted = false);
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
function s = bubblesort(v)
|
||||
itemCount = length(v);
|
||||
do
|
||||
hasChanged = false;
|
||||
itemCount--;
|
||||
for i = 1:itemCount
|
||||
if ( v(i) > v(i+1) )
|
||||
v([i,i+1]) = v([i+1,i]); % swap
|
||||
hasChanged = true;
|
||||
endif
|
||||
endfor
|
||||
until(hasChanged == false)
|
||||
s = v;
|
||||
endfunction
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
v = [9,8,7,3,1,100];
|
||||
disp(bubblesort(v));
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
declare
|
||||
proc {BubbleSort Arr}
|
||||
proc {Swap I J}
|
||||
Arr.J := (Arr.I := Arr.J) %% assignment returns the old value
|
||||
end
|
||||
IsSorted = {NewCell false}
|
||||
MaxItem = {NewCell {Array.high Arr}-1}
|
||||
in
|
||||
for until:@IsSorted do
|
||||
IsSorted := true
|
||||
for I in {Array.low Arr}..@MaxItem do
|
||||
if Arr.I > Arr.(I+1) then
|
||||
IsSorted := false
|
||||
{Swap I I+1}
|
||||
end
|
||||
end
|
||||
MaxItem := @MaxItem - 1
|
||||
end
|
||||
end
|
||||
Arr = {Tuple.toArray unit(10 9 8 7 6 5 4 3 2 1)}
|
||||
in
|
||||
{BubbleSort Arr}
|
||||
{Inspect Arr}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
declare
|
||||
local
|
||||
fun {Loop Xs Changed ?IsSorted}
|
||||
case Xs
|
||||
of X1|X2|Xr andthen X1 > X2 then
|
||||
X2|{Loop X1|Xr true IsSorted}
|
||||
[] X|Xr then
|
||||
X|{Loop Xr Changed IsSorted}
|
||||
[] nil then
|
||||
IsSorted = {Not Changed}
|
||||
nil
|
||||
end
|
||||
end
|
||||
in
|
||||
fun {BubbleSort Xs}
|
||||
IsSorted
|
||||
Result = {Loop Xs false ?IsSorted}
|
||||
in
|
||||
if IsSorted then Result
|
||||
else {BubbleSort Result}
|
||||
end
|
||||
end
|
||||
end
|
||||
in
|
||||
{Show {BubbleSort [3 1 4 1 5 9 2 6 5]}}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
bubbleSort(v)={
|
||||
for(i=1,#v-1,
|
||||
for(j=i+1,#v,
|
||||
if(v[j]<v[i],
|
||||
my(t=v[j]);
|
||||
v[j]=v[i];
|
||||
v[i]=t
|
||||
)
|
||||
)
|
||||
);
|
||||
v
|
||||
};
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
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 );
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
/* A primitive bubble sort */
|
||||
bubble_sort: procedure (A);
|
||||
declare A(*) fixed binary;
|
||||
declare temp fixed binary;
|
||||
declare i fixed binary, no_more_swaps bit (1) aligned;
|
||||
|
||||
do until (no_more_swaps);
|
||||
no_more_swaps = true;
|
||||
do i = lbound(A,1) to hbound(A,1)-1;
|
||||
if A(i) > A(i+1) then
|
||||
do; temp = A(i); A(i) = A(i+1); A(i+1) = temp;
|
||||
no_more_swaps = false;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end bubble_sort;
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
procedure bubble_sort(var list: array of real);
|
||||
var
|
||||
i, j, n: integer;
|
||||
t: real;
|
||||
begin
|
||||
n := length(list);
|
||||
for i := n downto 2 do
|
||||
for j := 0 to i - 1 do
|
||||
if list[j] > list[j + 1] then
|
||||
begin
|
||||
t := list[j];
|
||||
list[j] := list[j + 1];
|
||||
list[j + 1] := t;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
var
|
||||
list: array[0 .. 9] of real;
|
||||
// ...
|
||||
bubble_sort(list);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
sub bubble_sort (@a is rw) {
|
||||
for ^@a -> $i {
|
||||
for $i ^..^ @a -> $j {
|
||||
@a[$j] < @a[$i] and @a[$i, $j] = @a[$j, $i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Sorts an array in place
|
||||
sub bubble_sort {
|
||||
for my $i (0 .. $#_){
|
||||
for my $j ($i + 1 .. $#_){
|
||||
$_[$j] < $_[$i] and @_[$i, $j] = @_[$j, $i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my @a = (39, 25, 30, 28, 36, 72, 98, 25, 43, 38);
|
||||
bubble_sort(@a);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(de bubbleSort (Lst)
|
||||
(use Chg
|
||||
(loop
|
||||
(off Chg)
|
||||
(for (L Lst (cdr L) (cdr L))
|
||||
(when (> (car L) (cadr L))
|
||||
(xchg L (cdr L))
|
||||
(on Chg) ) )
|
||||
(NIL Chg Lst) ) ) )
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
define bubble_sort(v);
|
||||
lvars n=length(v), done=false, i;
|
||||
while not(done) do
|
||||
true -> done;
|
||||
n - 1 -> n;
|
||||
for i from 1 to n do
|
||||
if v(i) > v(i+1) then
|
||||
false -> done;
|
||||
;;; Swap using multiple assignment
|
||||
(v(i+1), v(i)) -> (v(i), v(i+1));
|
||||
endif;
|
||||
endfor;
|
||||
endwhile;
|
||||
enddefine;
|
||||
|
||||
;;; Test it
|
||||
vars ar = { 10 8 6 4 2 1 3 5 7 9};
|
||||
bubble_sort(ar);
|
||||
ar =>
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/bubblesort{
|
||||
/x exch def
|
||||
/temp x x length 1 sub get def
|
||||
/i x length 1 sub def
|
||||
/j i 1 sub def
|
||||
|
||||
x length 1 sub{
|
||||
i 1 sub{
|
||||
x j 1 sub get x j get lt
|
||||
{
|
||||
/temp x j 1 sub get def
|
||||
x j 1 sub x j get put
|
||||
x j temp put
|
||||
}if
|
||||
/j j 1 sub def
|
||||
}repeat
|
||||
/i i 1 sub def
|
||||
/j i 1 sub def
|
||||
}repeat
|
||||
x pstack
|
||||
}def
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
function bubblesort ($a) {
|
||||
$l = $a.Length
|
||||
$hasChanged = $true
|
||||
while ($hasChanged) {
|
||||
$hasChanged = $false
|
||||
$l--
|
||||
for ($i = 0; $i -lt $l; $i++) {
|
||||
if ($a[$i] -gt $a[$i+1]) {
|
||||
$a[$i], $a[$i+1] = $a[$i+1], $a[$i]
|
||||
$hasChanged = $true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Procedure bubbleSort(Array a(1))
|
||||
Protected i, itemCount, hasChanged
|
||||
|
||||
itemCount = ArraySize(a())
|
||||
Repeat
|
||||
hasChanged = #False
|
||||
itemCount - 1
|
||||
For i = 0 To itemCount
|
||||
If a(i) > a(i + 1)
|
||||
Swap a(i), a(i + 1)
|
||||
hasChanged = #True
|
||||
EndIf
|
||||
Next
|
||||
Until hasChanged = #False
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
def bubble_sort(seq):
|
||||
"""Inefficiently sort the mutable sequence (list) in place.
|
||||
seq MUST BE A MUTABLE SEQUENCE.
|
||||
|
||||
As with list.sort() and random.shuffle this does NOT return
|
||||
"""
|
||||
changed = True
|
||||
while changed:
|
||||
changed = False
|
||||
for i in xrange(len(seq) - 1):
|
||||
if seq[i] > seq[i+1]:
|
||||
seq[i], seq[i+1] = seq[i+1], seq[i]
|
||||
changed = True
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Sample usage and simple test suite"""
|
||||
|
||||
from random import shuffle
|
||||
|
||||
testset = range(100)
|
||||
testcase = testset[:] # make a copy
|
||||
shuffle(testcase)
|
||||
assert testcase != testset # we've shuffled it
|
||||
bubble_sort(testcase)
|
||||
assert testcase == testset # we've unshuffled it back into a copy
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(define bubble-shot
|
||||
[A] -> [A]
|
||||
[A B|R] -> [B|(bubble-shot [A|R])] where (> A B)
|
||||
[A |R] -> [A|(bubble-shot R)])
|
||||
|
||||
(define bubble-sort
|
||||
X -> (fix bubble-shot X))
|
||||
|
||||
(bubble-sort [6 8 5 9 3 2 2 1 4 7])
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
bubblesort <- function(v) {
|
||||
itemCount <- length(v)
|
||||
repeat {
|
||||
hasChanged <- FALSE
|
||||
itemCount <- itemCount - 1
|
||||
for(i in 1:itemCount) {
|
||||
if ( v[i] > v[i+1] ) {
|
||||
t <- v[i]
|
||||
v[i] <- v[i+1]
|
||||
v[i+1] <- t
|
||||
hasChanged <- TRUE
|
||||
}
|
||||
}
|
||||
if ( !hasChanged ) break;
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
v <- c(9,8,7,3,1,100)
|
||||
print(bubblesort(v))
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*REXX program sorts an array using the bubble-sort method. */
|
||||
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call bubbleSort highItem /*invoke the bubble sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
|
||||
bubbleSort: procedure expose @.; parse arg n /* n = number of items. */
|
||||
/*diminish # items each time. */
|
||||
do until done /*sort until it's done. */
|
||||
done=1 /*assume it's done (1 = true). */
|
||||
do j=1 for n-1 /*sort M items this time. */
|
||||
k=j+1 /*point to the next item. */
|
||||
if @.j>@.k then do /*is it out of order ? */
|
||||
_=@.j /*assign to a temp variable. */
|
||||
@.j=@.k /*swap current with next. */
|
||||
@.k=_ /*... and next with _ */
|
||||
done=0 /*indicate it's not done. */
|
||||
end /* 1=true 0=false */
|
||||
end /*j*/
|
||||
end /*until done*/
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.= /*assign some default values. */
|
||||
@.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 highItem=1 while @.highItem\=='' /*find how many entries. */
|
||||
end /*highitem*/
|
||||
|
||||
highItem=highItem-1 /*adjust highItem slightly. */
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: widthH=length(highItem) /*maximum width of any line. */
|
||||
do j=1 for highItem
|
||||
say 'element' right(j,widthH) arg(1)':' @.j
|
||||
end
|
||||
say copies('-',80) /*show a separator line. */
|
||||
return
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
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|
|
||||
self[i-1], self[i] = self[i], self[i-1] if self[i-1] < self[i]
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
ary = [3, 78, 4, 23, 6, 8, 6]
|
||||
ary.bubblesort1!
|
||||
p ary
|
||||
# => [3, 4, 6, 6, 8, 23, 78]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
siz = 100
|
||||
dim data$(siz)
|
||||
unSorted = 1
|
||||
|
||||
WHILE unSorted
|
||||
unSorted = 0
|
||||
FOR i = 1 TO siz -1
|
||||
IF data$(i) > data$(i + 1) THEN
|
||||
tmp = data$(i)
|
||||
data$(i) = data$(i + 1)
|
||||
data$(i + 1) = tmp
|
||||
unSorted = 1
|
||||
END IF
|
||||
NEXT
|
||||
WEND
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
* # Sort array in place, return array
|
||||
define('bubble(a,alen)i,j,ub,tmp') :(bubble_end)
|
||||
bubble i = 1; ub = alen
|
||||
outer gt(ub,1) :f(bdone)
|
||||
j = 1
|
||||
inner le(a<j>, a<j + 1>) :s(incrj)
|
||||
tmp = a<j>
|
||||
a<j> = a<j + 1>
|
||||
a<j + 1> = tmp
|
||||
incrj j = lt(j + 1,ub) j + 1 :s(inner)
|
||||
ub = ub - 1 :(outer)
|
||||
bdone bubble = a :(return)
|
||||
bubble_end
|
||||
|
||||
* # Fill array with test data
|
||||
str = '33 99 15 54 1 20 88 47 68 72'
|
||||
output = str; arr = array(10)
|
||||
floop i = i + 1; str span('0123456789') . arr<i> = :s(floop)
|
||||
|
||||
* # Test and display
|
||||
bubble(arr,10); str = ''
|
||||
sloop j = j + 1; str = str arr<j> ' ' :s(sloop)
|
||||
output = trim(str)
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package Bubble
|
||||
is
|
||||
|
||||
type Arr is array(Integer range <>) of Integer;
|
||||
|
||||
procedure Sort (A : in out Arr);
|
||||
--# derives A from *;
|
||||
|
||||
end Bubble;
|
||||
|
||||
|
||||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
Temp : Integer;
|
||||
begin
|
||||
if A'Last /= A'First then
|
||||
loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. A'Last - 1 loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Temp := A (J + 1);
|
||||
A (J + 1) := A (J);
|
||||
A (J) := Temp;
|
||||
end if;
|
||||
end loop;
|
||||
--# assert A'Last /= A'First;
|
||||
exit when Finished;
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package Bubble
|
||||
is
|
||||
|
||||
type Arr is array(Integer range <>) of Integer;
|
||||
|
||||
-- Sorted is a proof function with the definition:
|
||||
-- Sorted(A, From_I, To_I)
|
||||
-- <->
|
||||
-- (for all I in Integer range From_I .. To_I - 1 =>
|
||||
-- (A(I) <= A(I + 1))) .
|
||||
--
|
||||
--# function Sorted (A : Arr;
|
||||
--# From_I, To_I : Integer) return Boolean;
|
||||
|
||||
procedure Sort (A : in out Arr);
|
||||
--# derives A from *;
|
||||
--# post Sorted(A, A'First, A'Last);
|
||||
|
||||
end Bubble;
|
||||
|
||||
|
||||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
Temp : Integer;
|
||||
begin
|
||||
if A'Last > A'First then
|
||||
loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. A'Last - 1
|
||||
--# assert Finished -> Sorted(A, A'First, J);
|
||||
loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Temp := A (J + 1);
|
||||
A (J + 1) := A (J);
|
||||
A (J) := Temp;
|
||||
end if;
|
||||
end loop;
|
||||
--# assert A'Last /= A'First
|
||||
--# and (Finished -> Sorted(A, A'First, A'Last));
|
||||
exit when Finished;
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package body Bubble
|
||||
is
|
||||
procedure Sort (A : in out Arr)
|
||||
is
|
||||
Finished : Boolean;
|
||||
|
||||
-- In_Position is a proof function with the definition:
|
||||
-- In_Position(A, A_Start, A_I, A_End)
|
||||
-- <->
|
||||
-- ((for all K in Integer range A_Start .. A_I - 1 =>
|
||||
-- (A(K) <= A(A_I)))
|
||||
-- and
|
||||
-- Sorted(A, A_I, A_End) .
|
||||
--
|
||||
--# function In_Position (A : Arr;
|
||||
--# A_Start, A_I, A_End : Integer) return Boolean;
|
||||
|
||||
-- Swapped is a proof function with the definition:
|
||||
-- Swapped(A_In, A_Out, I1, I2)
|
||||
-- <->
|
||||
-- (A_Out = A_In[I1 => A_In(I2); I2 => A_In(I1)]).
|
||||
--
|
||||
--# function Swapped (A_In, A_Out : Arr;
|
||||
--# I1, I2 : Integer) return Boolean;
|
||||
|
||||
procedure Swap (A : in out Arr;
|
||||
I1 : in Integer;
|
||||
I2 : in Integer)
|
||||
--# derives A from *, I1, I2;
|
||||
--# pre I1 in A'First .. A'Last
|
||||
--# and I2 in A'First .. A'Last;
|
||||
--# post Swapped(A~, A, I1, I2);
|
||||
is
|
||||
Temp : Integer;
|
||||
begin
|
||||
Temp := A(I2);
|
||||
A(I2) := A(I1);
|
||||
A(I1) := Temp;
|
||||
end Swap;
|
||||
pragma Inline (Swap);
|
||||
|
||||
begin
|
||||
if A'Last > A'First then
|
||||
for I in reverse Integer range A'First + 1 .. A'Last loop
|
||||
Finished := True;
|
||||
for J in Integer range A'First .. I - 1 loop
|
||||
if A (J + 1) < A (J) then
|
||||
Finished := False;
|
||||
Swap (A, J, J + 1);
|
||||
end if;
|
||||
--# assert I% = I -- I is unchanged by execution of the loop
|
||||
--# and (for all K in Integer range A'First .. J =>
|
||||
--# (A(K) <= A(J + 1)))
|
||||
--# and (I < A'Last -> In_Position(A, A'First, I + 1, A'Last))
|
||||
--# and (Finished -> Sorted(A, A'First, J + 1));
|
||||
end loop;
|
||||
exit when Finished;
|
||||
--# assert In_Position(A, A'First, I, A'Last);
|
||||
end loop;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Bubble;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue