Sorting algorithms/Gnome sort: Difference between revisions

→‎{{header|Swift}}: Added a SNOBOL4 implementation.
(Added solution for Action!)
(→‎{{header|Swift}}: Added a SNOBOL4 implementation.)
Line 3,348:
 
1 to: 10 do: [ :i | (o at: i) displayNl ].</lang>
 
=={{header|SNOBOL4}}==
 
Implementation of the Gnome sort. Note this is an overengineered approach that performs many checks the real world would need but might obfuscate intent. As such the actual implementation is carefully labelled and the rest can be ignored except as interest dictates.
 
<lang snobol4>*** GNOME SORT *****************************************************************
* GNOME(V) -- gnome sort of the numerical vector V.
*** HELPER FUNCTIONS ***********************************************************
* IDXMAX(V) -- highest index of vector V.
* IDXMIN(V) -- lowest index of vector V.
* NUMBER(V) -- succeeds if V is some form of numerical data, fails otherwise.
* NUMBERS(V) -- succeeds iff all elements of vector V are numerical
* SWAP(A,B) -- swaps the values of two variables (named with .var).
* VECTOR(V) -- succeeds iff V is a vector.
********************************************************************************
define('GNOME(V)I,J,K,L,H,T')
*
define('IDXMAX(V)')
define('IDXMIN(V)')
define('NUMBER(V)')
define('NUMBERS(V)I,M')
define('SWAP(SWAP_PARAMETER_VAR_A,SWAP_PARAMETER_VAR_B)')
define('VECTOR(V)')
:(GNOME.END)
 
****************************************
* GNOME FUNCTION *
****************************************
GNOME numbers(v) :F(FRETURN)
gnome = copy(v)
l = idxmin(v) ; h = idxmax(v) ; i = l + 1
GNOME.LOOP j = i - 1
le(i, l) :S(GNOME.FORWARD)
gt(i,h) :S(RETURN)
le(gnome<j>, gnome<i>) :S(GNOME.FORWARD)
swap(.gnome<j>, .gnome<i>)
i = i - 1 :(GNOME.LOOP)
GNOME.FORWARD i = i + 1 :(GNOME.LOOP)
 
****************************************
* HELPER FUNCTIONS *
****************************************
IDXMAX vector(v) :F(FRETURN)
prototype(v) ':' span('-' &digits) . idxmax :S(RETURN)F(IDXMAX.NORM)
IDXMAX.NORM idxmax = prototype(v) :(RETURN)
****************************************
IDXMIN vector(v) :F(FRETURN)
prototype(v) span('-' &digits) . idxmin ':' :S(RETURN)F(IDXMIN.NORM)
IDXMIN.NORM idxmin = 1 :(RETURN)
****************************************
NUMBER ident(datatype(v), 'INTEGER') :S(RETURN)
ident(datatype(v), 'REAL') :S(RETURN)F(FRETURN)
****************************************
NUMBERS vector(v) :F(FRETURN)
i = idxmin(v) ; m = idxmax(v)
NUMBERS.LOOP le(i,m) :F(RETURN)
number(v<i>) :F(FRETURN)
i = i + 1 :(NUMBERS.LOOP)
****************************************
SWAP SWAP = $SWAP_PARAMETER_VAR_A
$SWAP_PARAMETER_VAR_A = $SWAP_PARAMETER_VAR_B
$SWAP_PARAMETER_VAR_B = SWAP
SWAP = :(RETURN)
****************************************
VECTOR ident(v) :S(FRETURN)
prototype(v) ',' :S(FRETURN)F(RETURN)
****************************************
GNOME.END</lang>
 
The test script looks like this:
<lang snobol4>-INCLUDE 'gnome_sort.sno'
 
GNOME.TEST output = 'valid values...'
a = array(5)
a<1> = 50 ; a<2> = 40 ; a<3> = 10
a<4> = 30 ; a<5> = 20
b = gnome(a) :F(ERROR)
eq(b<1>,10) ; eq(b<2>,20) ; eq(b<3>,30) :F(ERROR)
eq(b<4>,40) ; eq(b<5>,50) :F(ERROR)
a = array('-2:2')
a<-2> = 50 ; a<-1> = 40 ; a<0> = 10
a<1> = 30 ; a<2> = 20
b = gnome(a) :F(ERROR)
eq(b<-2>,10) ; eq(b<-1>,20) ; eq(b<0>,30) :F(ERROR)
eq(b<1>,40) ; eq(b<2>,50) :F(ERROR)
a = array(5)
a<1> = 5.5 ; a<2> = 4.4 ; a<3> = 1.1
a<4> = 3.3 ; a<5> = 2.2
b = gnome(a) :F(ERROR)
eq(b<1>,1.1) ; eq(b<2>,2.2) ; eq(b<3>,3.3) :F(ERROR)
eq(b<4>,4.4) ; eq(b<5>,5.5) :F(ERROR)
output = 'invalid values...'
a = array(5, "five")
b = gnome(a) :S(ERROR)
a = array(5)
b = gnome(a) :S(ERROR)
output = 'PASSED!'
 
END</lang>
 
=={{header|Swift}}==