June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,50 @@
import extensions.
import system'math.
import system'routines.
extension $op
{
cocktailSort
[
var list := self clone.
bool swapped := true.
while(swapped)
[
swapped := false.
0 to(list length - 2) do(:i)
[
if (list[i]>list[i+1])
[
list exchange(i,i+1).
swapped := true.
]
].
ifnot (swapped)
[
^ list
].
swapped := false.
(list length - 2) to(0) do(:i)
[
if (list[i]>list[i+1])
[
list exchange(i,i+1).
swapped := true.
]
].
].
^ list
]
}
program =
[
var list := (3, 5, 1, 9, 7, 6, 8, 2, 4 ).
console printLine("before:", list).
console printLine("after :", list cocktailSort).
].

View file

@ -0,0 +1,47 @@
Public Sub Main()
Dim siCount, siRev, siProcess As Short
Dim bSorting As Boolean
Dim byToSort As Byte[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 154, 147, 102, 46, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
Print "To sort: -"
ShowWorking(byToSort)
Print
Repeat
bSorting = False
siRev = byToSort.Max - 1
For siCount = 0 To byToSort.Max - 1
siProcess = siCount
GoSub Check
siProcess = siRev
GoSub Check
Dec siRev
Next
If bSorting Then ShowWorking(byToSort)
Until bSorting = False
Return
Check:
If byToSort[siProcess] > byToSort[siProcess + 1] Then
Swap byToSort[siProcess], byToSort[siProcess + 1]
bSorting = True
Endif
Return
End
'-----------------------------------------
Public Sub ShowWorking(byToSort As Byte[])
Dim siCount As Byte
For siCount = 0 To byToSort.Max
Print Str(byToSort[siCount]);
If siCount <> byToSort.Max Then Print ",";
Next
Print
End

View file

@ -1,27 +1,20 @@
function coctailsort{T<:Real}(a::Array{T,1})
function coctailsort(a::Vector)
b = copy(a)
isordered = false
lo = one(Int)
hi = length(b)
lo, hi = 1, length(b)
while !isordered && hi > lo
isordered = true
for i in lo+1:hi
if b[i] < b[i-1]
t = b[i]
b[i] = b[i-1]
b[i-1] = t
b[i-1], b[i] = b[i], b[i-1]
isordered = false
end
end
hi -= 1
if isordered || hi <= lo
break
end
if isordered || hi ≤ lo break end
for i in hi:-1:lo+1
if b[i-1] > b[i]
t = b[i]
b[i] = b[i-1]
b[i-1] = t
b[i-1], b[i] = b[i], b[i-1]
isordered = false
end
end
@ -30,9 +23,5 @@ function coctailsort{T<:Real}(a::Array{T,1})
return b
end
a = [rand(-2^10:2^10) for i in 1:20]
println("Before Sort:")
println(a)
a = coctailsort(a)
println("\nAfter Sort:")
println(a)
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", cocktailsort(v))

View file

@ -0,0 +1,26 @@
arr := Array([17,3,72,0,36,2,3,8,40,0]):
len := numelems(arr):
swap := proc(arr, a, b)
local temp := arr[a]:
arr[a] := arr[b]:
arr[b] := temp:
end proc:
while(true) do
swapped := false:
for i to len-1 do
if arr[i] > arr[i+1] then:
swap(arr, i, i+1):
swapped := true:
end if:
end do:
if (not swapped) then break: end if:
swapped := false:
for j from len-1 to 1 by -1 do
if arr[j] > arr[j+1] then
swap(arr,j,j+1):
swapped := true:
end if:
end do:
if (not swapped) then break: end if:
end do:
arr;