Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,62 +1,83 @@
class
SELECTION_SORT[G -> COMPARABLE]
SELECTION_SORT [G -> COMPARABLE]
feature {NONE}
index_of_min(ar: ARRAY [G]; lower: INTEGER):INTEGER
--find index of smallest element in array ar in the range of lower and the max index.
require
lower_positiv : lower >=1
lower_in_range: lower <= ar.count
ar_not_void: ar/= Void
local
i, index: INTEGER
min: G
do
from
i:=lower
min := ar.item (i)
index := i
until
i+1 > ar.count
loop
if ar.item(i+1) < min then
min := ar.item(i+1)
index := i+1
end
i := i + 1
end
Result := index
index_of_min (ar: ARRAY [G]; lower: INTEGER): INTEGER
--Index of smallest element in 'ar' in the range of lower and the max index.
require
lower_positiv: lower >= 1
lower_in_range: lower <= ar.count
ar_not_void: ar /= Void
local
i: INTEGER
min: G
do
from
i := lower
min := ar.item (i)
Result := i
until
i + 1 > ar.count
loop
if ar.item (i + 1) < min then
min := ar.item (i + 1)
Result := i + 1
end
i := i + 1
end
ensure
result_is_set: Result /= Void
end
end
sort (ar: ARRAY [G]):ARRAY[G]
-- sort array ar with selectionsort
sort (ar: ARRAY [G]): ARRAY [G]
-- sort array ar with selectionsort
require
ar_not_void: ar/=VOID
ar_not_void: ar /= Void
local
min_index: INTEGER
ith: G
do
across ar as ic loop
min_index := index_of_min(ar,ic.cursor_index)
ith:=ar[ic.cursor_index]
ar[ic.cursor_index]:= ar[min_index]
ar[min_index]:=ith
end
Result:= ar
do
create Result.make_empty
Result.deep_copy (ar)
across
Result as ic
loop
min_index := index_of_min (Result, ic.cursor_index)
ith := Result [ic.cursor_index]
Result [ic.cursor_index] := Result [min_index]
Result [min_index] := ith
end
ensure
Result_is_set: Result /= Void
end
Result_sorted: is_sorted (Result) = True
end
is_sorted (ar: ARRAY [G]): BOOLEAN
--- Is 'ar' sorted in ascending order?
require
ar_not_empty: ar.is_empty = False
local
i: INTEGER
do
Result := True
from
i := ar.lower
until
i = ar.upper
loop
if ar [i] > ar [i + 1] then
Result := False
end
i := i + 1
end
end
feature
selectionsort(ar: ARRAY[G]):ARRAY[G]
selectionsort (ar: ARRAY [G]): ARRAY [G]
do
Result:= sort(ar)
Result := sort (ar)
end
end

View file

@ -1,23 +1,32 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
make
feature
make
do
test := <<1, 27, 32, 99, 1, -7, 3, 5, 7>>
io.put_string ("Unsorted: ")
across test as ic loop io.put_string (ic.item.out + " ") end
create selection
io.put_string ("%NSorted: ")
test:= selectionsort.selectionsort(test)
across test as ar loop io.put_string (ar.item.out + " ") end
end
make
do
test := <<1, 27, 32, 99, 1, -7, 3, 5, 7>>
io.put_string ("Unsorted: ")
across
test as ic
loop
io.put_string (ic.item.out + " ")
end
create selectionsort
io.put_string ("%NSorted: ")
test := selectionsort.selectionsort (test)
across
test as ar
loop
io.put_string (ar.item.out + " ")
end
end
test: ARRAY [INTEGER]
selectionsort: SELECTION_SORT [INTEGER]
test: ARRAY[INTEGER]
selection: SELECTION_SORT[INTEGER]
end

View file

@ -0,0 +1,9 @@
defmodule Sort do
def selection_sort(list) when is_list(list), do: selection_sort(list, [])
defp selection_sort([], sorted), do: sorted
defp selection_sort(list, sorted) do
max = Enum.max(list)
selection_sort(List.delete(list, max), [max | sorted])
end
end

View file

@ -0,0 +1,21 @@
function selectionsort!{T<:Real}(a::Array{T,1})
len = length(a)
if len < 2
return nothing
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
end
end
return nothing
end
a = [rand(-100:100) for i in 1:20]
println("Before Sort:")
println(a)
selectionsort!(a)
println("\nAfter Sort:")
println(a)

View file

@ -1,6 +1,6 @@
Selection: procedure options (main); /* 2 November 2013 */
declare a(10) fixed b inary initial (
declare a(10) fixed binary initial (
5, 7, 3, 98, 4, -3, 25, 20, 60, 17);
put edit (trim(a)) (a, x(1));

View file

@ -1,5 +1,5 @@
def selectionSort(lst):
for i in range(0,len(lst)-1):
def selection_sort(lst):
for i, e in enumerate(lst):
mn = min(range(i,len(lst)), key=lst.__getitem__)
lst[i],lst[mn] = lst[mn],lst[i]
lst[i], lst[mn] = lst[mn], e
return lst

View file

@ -0,0 +1,19 @@
Function Selection_Sort(s)
arr = Split(s,",")
For i = 0 To UBound(arr)
For j = i To UBound(arr)
temp = arr(i)
If arr(j) < arr(i) Then
arr(i) = arr(j)
arr(j) = temp
End If
Next
Next
Selection_Sort = (Join(arr,","))
End Function
WScript.StdOut.Write "Pre-Sort" & vbTab & "Sorted"
WScript.StdOut.WriteLine
WScript.StdOut.Write "3,2,5,4,1" & vbTab & Selection_Sort("3,2,5,4,1")
WScript.StdOut.WriteLine
WScript.StdOut.Write "c,e,b,a,d" & vbTab & Selection_Sort("c,e,b,a,d")