Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,18 @@
;; recursive version (adapted from Racket)
(lib 'list) ;; list-delete
(define (sel-sort xs (x0))
(cond
[(null? xs) null]
[else (set! x0 (apply min xs))
(cons x0 (sel-sort (list-delete xs x0)))]))
(sel-sort (shuffle (iota 13)))
→ (0 1 2 3 4 5 6 7 8 9 10 11 12)
;; straightforward and more efficient implementation using list-swap!
(define (sel-sort list)
(maplist (lambda( L)
(first (list-swap! L (first L) (apply min L )))) list))
(sel-sort (shuffle (iota 13)))
→ (0 1 2 3 4 5 6 7 8 9 10 11 12)

View file

@ -0,0 +1,13 @@
;; sort an array in place
(define (sel-sort a (amin) (imin))
(define ilast (1- (vector-length a)))
(for [(i ilast)]
(set! amin [a (setv! imin i)]) ;; imin := i , amin := a[imin]
(for [(j (in-range (1+ i) (1+ ilast)))]
(when (< [a j] amin) (set! amin [a (setv! imin j)])))
(vector-swap! a i imin))
a )
(define a #(9 8 2 6 3 5 4))
(sel-sort a)
→ #( 2 3 4 5 6 8 9)

View file

@ -0,0 +1,47 @@
' version 03-12-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Sub selectionsort(arr() As Long)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long i, j, x
Dim As Long lb = LBound(arr)
Dim As Long ub = UBound(arr)
For i = lb To ub -1
x = i
For j = i +1 To ub
If arr(j) < arr(x) Then x = j
Next
If x <> i Then
Swap arr(i), arr(x)
End If
Next
End Sub
' ------=< MAIN >=------
Dim As Long i, array(-7 To 7)
Dim As Long a = LBound(array), b = UBound(array)
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
Print "unsort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
selectionsort(array()) ' sort the array
Print " sort ";
For i = a To b : Print Using "####"; array(i); : Next : Print
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,12 @@
proc selectionSort[T](a: var openarray[T]) =
let n = a.len
for i in 0 .. <n:
var m = i
for j in i .. <n:
if a[j] < a[m]:
m = j
swap a[i], a[m]
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
selectionSort a
echo a

View file

@ -0,0 +1,11 @@
: selectSort(l)
| b j i k s |
l size ->s
l asListBuffer ->b
s loop: i [
i dup ->k b at
i 1 + s for: j [ b at(j) 2dup <= ifTrue: [ drop ] else: [ nip j ->k ] ]
k i b at b put i swap b put
]
b dup freeze ;

View file

@ -0,0 +1,13 @@
function selection_sort(sequence s)
integer m
for i=1 to length(s) do
m = i
for j=i+1 to length(s) do
if s[j]<s[m] then
m = j
end if
end for
{s[i],s[m]} = {s[m],s[i]}
end for
return s
end function

View file

@ -0,0 +1,33 @@
class SelectionSort
**Sort a list with the Selection Sort algorithm**
on start
args := program arguments
.sort(args)
print args
define sort(list) is shared
**Sort the list**
test
list := [4, 2, 7, 3]
.sort(list)
assert list = [2, 3, 4, 7]
body
count := list.count
last := count - 1
for i in last
minCandidate := i
j := i + 1
while j < count
if list[j] < list[minCandidate], minCandidate := j
j :+ 1
temp := list[i]
list[i] := list[minCandidate]
list[minCandidate] := temp

View file

@ -0,0 +1,19 @@
aList = [7,6,5,9,8,4,3,1,2,0]
see sortList(aList)
func sortList list
count = len(list) + 1
last = count - 1
for i = 1 to last
minCandidate = i
j = i + 1
while j < count
if list[j] < list[minCandidate] minCandidate = j ok
j = j + 1
end
temp = list[i]
list[i] = list[minCandidate]
list[minCandidate] = temp
next
return list

View file

@ -0,0 +1,20 @@
class Array {
method selectionsort {
for i in ^(self.end) {
var min_idx = i
for j in (i+1 .. self.end) {
if (self[j] < self[min_idx]) {
min_idx = j
}
}
self.swap(i, min_idx)
}
return self
}
}
var nums = [7,6,5,9,8,4,3,1,2,0];
say nums.selectionsort;
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say strs.selectionsort;

View file

@ -0,0 +1,19 @@
func selectionSort(inout arr:[Int]) {
var min:Int
for n in 0..<arr.count {
min = n
for x in n+1..<arr.count {
if (arr[x] < arr[min]) {
min = x
}
}
if min != n {
let temp = arr[min]
arr[min] = arr[n]
arr[n] = temp
}
}
}

View file

@ -0,0 +1,18 @@
# Sort any array
def selection_sort:
def swap(i;j): if i == j then . else .[i] as $tmp | .[i] = .[j] | .[j] = $tmp end;
length as $length
| reduce range(0; $length) as $currentPlace
# state: $array
( .;
. as $array
| (reduce range( $currentPlace; $length) as $check
# state: [ smallestAt, smallest] except initially [null]
( [$currentPlace+1] ;
if length == 1 or $array[$check] < .[1]
then [$check, $array[$check] ]
else .
end
)) as $ans
| swap( $currentPlace; $ans[0] )
) ;

View file

@ -0,0 +1 @@
[1, 3.3, null, 2, null, [1,{"a":1 }] ] | selection_sort