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

@ -1,5 +1,7 @@
{{Sorting Algorithm}}
{{Wikipedia|Gnome sort}}
Gnome sort is a sorting algorithm which is similar to [[Insertion sort]], except that moving an element to its proper place is accomplished by a series of swaps, as in [[Bubble Sort]].
The pseudocode for the algorithm is:
@ -21,4 +23,7 @@ The pseudocode for the algorithm is:
'''endif'''
'''done'''
'''Task''': implement the Gnome sort in your language to sort an array (or list) of numbers.
;Task:
Implement the Gnome sort in your language to sort an array (or list) of numbers.
<br><br>

View file

@ -0,0 +1,40 @@
import extensions.
import system'routines.
extension $op
{
gnomeSort
[
var list := self clone.
int i := 1.
int j := 2.
while (i < list length)
[
if (list[i-1]<=list[i])
[
i := j.
j += 1
];
[
list exchange(i-1,i).
i -= 1.
if (i==0)
[
i := 1.
j := 2
]
]
].
^ list
]
}
program =
[
var list := (3, 9, 4, 6, 8, 1, 7, 2, 5).
console printLine("before:", list).
console printLine("after :", list gnomeSort).
].

View file

@ -0,0 +1,39 @@
Public Sub Main()
Dim siCount As Short
Dim siCounti As Short = 1
Dim siCountj As Short = 2
Dim siToSort As Short[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24]
Print "To sort: - ";
GoSub Display
While siCounti < siToSort.Count
If siToSort[siCounti - 1] <= siToSort[siCounti] Then
siCounti = siCountj
Inc siCountj
Else
Swap siToSort[siCounti - 1], siToSort[siCounti]
Dec siCounti
If siCounti = 0 Then
siCounti = siCountj
Inc siCountj
Endif
Endif
Wend
Print "Sorted: - ";
GoSub Display
Return
'--------------------------------------------
Display:
For siCount = 0 To siToSort.Max
Print Format(Str(siToSort[siCount]), "####");
If siCount <> siToSort.max Then Print ",";
Next
Print
Return
End

View file

@ -1,12 +1,11 @@
function gnomesort(A::AbstractVector)
i = 1
j = 2
while i < length(A)
if A[i] <= A[i + 1]
function gnomesort!(arr::Vector)
i, j = 1, 2
while i < length(arr)
if arr[i] ≤ arr[i+1]
i = j
j += 1
else
A[i], A[i + 1] = A[i + 1], A[i]
arr[i], arr[i+1] = arr[i+1], arr[i]
i -= 1
if i == 0
i = j
@ -14,9 +13,8 @@ function gnomesort(A::AbstractVector)
end
end
end
A
return arr
end
A = randcycle(20)
println("unsorted: ", A)
println("sorted: ", gnomesort(A))
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", bogosort!(v))

View file

@ -0,0 +1,14 @@
arr := Array([17,3,72,0,36,2,3,8,40,0]):
len := numelems(arr):
i := 2:
while (i <= len) do
if (i=1 or arr[i] >= arr[i-1]) then
i++:
else
temp:= arr[i]:
arr[i] := arr[i-1]:
arr[i-1] := temp:
i--:
end if:
end do:
arr;