September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -15,6 +15,8 @@ A boolean variable is used to track whether any changes have been made in the cu
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
'''repeat'''
'''if''' itemCount <= 1
'''return'''
hasChanged := false
'''decrement''' itemCount
'''repeat with''' index '''from''' 1 '''to''' itemCount

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,26 @@
100 PROGRAM "BubblSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(-5 TO 9)
130 CALL INIT(ARRAY)
140 CALL WRITE(ARRAY)
150 CALL BUBBLESORT(ARRAY)
160 CALL WRITE(ARRAY)
170 DEF INIT(REF A)
180 FOR I=LBOUND(A) TO UBOUND(A)
190 LET A(I)=RND(98)+1
200 NEXT
210 END DEF
220 DEF WRITE(REF A)
230 FOR I=LBOUND(A) TO UBOUND(A)
240 PRINT A(I);
250 NEXT
260 PRINT
270 END DEF
280 DEF BUBBLESORT(REF A)
290 DO
300 LET CH=0
310 FOR I=LBOUND(A) TO UBOUND(A)-1
320 IF A(I)>A(I+1) THEN LET T=A(I):LET A(I)=A(I+1):LET A(I+1)=T:LET CH=1
330 NEXT
340 LOOP WHILE CH
350 END DEF

View file

@ -1,34 +1,34 @@
import system'routines.
import extensions.
import system'routines;
import extensions;
extension $op
extension op
{
bubbleSort
[
var list := self clone.
bubbleSort()
{
var list := self.clone();
bool madeChanges := true.
int itemCount := list length.
bool madeChanges := true;
int itemCount := list.Length;
while (madeChanges)
[
madeChanges := false.
itemCount -= 1.
0 till:itemCount do(:i)
[
{
madeChanges := false;
itemCount -= 1;
for(int i := 0, i < itemCount, i += 1)
{
if (list[i] > list[i + 1])
[
list exchange(i,i+1).
madeChanges := true.
]
]
].
{
list.exchange(i,i+1);
madeChanges := true
}
}
};
^ list
]
}
}
program =
[
var list := (3, 7, 3, 2, 1, -4, 10, 12, 4).
console printLine(list bubbleSort).
].
public program()
{
var list := new int[]{3, 7, 3, 2, 1, -4, 10, 12, 4};
console.printLine(list.bubbleSort().asEnumerable())
}

View file

@ -1,11 +1,11 @@
defmodule Sort do
def bsort(list) when is_list(list) do
t = bsort_move(list)
t = bsort_iter(list)
if t == list do t else bsort(t) end
if t == list, do: t, else: bsort(t)
end
def bsort_move([x, y | t]) when x > y, do: [y | bsort_move([x | t])]
def bsort_move([x, y | t]), do: [x | bsort_move([y | t])]
def bsort_move(list), do: list
def bsort_iter([x, y | t]) when x > y, do: [y | bsort_iter([x | t])]
def bsort_iter([x, y | t]), do: [x | bsort_iter([y | t])]
def bsort_iter(list), do: list
end

View file

@ -1,4 +1,4 @@
Dim sortable() As Integer ' assume the array is populated
Dim sortable() As Integer = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sortable.Shuffle() ' sortable is now randomized
Dim swapped As Boolean
Do
@ -6,10 +6,10 @@
bound = sortable.Ubound
While index < bound
If Sortable(index) > Sortable(index + 1) Then
Dim s As Integer = Sortable(index)
Sortable.Remove(index)
Sortable.Insert(index + 1, s)
If sortable(index) > sortable(index + 1) Then
Dim s As Integer = sortable(index)
sortable.Remove(index)
sortable.Insert(index + 1, s)
swapped = True
End If
index = index + 1

View file

@ -0,0 +1,32 @@
/*REXX program sorts an array (of any kind of numbers) using the bubble─sort algorithm.*/
parse arg N seed . /*obtain optional size of array from CL*/
if N=='' | N=="," then N=30 /*Not specified? Then use the default.*/
if datatype(seed, 'W') then call random ,,seed /*An integer? Use the seed for RANDOM.*/
call gen N /*generate the array elements (items). */
call show 'before sort:' /*show the before array elements. */
$$= $ /*keep "before" copy for after the sort*/
call bSort N /*invoke the bubble sort with N items.*/
say $$
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg #; m=#-1 /*N: is the number of @ array elements.*/
call disp /*show a snapshot of the unsorted array*/
do m=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 do; parse value @.j @.k 0 with @.k @.j ok
end
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
call disp /*show snapshot of partically sorted @.*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: do j=1 for N; @.j= j; end
do k=1 for N; g= random(1,N); parse value @.k @.g with @.g @.k; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg $; do k=1 for N; $=$ right(@.k, length(N)); end; say $; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
disp: 'CLS'; $.= /*"CLS" is the command to clear screen.*/
do e=1 for #; $.e= ''overlay("", $.e, @.e); end /*e*/
do s=# for # by -1; say $.s; end /*s*/
say ""copies('', #) /*display the horizontal axis at bottom*/
return

View file

@ -0,0 +1,22 @@
package Bubble with SPARK_Mode is
type Arr is array (Integer range <>) of Integer;
function Sorted (A : Arr) return Boolean is
(for all I in A'First .. A'Last - 1 => A(I) <= A(I + 1))
with
Ghost,
Pre => A'Last > Integer'First;
function Bubbled (A : Arr) return Boolean is
(for all I in A'First .. A'Last - 1 => A(I) <= A(A'Last))
with
Ghost,
Pre => A'Last > Integer'First;
procedure Sort (A : in out Arr)
with
Pre => A'Last > Integer'First and A'Last < Integer'Last,
Post => Sorted (A);
end Bubble;

View file

@ -0,0 +1,34 @@
package body Bubble with SPARK_Mode is
procedure Sort (A : in out Arr)
is
Prev : Arr (A'Range) with Ghost;
Done : Boolean;
begin
for I in reverse A'First .. A'Last - 1 loop
Prev := A;
Done := True;
for J in A'First .. I loop
if A(J) > A(J + 1) then
declare
TMP : Integer := A(J);
begin
A(J) := A(J + 1);
A(J + 1) := TMP;
Done := False;
end;
end if;
pragma Loop_Invariant (if Done then Sorted (A(A'First .. J + 1)));
pragma Loop_Invariant (Bubbled (A(A'First .. J + 1)));
pragma Loop_Invariant (A(J + 2 .. A'Last) = Prev(J + 2 .. A'Last));
pragma Loop_Invariant (for some K in A'First .. J + 1 =>
A(J + 1) = Prev(K));
end loop;
exit when Done;
pragma Loop_Invariant (if Done then Sorted (A));
pragma Loop_Invariant (Bubbled (A(A'First .. I + 1)));
pragma Loop_Invariant (Sorted (A(I + 1 .. A'Last)));
end loop;
end Sort;
end Bubble;

View file

@ -0,0 +1,11 @@
with Ada.Integer_Text_IO;
with Bubble;
procedure Main is
A : Bubble.Arr := (5,4,6,3,7,2,8,1,9);
begin
Bubble.Sort (A);
for I in A'Range loop
Ada.Integer_Text_IO.Put (A(I));
end loop;
end Main;

View file

@ -0,0 +1,5 @@
project Bubble is
for Main use ("main.adb");
end Bubble;

View file

@ -0,0 +1,17 @@
mata
function bubble_sort(a) {
n = length(a)
for (j = n; j >= 2; j--) {
q = 1
for (i = 2; i <= j; i++) {
if (a[i-1] > a[i]) {
q = 0
s = a[i-1]
a[i-1] = a[i]
a[i] = s
}
}
if (q) return
}
}
end

View file

@ -0,0 +1,27 @@
Private Function bubble_sort(s As Variant) As Variant
Dim tmp As Variant
Dim changed As Boolean
For j = UBound(s) To 1 Step -1
changed = False
For i = 1 To j - 1
If s(i) > s(i + 1) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
changed = True
End If
Next i
If Not changed Then
Exit For
End If
Next j
bubble_sort = s
End Function
Public Sub main()
s = [{4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1}]
Debug.Print "Before: "
Debug.Print Join(s, ", ")
Debug.Print "After: "
Debug.Print Join(bubble_sort(s), ", ")
End Sub

View file

@ -0,0 +1,16 @@
Dim temp, count As Integer
Dim isDirty As Boolean
count = Ubound(list) // count the array size
// loop through until we don't move any numbers... this means we are sorted
Do
isDirty = False // we haven't touched anything yet
For i As Integer = 1 To count - 1 // loop through all the numbers
If list(i) > list(i + 1) Then // if the right number is smaller then the left.. swap
temp = list(i + 1)
list(i + 1) = list(i)
list(i) = temp
isDirty = True // we touched the data so mark it as dirty
End
Next
Loop Until isDirty = False // if we made it without touching the data then we are done

View file

@ -0,0 +1,21 @@
5000 CLS
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT"
5010 LET la=LEN a$
5011 LET i=1: LET u=0
5020 LET d=0: LET p=(u=0)-(u=1)
5021 LET l=(i AND u=0)+(la-i+u AND u=1)
5030 IF u=0 THEN IF a$(l+1)>=a$(l) THEN GO TO 5050
5031 IF u=1 THEN IF a$(l-1)<=a$(l) THEN GO TO 5050
5040 LET d=1
5042 LET t$=a$(l+p)
5043 LET a$(l+p)=a$(l)
5044 LET a$(l)=t$
5050 LET l=l+p
5051 PRINT AT 10,21;a$(l);AT 12,0;a$
5055 IF l<=la-i AND l>=i THEN GO TO 5023
5061 LET i=i+NOT u
5063 LET u=NOT u
5064 IF d AND i<la THEN GO TO 5020
5072 PRINT AT 12,0;a$
9000 STOP

View file

@ -0,0 +1,11 @@
10 LET siz=32
20 DIM d$(siz)
30 REM Populate d$
40 FOR n=1 TO siz: LET d$(n)=CHR$ (48+INT (RND*75)): NEXT n
50 PRINT d$
60 LET unSorted=0
70 FOR i=1 TO siz-1
80 IF d$(i)>d$(i+1) THEN LET t$=d$(i): LET d$(i)=d$(i+1): LET d$(i+1)=t$: LET unSorted=1
90 NEXT i
100 IF unSorted THEN LET siz=siz-1: GO TO 60
110 PRINT d$