June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,33 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
|
||||
extension $op
|
||||
{
|
||||
selectionSort
|
||||
[
|
||||
var copy := self clone.
|
||||
|
||||
0 till(copy length) do(:i)
|
||||
[
|
||||
int k := i.
|
||||
(i + 1) till(copy length) do(:j)
|
||||
[
|
||||
if (copy[j] < copy[k])
|
||||
[
|
||||
k := j.
|
||||
]
|
||||
].
|
||||
copy exchange(i,k).
|
||||
].
|
||||
|
||||
^ copy
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := ("this", "is", "a", "test", "of", "generic", "selection", "sort").
|
||||
|
||||
console printLine("before:",list).
|
||||
console printLine("after:",list selectionSort).
|
||||
].
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
siLow As Short = -99 'Set the lowest value number to create
|
||||
siHigh As Short = 99 'Set the highest value number to create
|
||||
siQty As Short = 20 'Set the quantity of numbers to create
|
||||
|
||||
Public Sub Main()
|
||||
Dim siToSort As Short[] = CreateNumbersToSort()
|
||||
Dim siPos, siLow, siChar, siCount As Short
|
||||
|
||||
PrintOut("To sort: ", siToSort)
|
||||
|
||||
For siCount = 0 To siToSort.Max
|
||||
siChar = siCount
|
||||
For siPos = siCount + 1 To siToSort.Max
|
||||
If siToSort[siChar] > siToSort[siPos] Then siChar = siPos
|
||||
Next
|
||||
siLow = siToSort[siChar]
|
||||
siToSort.Delete(siChar, 1)
|
||||
siToSort.Add(siLow, siCount)
|
||||
Next
|
||||
|
||||
PrintOut(" Sorted: ", siToSort)
|
||||
|
||||
End
|
||||
'---------------------------------------------------------
|
||||
Public Sub PrintOut(sText As String, siToSort As String[])
|
||||
Dim siCount As Short
|
||||
|
||||
Print sText;
|
||||
|
||||
For siCount = 0 To siToSort.Max
|
||||
Print siToSort[siCount];
|
||||
If siCount <> siToSort.max Then Print ", ";
|
||||
Next
|
||||
|
||||
Print
|
||||
|
||||
End
|
||||
'---------------------------------------------------------
|
||||
Public Sub CreateNumbersToSort() As Short[]
|
||||
Dim siCount As Short
|
||||
Dim siList As New Short[]
|
||||
|
||||
For siCount = 0 To siQty
|
||||
siList.Add(Rand(siLow, siHigh))
|
||||
Next
|
||||
|
||||
Return siList
|
||||
|
||||
End
|
||||
|
|
@ -1,21 +1,15 @@
|
|||
function selectionsort!{T<:Real}(a::Array{T,1})
|
||||
len = length(a)
|
||||
if len < 2
|
||||
return nothing
|
||||
end
|
||||
function selectionsort!(arr::Vector{<:Real})
|
||||
len = length(arr)
|
||||
if len < 2 return arr end
|
||||
for i in 1:len-1
|
||||
(lmin, j) = findmin(a[i+1:end])
|
||||
if lmin < a[i]
|
||||
a[i+j] = a[i]
|
||||
a[i] = lmin
|
||||
lmin, j = findmin(arr[i+1:end])
|
||||
if lmin < arr[i]
|
||||
arr[i+j] = arr[i]
|
||||
arr[i] = lmin
|
||||
end
|
||||
end
|
||||
return nothing
|
||||
return arr
|
||||
end
|
||||
|
||||
a = [rand(-100:100) for i in 1:20]
|
||||
println("Before Sort:")
|
||||
println(a)
|
||||
selectionsort!(a)
|
||||
println("\nAfter Sort:")
|
||||
println(a)
|
||||
v = rand(-10:10, 10)
|
||||
println("# unordered: $v\n -> ordered: ", selectionsort!(v))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
arr:= Array([17,3,72,0,36,2,3,8,40,0]):
|
||||
len := numelems(arr):
|
||||
for i to len-1 do
|
||||
j_min := i:
|
||||
for j from i+1 to len do
|
||||
if arr[j] < arr[j_min] then
|
||||
j_min := j:
|
||||
end if:
|
||||
end do:
|
||||
if (not j_min = i) then
|
||||
temp := arr[i]:
|
||||
arr[i] := arr[j_min]:
|
||||
arr[j_min] := temp:
|
||||
end if:
|
||||
end do:
|
||||
arr;
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
.de end
|
||||
..
|
||||
.de array
|
||||
. nr \\$1.c 0 1
|
||||
. de \\$1.push end
|
||||
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
|
||||
. end
|
||||
. de \\$1.pushln end
|
||||
. if \\\\n(.$>0 .\\$1.push \\\\$1
|
||||
. if \\\\n(.$>1 \{ \
|
||||
. shift
|
||||
. \\$1.pushln \\\\$@
|
||||
. \}
|
||||
. end
|
||||
. de \\$1.dump end
|
||||
. nr i 0 1
|
||||
. while \\\\n+i<=\\\\n[\\$1.c] .tm \\\\n[\\$1..\\\\ni]
|
||||
. rr i
|
||||
. end
|
||||
. de \\$1.swap end
|
||||
. if (\\\\$1<=\\\\n[\\$1.c])&(\\\\$2<=\\\\n[\\$1.c]) \{ \
|
||||
. nr b \\\\n[\\$1..\\\\$1]
|
||||
. nr \\$1..\\\\$1 \\\\n[\\$1..\\\\$2]
|
||||
. nr \\$1..\\\\$2 \\\\nb
|
||||
. rr b
|
||||
. \}
|
||||
. end
|
||||
..
|
||||
.array myArray
|
||||
.myArray.pushln 14 62 483 21 12 11 0 589 212 10 5 4 95 4 2 2 12 0 0
|
||||
.de sort
|
||||
. nr i 0 1
|
||||
. while \\n+i<=\\n[\\$1.c] \{ \
|
||||
. nr j \\ni 1
|
||||
. nr st \\nj
|
||||
. while \\n+j<=\\n[\\$1.c] \{ \
|
||||
. if \\n[\\$1..\\nj]<\\n[\\$1..\\n(st] .nr st \\nj
|
||||
. \}
|
||||
. if !\\n(st=\\ni .\\$1.swap \\ni \\n(st
|
||||
. \}
|
||||
..
|
||||
.sort myArray
|
||||
.myArray.dump
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
0
|
||||
0
|
||||
0
|
||||
2
|
||||
2
|
||||
4
|
||||
4
|
||||
5
|
||||
10
|
||||
11
|
||||
12
|
||||
12
|
||||
14
|
||||
21
|
||||
62
|
||||
95
|
||||
212
|
||||
483
|
||||
589
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
mata
|
||||
function selection_sort(real vector a) {
|
||||
real scalar i, j, k, n
|
||||
n = length(a)
|
||||
for (i = 1; i < n; i++) {
|
||||
k = i
|
||||
for (j = i+1; j <= n; j++) {
|
||||
if (a[j] < a[k]) k = j
|
||||
}
|
||||
if (k != i) a[(i, k)] = a[(k, i)]
|
||||
}
|
||||
}
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue