September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,42 +1,42 @@
|
|||
import extensions.
|
||||
import system'math.
|
||||
import system'routines.
|
||||
import extensions;
|
||||
import system'math;
|
||||
import system'routines;
|
||||
|
||||
extension $op
|
||||
extension op
|
||||
{
|
||||
combSort
|
||||
[
|
||||
var list := self clone.
|
||||
combSort()
|
||||
{
|
||||
var list := self.clone();
|
||||
|
||||
real gap := list length.
|
||||
bool swaps := true.
|
||||
while ((gap > 1)|| (swaps))
|
||||
[
|
||||
gap /= 1.247330950103979r.
|
||||
if (gap<1) [ gap := 1 ].
|
||||
real gap := list.Length;
|
||||
bool swaps := true;
|
||||
while (gap > 1 || swaps)
|
||||
{
|
||||
gap /= 1.247330950103979r;
|
||||
if (gap<1) { gap := 1 };
|
||||
|
||||
int i := 0.
|
||||
swaps := false.
|
||||
while (i + gap roundedInt < list length)
|
||||
[
|
||||
int igap := i + gap roundedInt.
|
||||
int i := 0;
|
||||
swaps := false;
|
||||
while (i + gap.RoundedInt < list.Length)
|
||||
{
|
||||
int igap := i + gap.RoundedInt;
|
||||
if (list[i] > list[igap])
|
||||
[
|
||||
list exchange(i,igap).
|
||||
{
|
||||
list.exchange(i,igap);
|
||||
swaps := true
|
||||
].
|
||||
};
|
||||
i += 1
|
||||
].
|
||||
].
|
||||
}
|
||||
};
|
||||
|
||||
^ list
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := (3, 5, 1, 9, 7, 6, 8, 2, 4 ).
|
||||
public program()
|
||||
{
|
||||
var list := new int[]{3, 5, 1, 9, 7, 6, 8, 2, 4 };
|
||||
|
||||
console printLine("before:", list).
|
||||
console printLine("after :", list combSort).
|
||||
].
|
||||
console.printLine("before:", list.asEnumerable());
|
||||
console.printLine("after :", list.combSort().asEnumerable())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
\ combsort for the Forth Newbie (GForth)
|
||||
|
||||
HEX
|
||||
\ gratuitous variables ( Add clarity but NOT re-entrant)
|
||||
VARIABLE GAP
|
||||
VARIABLE SORTED \ flag
|
||||
|
||||
DECIMAL
|
||||
100 CONSTANT SIZE
|
||||
|
||||
\ allocate a small array of cells
|
||||
CREATE Q SIZE CELLS ALLOT
|
||||
|
||||
\ operator to index into the array
|
||||
: ]Q ( n -- adr) CELLS Q + ;
|
||||
|
||||
\ fill array and see array
|
||||
: INITDATA ( -- ) SIZE 0 DO SIZE I - I ]Q ! LOOP ;
|
||||
|
||||
: SEEDATA ( -- ) CR SIZE 0 DO I ]Q @ U. LOOP ;
|
||||
|
||||
\ compute a new gap using scaled division
|
||||
\ factored out for this example. Could be a macro or inline code.
|
||||
: /1.3 ( n -- n' ) 10 13 */ ;
|
||||
|
||||
\ factored out for this example. Could be a macro or inline code.
|
||||
: XCHG ( adr1 adr2 n1 n2-- ) SWAP ROT ! SWAP ! ;
|
||||
|
||||
: COMBSORT ( n -- )
|
||||
DUP >R \ copy n to return stack
|
||||
1+ GAP ! \ set GAP to n+1
|
||||
BEGIN
|
||||
GAP @ /1.3 GAP ! \ re-compute the gap
|
||||
SORTED ON
|
||||
R@ GAP @ - 0 \ n-gap is loop limit
|
||||
DO
|
||||
I GAP @ + ]Q I ]Q \ compute array addresses
|
||||
OVER @ OVER @ \ fetch the data in each cell
|
||||
2DUP < \ compare a copy of the data
|
||||
IF
|
||||
XCHG \ Exchange the data in the cells
|
||||
SORTED OFF \ flag we are not sorted
|
||||
ELSE
|
||||
2DROP 2DROP \ remove address and data
|
||||
THEN
|
||||
LOOP
|
||||
SORTED @ GAP @ 0= AND \ test for complete
|
||||
UNTIL
|
||||
R> DROP ; \ remove 'n' from return stack
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
Function comb_sort(ByVal s As Variant) As Variant
|
||||
Dim gap As Integer: gap = UBound(s)
|
||||
Dim swapped As Integer
|
||||
Do While True
|
||||
gap = WorksheetFunction.Max(WorksheetFunction.Floor_Precise(gap / 1.3), 1)
|
||||
swapped = False
|
||||
For i = 0 To UBound(s) - gap
|
||||
si = Val(s(i))
|
||||
If si > Val(s(i + gap)) Then
|
||||
s(i) = s(i + gap)
|
||||
s(i + gap) = CStr(si)
|
||||
swapped = True
|
||||
End If
|
||||
Next i
|
||||
If gap = 1 And Not swapped Then Exit Do
|
||||
Loop
|
||||
comb_sort = s
|
||||
End Function
|
||||
|
||||
Public Sub main()
|
||||
Dim s(9) As Variant
|
||||
For i = 0 To 9
|
||||
s(i) = CStr(Int(1000 * Rnd))
|
||||
Next i
|
||||
Debug.Print Join(s, ", ")
|
||||
Debug.Print Join(comb_sort(s), ", ")
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue