all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
24
Task/Sorting-algorithms-Gnome-sort/0DESCRIPTION
Normal file
24
Task/Sorting-algorithms-Gnome-sort/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{{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:
|
||||
'''function''' ''gnomeSort''(a[0..size-1])
|
||||
i := 1
|
||||
j := 2
|
||||
'''while''' i < size '''do'''
|
||||
'''if''' a[i-1] <= a[i] '''then'''
|
||||
''// for descending sort, use >= for comparison''
|
||||
i := j
|
||||
j := j + 1
|
||||
'''else'''
|
||||
'''swap''' a[i-1] '''and''' a[i]
|
||||
i := i - 1
|
||||
'''if''' i = 0 '''then'''
|
||||
i := j
|
||||
j := j + 1
|
||||
'''endif'''
|
||||
'''endif'''
|
||||
'''done'''
|
||||
|
||||
'''Task''': implement the Gnome sort in your language to sort an array (or list) of numbers.
|
||||
2
Task/Sorting-algorithms-Gnome-sort/1META.yaml
Normal file
2
Task/Sorting-algorithms-Gnome-sort/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Sorting Algorithms
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
MODE SORTSTRUCT = CHAR;
|
||||
|
||||
PROC inplace gnome sort = (REF[]SORTSTRUCT list)REF[]SORTSTRUCT:
|
||||
BEGIN
|
||||
INT i:=LWB list + 1, j:=LWB list + 2;
|
||||
WHILE i <= UPB list DO
|
||||
IF list[i-1] <= list[i] THEN
|
||||
i := j; j+:=1
|
||||
ELSE
|
||||
SORTSTRUCT swap = list[i-1]; list[i-1]:= list[i]; list[i]:= swap;
|
||||
i-:=1;
|
||||
IF i=LWB list THEN i:=j; j+:=1 FI
|
||||
FI
|
||||
OD;
|
||||
list
|
||||
END;
|
||||
|
||||
PROC gnome sort = ([]SORTSTRUCT seq)[]SORTSTRUCT:
|
||||
in place gnome sort(LOC[LWB seq: UPB seq]SORTSTRUCT:=seq);
|
||||
|
||||
[]SORTSTRUCT char array data = "big fjords vex quick waltz nymph";
|
||||
print((gnome sort(char array data), new line))
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
BEGIN {
|
||||
d[1] = 3.0
|
||||
d[2] = 4.0
|
||||
d[3] = 1.0
|
||||
d[4] = -8.4
|
||||
d[5] = 7.2
|
||||
d[6] = 4.0
|
||||
d[7] = 1.0
|
||||
d[8] = 1.2
|
||||
showD("Before: ")
|
||||
gnomeSortD()
|
||||
showD("Sorted: ")
|
||||
exit
|
||||
}
|
||||
|
||||
function gnomeSortD( i) {
|
||||
for (i = 2; i <= length(d); i++) {
|
||||
if (d[i] < d[i-1]) gnomeSortBackD(i)
|
||||
}
|
||||
}
|
||||
|
||||
function gnomeSortBackD(i, t) {
|
||||
for (; i > 1 && d[i] < d[i-1]; i--) {
|
||||
t = d[i]
|
||||
d[i] = d[i-1]
|
||||
d[i-1] = t
|
||||
}
|
||||
}
|
||||
|
||||
function showD(p, i) {
|
||||
printf p
|
||||
for (i = 1; i <= length(d); i++) {
|
||||
printf d[i] " "
|
||||
}
|
||||
print ""
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
function gnomeSort(array:Array)
|
||||
{
|
||||
var pos:uint = 0;
|
||||
while(pos < array.length)
|
||||
{
|
||||
if(pos == 0 || array[pos] >= array[pos-1])
|
||||
pos++;
|
||||
else
|
||||
{
|
||||
var tmp = array[pos];
|
||||
array[pos] = array[pos-1];
|
||||
array[pos-1] = tmp;
|
||||
pos--;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
type Index is (<>);
|
||||
type Collection is array(Index) of Element_Type;
|
||||
with function "<=" (Left, Right : Element_Type) return Boolean is <>;
|
||||
procedure Gnome_Sort(Item : in out Collection);
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
procedure Gnome_Sort(Item : in out Collection) is
|
||||
procedure Swap(Left, Right : in out Element_Type) is
|
||||
Temp : Element_Type := Left;
|
||||
begin
|
||||
Left := Right;
|
||||
Right := Temp;
|
||||
end Swap;
|
||||
|
||||
I : Integer := Index'Pos(Index'Succ(Index'First));
|
||||
J : Integer := I + 1;
|
||||
begin
|
||||
while I <= Index'Pos(Index'Last) loop
|
||||
if Item(Index'Val(I - 1)) <= Item(Index'Val(I)) then
|
||||
I := J;
|
||||
J := J + 1;
|
||||
else
|
||||
Swap(Item(Index'Val(I - 1)), Item(Index'Val(I)));
|
||||
I := I - 1;
|
||||
if I = Index'Pos(Index'First) then
|
||||
I := J;
|
||||
J := J + 1;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
end Gnome_Sort;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
with Gnome_Sort;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Gnome_Sort_Test is
|
||||
type Index is range 0..9;
|
||||
type Buf is array(Index) of Integer;
|
||||
procedure Sort is new Gnome_Sort(Integer, Index, Buf);
|
||||
A : Buf := (900, 700, 800, 600, 400, 500, 200, 100, 300, 0);
|
||||
begin
|
||||
for I in A'range loop
|
||||
Put(Integer'Image(A(I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
Sort(A);
|
||||
for I in A'range loop
|
||||
Put(Integer'Image(A(I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end Gnome_Sort_Test;
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
MsgBox % GnomeSort("")
|
||||
MsgBox % GnomeSort("xxx")
|
||||
MsgBox % GnomeSort("3,2,1")
|
||||
MsgBox % GnomeSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")
|
||||
|
||||
GnomeSort(var) { ; SORT COMMA SEPARATED LIST
|
||||
StringSplit a, var, `, ; make array, size = a0
|
||||
i := 2, j := 3
|
||||
While i <= a0 { ; stop when sorted
|
||||
u := i-1
|
||||
If (a%u% < a%i%) ; search for pairs to swap
|
||||
i := j, j := j+1
|
||||
Else { ; swap
|
||||
t := a%u%, a%u% := a%i%, a%i% := t
|
||||
If (--i = 1) ; restart search
|
||||
i := j, j++
|
||||
}
|
||||
}
|
||||
Loop % a0 ; construct string from sorted array
|
||||
sorted .= "," . a%A_Index%
|
||||
Return SubStr(sorted,2) ; drop leading comma
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
dim a(0 to n-1) as integer
|
||||
'...more code...
|
||||
sort:
|
||||
i = 1
|
||||
j = 2
|
||||
|
||||
while(i < ubound(a) - lbound(a))
|
||||
if a(i-1) <= a(i) then
|
||||
i = j
|
||||
j = j + 1
|
||||
else
|
||||
swap a(i-1), a(i)
|
||||
i = i - 1
|
||||
if i = 0 then
|
||||
i = j
|
||||
j = j + 1
|
||||
end if
|
||||
end if
|
||||
wend
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
DEF PROC_GnomeSort1(Size%)
|
||||
I%=2
|
||||
J%=2
|
||||
REPEAT
|
||||
IF data%(J%-1) <=data%(J%) THEN
|
||||
I%+=1
|
||||
J%=I%
|
||||
ELSE
|
||||
SWAP data%(J%-1),data%(J%)
|
||||
J%-=1
|
||||
IF J%=1 THEN
|
||||
I%+=1
|
||||
J%=I%
|
||||
ENDIF
|
||||
ENDIF
|
||||
UNTIL I%>Size%
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
template<typename RandomAccessIterator>
|
||||
void gnomeSort(RandomAccessIterator begin, RandomAccessIterator end) {
|
||||
RandomAccessIterator i = begin + 1;
|
||||
RandomAccessIterator j = begin + 2;
|
||||
|
||||
while(i < end) {
|
||||
if(*(i - 1) <= *i) {
|
||||
i = j;
|
||||
++j;
|
||||
} else {
|
||||
std::iter_swap(i - 1, i);
|
||||
--i;
|
||||
if(i == begin) {
|
||||
i = j;
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
void gnome_sort(int *a, int n)
|
||||
{
|
||||
int i=1, j=2, t;
|
||||
# define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; }
|
||||
while(i < n) {
|
||||
if (a[i - 1] > a[i]) {
|
||||
swap(i - 1, i);
|
||||
if (--i) continue;
|
||||
}
|
||||
i = j++;
|
||||
}
|
||||
# undef swap
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
C-SORT SECTION.
|
||||
C-000.
|
||||
DISPLAY "SORT STARTING".
|
||||
|
||||
SET WB-IX-1 TO 2.
|
||||
MOVE 1 TO WC-NEXT-POSN.
|
||||
|
||||
PERFORM E-GNOME UNTIL WC-NEXT-POSN > WC-SIZE.
|
||||
|
||||
DISPLAY "SORT FINISHED".
|
||||
|
||||
C-999.
|
||||
EXIT.
|
||||
|
||||
E-GNOME SECTION.
|
||||
E-000.
|
||||
IF WB-ENTRY(WB-IX-1 - 1) NOT > WB-ENTRY(WB-IX-1)
|
||||
ADD 1 TO WC-NEXT-POSN
|
||||
SET WB-IX-1 TO WC-NEXT-POSN
|
||||
ELSE
|
||||
MOVE WB-ENTRY(WB-IX-1 - 1) TO WC-TEMP
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WB-ENTRY(WB-IX-1 - 1)
|
||||
MOVE WC-TEMP TO WB-ENTRY(WB-IX-1)
|
||||
SET WB-IX-1 DOWN BY 1
|
||||
IF WB-IX-1 = 1
|
||||
ADD 1 TO WC-NEXT-POSN
|
||||
SET WB-IX-1 TO WC-NEXT-POSN.
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(defn gnomesort
|
||||
([c] (gnomesort c <))
|
||||
([c pred]
|
||||
(loop [x [] [y1 & ys :as y] (seq c)]
|
||||
(cond (empty? y) x
|
||||
(empty? x) (recur (list y1) ys)
|
||||
true (let [zx (last x)]
|
||||
(if (pred y1 zx)
|
||||
(recur (butlast x) (concat (list y1 zx) ys))
|
||||
(recur (concat x (list y1)) ys)))))))
|
||||
|
||||
(println (gnomesort [3 1 4 1 5 9 2 6 5]))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(defun gnome-sort (array predicate &aux (length (length array)))
|
||||
(do ((position (min 1 length)))
|
||||
((eql length position) array)
|
||||
(cond
|
||||
((eql 0 position)
|
||||
(incf position))
|
||||
((funcall predicate
|
||||
(aref array position)
|
||||
(aref array (1- position)))
|
||||
(rotatef (aref array position)
|
||||
(aref array (1- position)))
|
||||
(decf position))
|
||||
(t (incf position)))))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void gnomeSort(T)(T arr) {
|
||||
int i = 1, j = 2;
|
||||
while (i < arr.length) {
|
||||
if (arr[i-1] <= arr[i]) {
|
||||
i = j;
|
||||
j++;
|
||||
} else {
|
||||
swap(arr[i-1], arr[i]);
|
||||
i--;
|
||||
if (i == 0) {
|
||||
i = j;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto a = [3,4,2,5,1,6];
|
||||
gnomeSort(a);
|
||||
writeln(a);
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
procedure GnomeSort(a : array of Integer);
|
||||
var
|
||||
i, j : Integer;
|
||||
begin
|
||||
i := 1;
|
||||
j := 2;
|
||||
while i < a.Length do begin
|
||||
if a[i-1] <= a[i] then begin
|
||||
i := j;
|
||||
j := j + 1;
|
||||
end else begin
|
||||
a.Swap(i-1, i);
|
||||
i := i - 1;
|
||||
if i = 0 then begin
|
||||
i := j;
|
||||
j := j + 1;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
var i : Integer;
|
||||
var a := new Integer[16];
|
||||
|
||||
Print('{');
|
||||
for i := 0 to a.High do begin
|
||||
a[i] := i xor 5;
|
||||
Print(Format('%3d ', [a[i]]));
|
||||
end;
|
||||
PrintLn('}');
|
||||
|
||||
GnomeSort(a);
|
||||
|
||||
Print('{');
|
||||
for i := 0 to a.High do
|
||||
Print(Format('%3d ', [a[i]]));
|
||||
PrintLn('}');
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
program TestGnomeSort;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
{.$DEFINE DYNARRAY} // remove '.' to compile with dynamic array
|
||||
|
||||
type
|
||||
TItem = Integer; // declare ordinal type for array item
|
||||
{$IFDEF DYNARRAY}
|
||||
TArray = array of TItem; // dynamic array
|
||||
{$ELSE}
|
||||
TArray = array[0..15] of TItem; // static array
|
||||
{$ENDIF}
|
||||
|
||||
procedure GnomeSort(var A: TArray);
|
||||
var
|
||||
Item: TItem;
|
||||
I, J: Integer;
|
||||
|
||||
begin
|
||||
I:= Low(A) + 1;
|
||||
J:= Low(A) + 2;
|
||||
while I <= High(A) do begin
|
||||
if A[I - 1] <= A[I] then begin
|
||||
I:= J;
|
||||
J:= J + 1;
|
||||
end
|
||||
else begin
|
||||
Item:= A[I - 1];
|
||||
A[I - 1]:= A[I];
|
||||
A[I]:= Item;
|
||||
I:= I - 1;
|
||||
if I = Low(A) then begin
|
||||
I:= J;
|
||||
J:= J + 1;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
A: TArray;
|
||||
I: Integer;
|
||||
|
||||
begin
|
||||
{$IFDEF DYNARRAY}
|
||||
SetLength(A, 16);
|
||||
{$ENDIF}
|
||||
for I:= Low(A) to High(A) do
|
||||
A[I]:= Random(100);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
GnomeSort(A);
|
||||
for I:= Low(A) to High(A) do
|
||||
Write(A[I]:3);
|
||||
Writeln;
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
def gnomeSort(array) {
|
||||
var size := array.size()
|
||||
var i := 1
|
||||
var j := 2
|
||||
while (i < size) {
|
||||
if (array[i-1] <= array[i]) {
|
||||
i := j
|
||||
j += 1
|
||||
} else {
|
||||
def t := array[i-1]
|
||||
array[i-1] := array[i]
|
||||
array[i] := t
|
||||
i -= 1
|
||||
if (i <=> 0) {
|
||||
i := j
|
||||
j += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
? def a := [7,9,4,2,1,3,6,5,0,8].diverge()
|
||||
# value: [7, 9, 4, 2, 1, 3, 6, 5, 0, 8].diverge()
|
||||
|
||||
? gnomeSort(a)
|
||||
? a
|
||||
# value: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].diverge()
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
-module(gnome_sort).
|
||||
-export([gnome/1]).
|
||||
|
||||
gnome(L, []) -> L;
|
||||
gnome([Prev|P], [Next|N]) when Next > Prev ->
|
||||
gnome(P, [Next|[Prev|N]]);
|
||||
gnome(P, [Next|N]) ->
|
||||
gnome([Next|P], N).
|
||||
gnome([H|T]) -> gnome([H], T).
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Eshell V5.7.3 (abort with ^G)
|
||||
1> c(gnome_sort).
|
||||
{ok,gnome_sort}
|
||||
2> gnome_sort:gnome([8,3,9,1,3,2,6]).
|
||||
[1,2,3,3,6,8,9]
|
||||
3>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
function gnomeSort(sequence s)
|
||||
integer i,j
|
||||
object temp
|
||||
i = 1
|
||||
j = 2
|
||||
while i < length(s) do
|
||||
if compare(s[i], s[i+1]) <= 0 then
|
||||
i = j
|
||||
j += 1
|
||||
else
|
||||
temp = s[i]
|
||||
s[i] = s[i+1]
|
||||
s[i+1] = temp
|
||||
i -= 1
|
||||
if i = 0 then
|
||||
i = j
|
||||
j += 1
|
||||
end if
|
||||
end if
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
class Main
|
||||
{
|
||||
Int[] gnomesort (Int[] list)
|
||||
{
|
||||
i := 1
|
||||
j := 2
|
||||
while (i < list.size)
|
||||
{
|
||||
if (list[i-1] <= list[i])
|
||||
{
|
||||
i = j
|
||||
j += 1
|
||||
}
|
||||
else
|
||||
{
|
||||
list.swap(i-1, i)
|
||||
i -= 1
|
||||
if (i == 0)
|
||||
{
|
||||
i = j
|
||||
j += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
Void main ()
|
||||
{
|
||||
list := [4,1,5,8,2,1,5,7]
|
||||
echo ("" + list + " sorted is " + gnomesort (list))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
defer precedes
|
||||
defer exchange
|
||||
|
||||
: gnomesort ( a n)
|
||||
swap >r 1 ( n c)
|
||||
begin ( n c)
|
||||
over over > ( n c f)
|
||||
while ( n c)
|
||||
dup if ( n c)
|
||||
dup dup 1- over over r@ precedes
|
||||
if r@ exchange 1- else drop drop 1+ then
|
||||
else 1+ then ( n c)
|
||||
repeat drop drop r> drop ( --)
|
||||
;
|
||||
|
||||
create example
|
||||
8 93 69 52 50 79 33 52 19 77 , , , , , , , , , ,
|
||||
|
||||
:noname >r cells r@ + @ swap cells r> + @ swap < ; is precedes
|
||||
:noname >r cells r@ + swap cells r> + over @ over @ swap rot ! swap ! ; is exchange
|
||||
|
||||
: .array 10 0 do example i cells + ? loop cr ;
|
||||
|
||||
.array example 10 gnomesort .array
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
: gnomesort+ ( a n)
|
||||
swap >r 2 tuck 1- ( c2 n c1)
|
||||
begin ( c2 n c1)
|
||||
over over > ( c2 n c1 f)
|
||||
while ( c2 n c1)
|
||||
dup if ( c2 n c1)
|
||||
dup dup 1- over over r@ precedes
|
||||
if r@ exchange 1- else drop drop drop >r dup 1+ swap r> swap then
|
||||
else drop >r dup 1+ swap r> swap then
|
||||
repeat drop drop drop r> drop
|
||||
; ( --)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
program example
|
||||
|
||||
implicit none
|
||||
|
||||
integer :: array(8) = (/ 2, 8, 6, 1, 3, 5, 4, 7 /)
|
||||
|
||||
call Gnomesort(array)
|
||||
write(*,*) array
|
||||
|
||||
contains
|
||||
|
||||
subroutine Gnomesort(a)
|
||||
|
||||
integer, intent(in out) :: a(:)
|
||||
integer :: i, j, temp
|
||||
|
||||
i = 2
|
||||
j = 3
|
||||
do while (i <= size(a))
|
||||
if (a(i-1) <= a(i)) then
|
||||
i = j
|
||||
j = j + 1
|
||||
else
|
||||
temp = a(i-1)
|
||||
a(i-1) = a(i)
|
||||
a(i) = temp
|
||||
i = i - 1
|
||||
if (i == 1) then
|
||||
i = j
|
||||
j = j + 1
|
||||
end if
|
||||
end if
|
||||
end do
|
||||
|
||||
end subroutine Gnomesort
|
||||
|
||||
end program example
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := []int{170, 45, 75, -90, -802, 24, 2, 66}
|
||||
fmt.Println("before:", a)
|
||||
gnomeSort(a)
|
||||
fmt.Println("after: ", a)
|
||||
}
|
||||
|
||||
func gnomeSort(a []int) {
|
||||
for i, j := 1, 2; i < len(a); {
|
||||
if a[i-1] > a[i] {
|
||||
a[i-1], a[i] = a[i], a[i-1]
|
||||
i--
|
||||
if i > 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
i = j
|
||||
j++
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := []int{170, 45, 75, -90, -802, 24, 2, 66}
|
||||
fmt.Println("before:", a)
|
||||
gnomeSort(sort.IntSlice(a))
|
||||
fmt.Println("after: ", a)
|
||||
}
|
||||
|
||||
func gnomeSort(a sort.Interface) {
|
||||
for i, j := 1, 2; i < a.Len(); {
|
||||
if a.Less(i, i-1) {
|
||||
a.Swap(i-1, i)
|
||||
i--
|
||||
if i > 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
i = j
|
||||
j++
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] }
|
||||
|
||||
def checkSwap = { list, i, j = i+1 -> [(list[i] > list[j])].find{ it }.each { makeSwap(list, i, j) } }
|
||||
|
||||
def gnomeSort = { input ->
|
||||
def swap = checkSwap.curry(input)
|
||||
def index = 1
|
||||
while (index < input.size()) {
|
||||
index += (swap(index-1) && index > 1) ? -1 : 1
|
||||
}
|
||||
input
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println (gnomeSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]))
|
||||
println (gnomeSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
gnomeSort [] = []
|
||||
gnomeSort (x:xs) = gs [x] xs
|
||||
where
|
||||
gs vv@(v:vs) (w:ws)
|
||||
| v<=w = gs (w:vv) ws
|
||||
| otherwise = gs vs (w:v:ws)
|
||||
gs [] (y:ys) = gs [y] ys
|
||||
gs xs [] = reverse xs
|
||||
-- keeping the first argument of gs in reverse order avoids the deterioration into cubic behaviour
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure gnomesort(X,op) #: return sorted list
|
||||
local i,j
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
j := (i := 2) + 1 # translation of pseudo code
|
||||
while i <= *X do {
|
||||
if op(X[i],X[i-1]) then {
|
||||
X[i] :=: X[i -:= 1]
|
||||
if i > 1 then next
|
||||
}
|
||||
j := (i := j) + 1
|
||||
}
|
||||
return X
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
List do(
|
||||
gnomeSortInPlace := method(
|
||||
idx := 1
|
||||
while(idx <= size,
|
||||
if(idx == 0 or at(idx) > at(idx - 1)) then(
|
||||
idx = idx + 1
|
||||
) else(
|
||||
swapIndices(idx, idx - 1)
|
||||
idx = idx - 1
|
||||
)
|
||||
)
|
||||
self)
|
||||
)
|
||||
|
||||
lst := list(5, -1, -4, 2, 9)
|
||||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
List do(
|
||||
gnomeSortInPlace := method(
|
||||
idx1 := 1
|
||||
idx2 := 2
|
||||
|
||||
while(idx1 <= size,
|
||||
if(idx1 == 0 or at(idx1) > at(idx1 - 1)) then(
|
||||
idx1 = idx2
|
||||
idx2 = idx2 + 1
|
||||
) else(
|
||||
swapIndices(idx1, idx1 - 1)
|
||||
idx1 = idx1 - 1
|
||||
)
|
||||
)
|
||||
self)
|
||||
)
|
||||
|
||||
lst := list(5, -1, -4, 2, 9)
|
||||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
position=: 0 {.@I.@, [
|
||||
swap=: ] A.~ *@position * #@[ !@- <:@position
|
||||
gnome=: swap~ 2 >/\ ]
|
||||
gnomes=: gnome^:_
|
||||
|
|
@ -0,0 +1 @@
|
|||
swap=: position (] {~ _1 0 + [)`(0 _1 + [)`]}^:(*@[) ]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
gt=: -.@(-: /:~)@,&<
|
||||
gnome=: swap~ 2 gt/\ ]
|
||||
|
|
@ -0,0 +1 @@
|
|||
gnomeps=: gnome^:a:
|
||||
|
|
@ -0,0 +1 @@
|
|||
2 ~:/\ gnomeps
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
public static void gnomeSort(int[] a)
|
||||
{
|
||||
int i=1;
|
||||
int j=2;
|
||||
|
||||
while(i < a.length) {
|
||||
if ( a[i-1] <= a[i] ) {
|
||||
i = j; j++;
|
||||
} else {
|
||||
int tmp = a[i-1];
|
||||
a[i-1] = a[i];
|
||||
a[i--] = tmp;
|
||||
i = (i==0) ? j++ : i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function gnomeSort(a) {
|
||||
function moveBack(i) {
|
||||
for( ; i > 0 && a[i-1] > a[i]; i--) {
|
||||
var t = a[i];
|
||||
a[i] = a[i-1];
|
||||
a[i-1] = t;
|
||||
}
|
||||
}
|
||||
for (var i = 1; i < a.length; i++) {
|
||||
if (a[i-1] > a[i]) moveBack(i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
function gnomeSort(a)
|
||||
local i, j = 2, 3
|
||||
|
||||
while i < #a do
|
||||
if a[i-1] <= a[i] then
|
||||
i = j
|
||||
j = j + 1
|
||||
else
|
||||
a[i-1], a[i] = a[i], a[i-1] -- swap
|
||||
i = i - 1
|
||||
if i == 1 then -- 1 instead of 0
|
||||
i = j
|
||||
j = j + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
list = { 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97 }
|
||||
gnomeSort(list)
|
||||
for i, j in pairs(list) do
|
||||
print(j)
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
function list = gnomeSort(list)
|
||||
|
||||
i = 2;
|
||||
j = 3;
|
||||
|
||||
while i <= numel(list)
|
||||
|
||||
if list(i-1) <= list(i)
|
||||
i = j;
|
||||
j = j+1;
|
||||
else
|
||||
list([i-1 i]) = list([i i-1]); %Swap
|
||||
i = i-1;
|
||||
if i == 1
|
||||
i = j;
|
||||
j = j+1;
|
||||
end
|
||||
end %if
|
||||
|
||||
end %while
|
||||
end %gnomeSort
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> gnomeSort([4 3 1 5 6 2])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
gnomeSort[list_]:=Module[{i=2,j=3},
|
||||
While[ i<=Length[[list]],
|
||||
If[ list[[i-1]]<=list[[i]],
|
||||
i=j; j++;,
|
||||
list[[i-1;;i]]=list[[i;;i-1]];i--;];
|
||||
If[i==1,i=j;j++;]
|
||||
]]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def gnomesort(suffix v)(expr n) =
|
||||
begingroup save i, j, t;
|
||||
i := 1; j := 2;
|
||||
forever: exitif not (i < n);
|
||||
if v[i-1] <= v[i]:
|
||||
i := j; j := j + 1;
|
||||
else:
|
||||
t := v[i-1];
|
||||
v[i-1] := v[i];
|
||||
v[i] := t;
|
||||
i := i - 1;
|
||||
i := if i=0: j; j := j + 1 else: i fi;
|
||||
fi
|
||||
endfor
|
||||
endgroup enddef;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
numeric a[];
|
||||
for i = 0 upto 9: a[i] := uniformdeviate(40); message decimal a[i]; endfor
|
||||
message char10;
|
||||
|
||||
gnomesort(a, 10);
|
||||
for i = 0 upto 9: message decimal a[i]; endfor
|
||||
end
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
import java.util.List
|
||||
|
||||
i1 = ArrayList(Arrays.asList([Integer(3), Integer(3), Integer(1), Integer(2), Integer(4), Integer(3), Integer(5), Integer(6)]))
|
||||
say i1.toString
|
||||
say gnomeSort(i1).toString
|
||||
|
||||
return
|
||||
|
||||
method isTrue public constant binary returns boolean
|
||||
return 1 == 1
|
||||
|
||||
method isFalse public constant binary returns boolean
|
||||
return \isTrue
|
||||
|
||||
method gnomeSort(a = String[], sAsc = isTrue) public constant binary returns String[]
|
||||
|
||||
rl = String[a.length]
|
||||
al = List gnomeSort(Arrays.asList(a), sAsc)
|
||||
al.toArray(rl)
|
||||
|
||||
return rl
|
||||
|
||||
method gnomeSort(a = List, sAsc = isTrue) public constant binary returns ArrayList
|
||||
|
||||
sDsc = \sAsc -- Ascending/descending switches
|
||||
ra = ArrayList(a)
|
||||
i_ = 1
|
||||
j_ = 2
|
||||
loop label i_ while i_ < ra.size
|
||||
ctx = (Comparable ra.get(i_ - 1)).compareTo(Comparable ra.get(i_))
|
||||
if (sAsc & ctx <= 0) | (sDsc & ctx >= 0) then do
|
||||
i_ = j_
|
||||
j_ = j_ + 1
|
||||
end
|
||||
else do
|
||||
swap = ra.get(i_)
|
||||
ra.set(i_, ra.get(i_ - 1))
|
||||
ra.set(i_ - 1, swap)
|
||||
i_ = i_ - 1
|
||||
if i_ = 0 then do
|
||||
i_ = j_
|
||||
j_ = j_ + 1
|
||||
end
|
||||
end
|
||||
end i_
|
||||
|
||||
return ra
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
# let gnome_sort a =
|
||||
let i = ref 1
|
||||
and j = ref 2 in
|
||||
while !i < Array.length a do
|
||||
if a.(!i-1) <= a.(!i) then
|
||||
begin
|
||||
i := !j;
|
||||
j := !j + 1;
|
||||
end else begin
|
||||
swap a (!i-1) (!i);
|
||||
i := !i - 1;
|
||||
if !i = 0 then begin
|
||||
i := !j;
|
||||
j := !j + 1;
|
||||
end;
|
||||
end;
|
||||
done;
|
||||
;;
|
||||
val gnome_sort : 'a array -> unit = <fun>
|
||||
|
||||
# let a = [| 7; 9; 4; 2; 1; 3; 6; 5; 0; 8; |] ;;
|
||||
val a : int array = [|7; 9; 4; 2; 1; 3; 6; 5; 0; 8|]
|
||||
|
||||
# gnome_sort a ;;
|
||||
- : unit = ()
|
||||
|
||||
# a ;;
|
||||
- : int array = [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
function : GnomeSort(a : Int[]) {
|
||||
i := 1;
|
||||
j := 2;
|
||||
|
||||
while(i < a->Size()) {
|
||||
if (a[i-1] <= a[i]) {
|
||||
i := j;
|
||||
j += 1;
|
||||
}
|
||||
else {
|
||||
tmp := a[i-1];
|
||||
a[i-1] := a[i];
|
||||
a[i] := tmp;
|
||||
i -= 1;
|
||||
if(i = 0) {
|
||||
i := j;
|
||||
j += 1;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function s = gnomesort(v)
|
||||
i = 2; j = 3;
|
||||
while ( i <= length(v) )
|
||||
if ( v(i-1) <= v(i) )
|
||||
i = j;
|
||||
j++;
|
||||
else
|
||||
t = v(i-1);
|
||||
v(i-1) = v(i);
|
||||
v(i) = t;
|
||||
i--;
|
||||
if ( i == 1 )
|
||||
i = j;
|
||||
j++;
|
||||
endif
|
||||
endif
|
||||
endwhile
|
||||
s = v;
|
||||
endfunction
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
v = [7, 9, 4, 2, 1, 3, 6, 5, 0, 8];
|
||||
disp(gnomesort(v));
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
declare
|
||||
fun {GnomeSort Xs}
|
||||
case Xs of nil then nil
|
||||
[] X|Xr then {Loop [X] Xr}
|
||||
end
|
||||
end
|
||||
|
||||
fun {Loop Vs Ws}
|
||||
case [Vs Ws]
|
||||
of [V|_ W|Wr] andthen V =< W then {Loop W|Vs Wr}
|
||||
[] [V|Vr W|Wr] then {Loop Vr W|V|Wr}
|
||||
[] [nil W|Wr] then {Loop [W] Wr}
|
||||
[] [Vs nil ] then {Reverse Vs}
|
||||
end
|
||||
end
|
||||
in
|
||||
{Show {GnomeSort [3 1 4 1 5 9 2 6 5]}}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
gnomeSort(v)={
|
||||
my(i=2,j=3,n=#v,t);
|
||||
while(i<=n,
|
||||
if(v[i-1]>v[i],
|
||||
t=v[i];
|
||||
v[i]=v[i-1];
|
||||
v[i--]=t;
|
||||
if(i==1, i=j; j++);
|
||||
,
|
||||
i=j;
|
||||
j++
|
||||
)
|
||||
);
|
||||
v
|
||||
};
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function gnomeSort($arr){
|
||||
$i = 1;
|
||||
$j = 2;
|
||||
while($i < count($arr)){
|
||||
if ($arr[$i-1] <= $arr[$i]){
|
||||
$i = $j;
|
||||
$j++;
|
||||
}else{
|
||||
list($arr[$i],$arr[$i-1]) = array($arr[$i-1],$arr[$i]);
|
||||
$i--;
|
||||
if($i == 0){
|
||||
$i = $j;
|
||||
$j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
$arr = array(3,1,6,2,9,4,7,8,5);
|
||||
echo implode(',',gnomeSort($arr));
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
procedure gnomesort(var arr : Array of Integer; size : Integer);
|
||||
var
|
||||
i, j, t : Integer;
|
||||
begin
|
||||
i := 1;
|
||||
j := 2;
|
||||
while i < size do begin
|
||||
if arr[i-1] <= arr[i] then
|
||||
begin
|
||||
i := j;
|
||||
j := j + 1
|
||||
end
|
||||
else begin
|
||||
t := arr[i-1];
|
||||
arr[i-1] := arr[i];
|
||||
arr[i] := t;
|
||||
i := i - 1;
|
||||
if i = 0 then begin
|
||||
i := j;
|
||||
j := j + 1
|
||||
end
|
||||
end
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
sub gnome_sort (@a is rw) {
|
||||
my ($i, $j) = 1, 2;
|
||||
while $i < @a {
|
||||
if @a[$i - 1] <= @a[$i] {
|
||||
($i, $j) = $j, $j + 1;
|
||||
}
|
||||
else {
|
||||
(@a[$i - 1], @a[$i]) = @a[$i], @a[$i - 1];
|
||||
--$i or ($i, $j) = $j, $j + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
use strict;
|
||||
|
||||
sub gnome_sort
|
||||
{
|
||||
my @a = @_;
|
||||
|
||||
my $size = scalar(@a);
|
||||
my $i = 1;
|
||||
my $j = 2;
|
||||
while($i < $size) {
|
||||
if ( $a[$i-1] <= $a[$i] ) {
|
||||
$i = $j;
|
||||
$j++;
|
||||
} else {
|
||||
@a[$i, $i-1] = @a[$i-1, $i];
|
||||
$i--;
|
||||
if ($i == 0) {
|
||||
$i = $j;
|
||||
$j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return @a;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my @arr = ( 10, 9, 8, 5, 2, 1, 1, 0, 50, -2 );
|
||||
print "$_\n" foreach gnome_sort( @arr );
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(de gnomeSort (Lst)
|
||||
(let J (cdr Lst)
|
||||
(for (I Lst (cdr I))
|
||||
(if (>= (cadr I) (car I))
|
||||
(setq I J J (cdr J))
|
||||
(xchg I (cdr I))
|
||||
(if (== I Lst)
|
||||
(setq I J J (cdr J))
|
||||
(setq I (prior I Lst)) ) ) ) )
|
||||
Lst )
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
SUB gnomeSort (a() AS LONG)
|
||||
DIM i AS LONG, j AS LONG
|
||||
i = 1
|
||||
j = 2
|
||||
WHILE (i < UBOUND(a) + 1)
|
||||
IF (a(i - 1) <= a(i)) THEN
|
||||
i = j
|
||||
INCR j
|
||||
ELSE
|
||||
SWAP a(i - 1), a(i)
|
||||
DECR i
|
||||
IF 0 = i THEN
|
||||
i = j
|
||||
INCR j
|
||||
END IF
|
||||
END IF
|
||||
WEND
|
||||
END SUB
|
||||
|
||||
FUNCTION PBMAIN () AS LONG
|
||||
DIM n(9) AS LONG, x AS LONG
|
||||
RANDOMIZE TIMER
|
||||
OPEN "output.txt" FOR OUTPUT AS 1
|
||||
FOR x = 0 TO 9
|
||||
n(x) = INT(RND * 9999)
|
||||
PRINT #1, n(x); ",";
|
||||
NEXT
|
||||
PRINT #1,
|
||||
gnomeSort n()
|
||||
FOR x = 0 TO 9
|
||||
PRINT #1, n(x); ",";
|
||||
NEXT
|
||||
CLOSE 1
|
||||
END FUNCTION
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
Procedure GnomeSort(Array a(1))
|
||||
Protected Size = ArraySize(a()) + 1
|
||||
Protected i = 1, j = 2
|
||||
|
||||
While i < Size
|
||||
If a(i - 1) <= a(i)
|
||||
;for descending SORT, use >= for comparison
|
||||
i = j
|
||||
j + 1
|
||||
Else
|
||||
Swap a(i - 1), a(i)
|
||||
i - 1
|
||||
If i = 0
|
||||
i = j
|
||||
j + 1
|
||||
EndIf
|
||||
EndIf
|
||||
Wend
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
>>> def gnomesort(a):
|
||||
i,j,size = 1,2,len(a)
|
||||
while i < size:
|
||||
if a[i-1] <= a[i]:
|
||||
i,j = j, j+1
|
||||
else:
|
||||
a[i-1],a[i] = a[i],a[i-1]
|
||||
i -= 1
|
||||
if i == 0:
|
||||
i,j = j, j+1
|
||||
return a
|
||||
|
||||
>>> gnomesort([3,4,2,5,1,6])
|
||||
[1, 2, 3, 4, 5, 6]
|
||||
>>>
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
gnomesort <- function(x)
|
||||
{
|
||||
i <- 1
|
||||
j <- 1
|
||||
while(i < length(x))
|
||||
{
|
||||
if(x[i] <= x[i+1])
|
||||
{
|
||||
i <- j
|
||||
j <- j+1
|
||||
} else
|
||||
{
|
||||
temp <- x[i]
|
||||
x[i] <- x[i+1]
|
||||
x[i+1] <- temp
|
||||
i <- i - 1
|
||||
if(i == 0)
|
||||
{
|
||||
i <- j
|
||||
j <- j+1
|
||||
}
|
||||
}
|
||||
}
|
||||
x
|
||||
}
|
||||
gnomesort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*REXX program sorts an array using the gnome-sort method. */
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show "before" array elements.*/
|
||||
call gnomeSort highItem /*invoke the infamous gnome sort.*/
|
||||
call show@ ' after sort' /*show "after" array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────gnomeSORT subroutine─────────────*/
|
||||
gnomeSort: procedure expose @.; parse arg n; k=2
|
||||
|
||||
do j=3 while k<=n; km=k-1
|
||||
if @.km<=@.k then k=j
|
||||
else do
|
||||
_=@.km /*swap two entries in the array. */
|
||||
@.km=@.k
|
||||
@.k=_
|
||||
k=k-1; if k==1 then k=j
|
||||
end
|
||||
end /*j*/
|
||||
return
|
||||
/*─────────────────────────────────────GEN@ subroutine──────────────────*/
|
||||
gen@: @.= /*assign a default value (null). */
|
||||
@.1='---the seven virtues---'
|
||||
@.2='======================='
|
||||
@.3='Faith'
|
||||
@.4='Hope'
|
||||
@.5='Charity [Love]'
|
||||
@.6='Fortitude'
|
||||
@.7='Justice'
|
||||
@.8='Prudence'
|
||||
@.9='Temperance'
|
||||
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/
|
||||
end /*not much of a DO loop, eh? */
|
||||
highItem=highItem-1 /*because of DO, adjust highItem.*/
|
||||
return
|
||||
/*─────────────────────────────────────SHOW@ subroutine─────────────────*/
|
||||
show@: widthH=length(highItem) /*the maximum width of any line. */
|
||||
do j=1 for highItem /*show and tell time for array. */
|
||||
say 'element' right(j,widthH) arg(1)":" @.j /*make it pretty.*/
|
||||
end /*j*/
|
||||
say copies('─',60) /*show a separator line that fits*/
|
||||
return
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
import List;
|
||||
|
||||
public list[int] gnomeSort(a){
|
||||
i = 1;
|
||||
j = 2;
|
||||
while(i < size(a)){
|
||||
if(a[i-1] <= a[i]){
|
||||
i = j;
|
||||
j += 1;}
|
||||
else{
|
||||
temp = a[i-1];
|
||||
a[i-1] = a[i];
|
||||
a[i] = temp;
|
||||
i = i - 1;
|
||||
if(i == 0){
|
||||
i = j;
|
||||
j += 1;}}}
|
||||
return a;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
gnomeSort([4, 65, 2, -31, 0, 99, 83, 782, 1])
|
||||
list[int]: [-31,0,1,2,4,65,83,99,782]
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
class Array
|
||||
def gnomesort!
|
||||
i, j = 1, 2
|
||||
while i < length
|
||||
if self[i-1] <= self[i]
|
||||
i, j = j, j+1
|
||||
else
|
||||
self[i-1], self[i] = self[i], self[i-1]
|
||||
i -= 1
|
||||
if i == 0
|
||||
i, j = j, j+1
|
||||
end
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
ary = [7,6,5,9,8,4,3,1,2,0]
|
||||
ary.gnomesort!
|
||||
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
; supply comparison function, which returns true if first and second
|
||||
; arguments are in order or equal.
|
||||
(define (gnome-sort-compar in-order input-list)
|
||||
(let gnome ((p (list (car input-list)))
|
||||
(n (cdr input-list)))
|
||||
(if (null? n) ; no more flowerpots?
|
||||
p ; we're done
|
||||
(let ((prev-pot (car p))
|
||||
(next-pot (car n)))
|
||||
(if (in-order next-pot prev-pot)
|
||||
; if the pots are in order, step forwards.
|
||||
; otherwise, exchange the two pots, and step backwards.
|
||||
(gnome (cons next-pot p) ; Prev list grows
|
||||
(cdr n)) ; Next list shorter by one
|
||||
(if (null? (cdr p)) ; are we at the beginning?
|
||||
(gnome ; if so, we can't step back
|
||||
(list next-pot) ; simply exchange the pots without
|
||||
(cons prev-pot (cdr n))) ; changing lengths of lists
|
||||
(gnome
|
||||
(cdr p) ; Prev list shorter by one
|
||||
(cons next-pot (cons prev-pot (cdr n))))))))))
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
Smalltalk at: #gnomesort put: nil.
|
||||
|
||||
"Utility"
|
||||
OrderedCollection extend [
|
||||
swap: a with: b [ |t|
|
||||
t := self at: a.
|
||||
self at: a put: b.
|
||||
self at: b put: t
|
||||
]
|
||||
].
|
||||
|
||||
"Gnome sort as block closure"
|
||||
gnomesort := [ :c |
|
||||
|i j|
|
||||
i := 2. j := 3.
|
||||
[ i <= (c size) ]
|
||||
whileTrue: [
|
||||
(c at: (i-1)) <= (c at: i)
|
||||
ifTrue: [
|
||||
i := j copy.
|
||||
j := j + 1.
|
||||
]
|
||||
ifFalse: [
|
||||
c swap: (i-1) with: i.
|
||||
i := i - 1.
|
||||
i == 1
|
||||
ifTrue: [
|
||||
i := j copy.
|
||||
j := j + 1
|
||||
]
|
||||
]
|
||||
]
|
||||
].
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
|r o|
|
||||
r := Random new.
|
||||
o := OrderedCollection new.
|
||||
|
||||
1 to: 10 do: [ :i | o add: (r between: 0 and: 50) ].
|
||||
|
||||
gnomesort value: o.
|
||||
|
||||
1 to: 10 do: [ :i | (o at: i) displayNl ].
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package require Tcl 8.5
|
||||
package require struct::list
|
||||
|
||||
proc gnomesort {a} {
|
||||
set i 1
|
||||
set j 2
|
||||
set size [llength $a]
|
||||
while {$i < $size} {
|
||||
if {[lindex $a [expr {$i - 1}]] <= [lindex $a $i]} {
|
||||
set i $j
|
||||
incr j
|
||||
} else {
|
||||
struct::list swap a [expr {$i - 1}] $i
|
||||
incr i -1
|
||||
if {$i == 0} {
|
||||
set i $j
|
||||
incr j
|
||||
}
|
||||
}
|
||||
}
|
||||
return $a
|
||||
}
|
||||
|
||||
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
gnome_sort "p" =
|
||||
|
||||
@NiX ^=lx -+ # iteration
|
||||
~&r?\~& @lNXrX ->llx2rhPlrPCTxPrtPX~&lltPlhPrCXPrX ~&ll&& @llPrXh not "p", # backward bubble
|
||||
->~&rhPlCrtPX ~&r&& ~&lZ!| @bh "p"+- # forward scan
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#import std
|
||||
#cast %s
|
||||
|
||||
t = gnome_sort(lleq) 'dfijhkwlawfkjnksdjnoqwjefgflkdsgioi'
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
function gnomeSort( a )
|
||||
dim i
|
||||
dim j
|
||||
i = 1
|
||||
j = 2
|
||||
do while i < ubound( a ) + 1
|
||||
if a(i-1) <= a(i) then
|
||||
i = j
|
||||
j = j + 1
|
||||
else
|
||||
swap a(i-1), a(i)
|
||||
i = i - 1
|
||||
if i = 0 then
|
||||
i = j
|
||||
j = j + 1
|
||||
end if
|
||||
end if
|
||||
loop
|
||||
gnomeSort = a
|
||||
end function
|
||||
|
||||
sub swap( byref x, byref y )
|
||||
dim temp
|
||||
temp = x
|
||||
x = y
|
||||
y = temp
|
||||
end sub
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
dim a
|
||||
dim b
|
||||
|
||||
a = array( "zanzibar", "aardvark","ampicillin","zulu","gogodala", "wolverhampton")
|
||||
b = gnomeSort( a )
|
||||
wscript.echo join(a, ", ")
|
||||
|
||||
a = array( 234,567,345,568,2345,89,547,23,649,5769,456,456,567)
|
||||
b = gnomeSort( a )
|
||||
wscript.echo join(a, ", ")
|
||||
|
||||
a = array( true, false, true, true, false, false, true, false)
|
||||
b = gnomeSort( a )
|
||||
wscript.echo join(a, ", ")
|
||||
|
||||
a = array( 1,2,2,4,67789,-3,-45.99)
|
||||
b = gnomeSort( a )
|
||||
wscript.echo join(a, ", ")
|
||||
|
||||
a = array( now(), now()-1,now()+2)
|
||||
b = gnomeSort( a )
|
||||
wscript.echo join(a, ", ")
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
aardvark, ampicillin, gogodala, wolverhampton, zanzibar, zulu
|
||||
23, 89, 234, 345, 456, 456, 547, 567, 567, 568, 649, 2345, 5769
|
||||
True, True, True, True, False, False, False, False
|
||||
-45.99, -3, 1, 2, 2, 4, 67789
|
||||
2/02/2010 10:19:51 AM, 3/02/2010 10:19:51 AM, 5/02/2010 10:19:51 AM
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
code ChOut=8, IntOut=11;
|
||||
|
||||
proc GnomeSort(A(0..Size-1)); \Sort array A
|
||||
int A, Size;
|
||||
int I, J, T;
|
||||
[I:= 1;
|
||||
J:= 2;
|
||||
while I < Size do
|
||||
if A(I-1) <= A(I) then
|
||||
[\ for descending sort, use >= for comparison
|
||||
I:= J;
|
||||
J:= J+1;
|
||||
]
|
||||
else
|
||||
[T:= A(I-1); A(I-1):= A(I); A(I):= T; \swap
|
||||
I:= I-1;
|
||||
if I = 0 then
|
||||
[I:= J;
|
||||
J:= J+1;
|
||||
];
|
||||
];
|
||||
];
|
||||
|
||||
int A, I;
|
||||
[A:= [3, 1, 4, 1, -5, 9, 2, 6, 5, 4];
|
||||
GnomeSort(A, 10);
|
||||
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue