2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,8 +1,19 @@
|
|||
{{Sorting Algorithm}}
|
||||
In this task, the goal is to sort an array of elements using the [[wp:Shell sort|Shell sort]] algorithm, a diminishing increment sort.
|
||||
The Shell sort is named after its inventor, Donald Shell, who published the algorithm in 1959.
|
||||
Shellsort is a sequence of interleaved insertion sorts based on an increment sequence.
|
||||
|
||||
;Task:
|
||||
Sort an array of elements using the [[wp:Shell sort|Shell sort]] algorithm, a diminishing increment sort.
|
||||
|
||||
The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959.
|
||||
|
||||
Shell sort is a sequence of interleaved insertion sorts based on an increment sequence.
|
||||
The increment size is reduced after each pass until the increment size is 1.
|
||||
|
||||
With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case".
|
||||
Any sequence will sort the data as long as it ends in 1, but some work better than others. Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice.
|
||||
[http://www.cs.princeton.edu/~rs/shell/] Other good sequences are found at the [https://oeis.org/search?q=shell+sort On-Line Encyclopedia of Integer Sequences].
|
||||
|
||||
Any sequence will sort the data as long as it ends in 1, but some work better than others.
|
||||
|
||||
Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice.
|
||||
[http://www.cs.princeton.edu/~rs/shell/]
|
||||
|
||||
Other good sequences are found at the [https://oeis.org/search?q=shell+sort On-Line Encyclopedia of Integer Sequences].
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
* Shell sort 24/06/2016
|
||||
SHELLSRT CSECT
|
||||
USING SHELLSRT,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) "
|
||||
ST R15,8(R13) "
|
||||
LR R13,R15 "
|
||||
L RK,N incr=n
|
||||
SRA RK,1 incr=n/2
|
||||
DO WHILE=(LTR,RK,P,RK) do while(incr>0)
|
||||
LA RI,1(RK) i=1+incr
|
||||
DO WHILE=(C,RI,LE,N) do i=1+incr to n
|
||||
LR RJ,RI j=i
|
||||
LR R1,RI i
|
||||
SLA R1,2 .
|
||||
L RT,A-4(R1) temp=a(i)
|
||||
LR R2,RK incr
|
||||
LA R2,1(R2) r2=incr+1
|
||||
LR R3,RJ j
|
||||
SR R3,RK j-incr
|
||||
SLA R3,2 *.
|
||||
LA R3,A-4(R3) r3=@a(j-incr)
|
||||
LR R4,RK incr
|
||||
SLA R4,2 r4=incr*4
|
||||
LR R5,RJ j
|
||||
SLA R5,2 .
|
||||
LA R5,A-4(R5) @a(j)
|
||||
* do while j-incr>=1 and a(j-incr)>temp
|
||||
DO WHILE=(CR,RJ,GE,R2,AND,C,RT,LT,0(R3))
|
||||
L R0,0(R3) a(j-incr)
|
||||
ST R0,0(R5) a(j)=a(j-incr)
|
||||
SR RJ,RK j=j-incr
|
||||
LR R5,R3 @a(j)
|
||||
SR R3,R4 @a(j-incr)=@a(j-incr)-incr*4
|
||||
ENDDO , end do
|
||||
ST RT,0(R5) a(j)=temp
|
||||
LA RI,1(RI) i=i+1
|
||||
ENDDO , end do
|
||||
IF C,RK,EQ,=F'2' if incr=2
|
||||
LA RK,1 incr=1
|
||||
ELSE , else
|
||||
LR R5,RK incr
|
||||
M R4,=F'5' *5
|
||||
D R4,=F'11' /11
|
||||
LR RK,R5 incr=incr*5/11
|
||||
ENDIF , end if
|
||||
ENDDO , end do
|
||||
LA R3,PG pgi=0
|
||||
LA RI,1 i=1
|
||||
DO WHILE=(C,RI,LE,N) do i=1 to n
|
||||
LR R1,RI i
|
||||
SLA R1,2 .
|
||||
L R2,A-4(R1) a(i)
|
||||
XDECO R2,XDEC edit a(i)
|
||||
MVC 0(4,R3),XDEC+8 output a(i)
|
||||
LA R3,4(R3) pgi=pgi+4
|
||||
LA RI,1(RI) i=i+1
|
||||
ENDDO , end do
|
||||
XPRNT PG,L'PG print buffer
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) "
|
||||
XR R15,R15 "
|
||||
BR R14 exit
|
||||
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
|
||||
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
|
||||
N DC A((N-A)/L'A) number of items of a
|
||||
PG DC CL80' ' buffer
|
||||
XDEC DS CL12 temp for xdeco
|
||||
YREGS
|
||||
RI EQU 6 i
|
||||
RJ EQU 7 j
|
||||
RK EQU 8 incr
|
||||
RT EQU 9 temp
|
||||
END SHELLSRT
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
defmodule Sort do
|
||||
def shell_sort(list) when length(list)<=1, do: list
|
||||
def shell_sort(list), do: shell_sort(list, div(length(list),2))
|
||||
|
||||
defp shell_sort(list, inc) do
|
||||
gb = Enum.with_index(list) |> Enum.group_by(fn {_,i} -> rem(i,inc) end)
|
||||
wk = Enum.map(0..inc-1, fn i ->
|
||||
Enum.map(gb[i], fn {x,_} -> x end) |> insert_sort([])
|
||||
end)
|
||||
|> merge
|
||||
if sorted?(wk), do: wk, else: shell_sort( wk, max(trunc(inc / 2.2), 1) )
|
||||
end
|
||||
|
||||
defp merge(lists) do
|
||||
len = length(hd(lists))
|
||||
Enum.map(lists, fn list -> if length(list)<len, do: list++[nil], else: list end)
|
||||
|> List.zip
|
||||
|> Enum.flat_map(fn tuple -> Tuple.to_list(tuple) end)
|
||||
|> Enum.filter(&(&1)) # remove nil
|
||||
end
|
||||
|
||||
defp sorted?(list) do
|
||||
Enum.chunk(list,2,1) |> Enum.all?(fn [a,b] -> a <= b end)
|
||||
end
|
||||
|
||||
defp insert_sort(list), do: insert_sort(list, [])
|
||||
|
||||
defp insert_sort([], sorted), do: sorted
|
||||
defp insert_sort([h | t], sorted), do: insert_sort(t, insert(h, sorted))
|
||||
|
||||
defp insert(x, []), do: [x]
|
||||
defp insert(x, sorted) when x < hd(sorted), do: [x | sorted]
|
||||
defp insert(x, [h | t]), do: [h | insert(x, t)]
|
||||
end
|
||||
|
||||
list = [0, 14, 11, 8, 13, 15, 5, 7, 16, 17, 1, 6, 12, 2, 10, 4, 19, 9, 18, 3]
|
||||
IO.inspect Sort.shell_sort(list)
|
||||
|
|
@ -2,7 +2,7 @@ shellSort[ lst_ ] := Module[ {list = lst, incr, temp, i, j},
|
|||
incr = Round[Length[list]/2];
|
||||
While[incr > 0,
|
||||
|
||||
For[i = incr + 1, i < Length[list], i++,
|
||||
For[i = incr + 1, i <= Length[list], i++,
|
||||
|
||||
temp = list[[i]]; j = i;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,48 +1,48 @@
|
|||
/*REXX program sorts a stemmed array using the shell sort algorithm. */
|
||||
call gen /*generate the array elements. */
|
||||
call show 'before sort' /*display the before array elements. */
|
||||
say copies('▒',75) /*displat a separator line (a fence). */
|
||||
call shellSort # /*invoke the shell sort. */
|
||||
call show ' after sort' /*display the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────GEN subroutine────────────────────────────*/
|
||||
gen: @.= /*assign a default value to stem array.*/
|
||||
@.1='3 character abbreviations for states of the USA' /*predates ZIP code.*/
|
||||
@.2='==============================================='
|
||||
@.3='RHO Rhode Island and Providence Plantations' ; @.36='NMX New Mexico'
|
||||
@.4='CAL California' ; @.20='NEV Nevada' ; @.37='IND Indiana'
|
||||
@.5='KAN Kansas' ; @.21='TEX Texas' ; @.38='MOE Missouri'
|
||||
@.6='MAS Massachusetts' ; @.22='VGI Virginia' ; @.39='COL Colorado'
|
||||
@.7='WAS Washington' ; @.23='OHI Ohio' ; @.40='CON Connecticut'
|
||||
@.8='HAW Hawaii' ; @.24='NHM New Hampshire'; @.41='MON Montana'
|
||||
@.9='NCR North Carolina'; @.25='MAE Maine' ; @.42='LOU Louisiana'
|
||||
@.10='SCR South Carolina'; @.26='MIC Michigan' ; @.43='IOW Iowa'
|
||||
@.11='IDA Idaho' ; @.27='MIN Minnesota' ; @.44='ORE Oregon'
|
||||
@.12='NDK North Dakota' ; @.28='MIS Mississippi' ; @.45='ARK Arkansas'
|
||||
@.13='SDK South Dakota' ; @.29='WIS Wisconsin' ; @.46='ARZ Arizona'
|
||||
@.14='NEB Nebraska' ; @.30='OKA Oklahoma' ; @.47='UTH Utah'
|
||||
@.15='DEL Delaware' ; @.31='ALA Alabama' ; @.48='KTY Kentucky'
|
||||
@.16='PEN Pennsylvania' ; @.32='FLA Florida' ; @.49='WVG West Virginia'
|
||||
@.17='TEN Tennessee' ; @.33='MLD Maryland' ; @.50='NWJ New Jersey'
|
||||
@.18='GEO Georgia' ; @.34='ALK Alaska' ; @.51='NYK New York'
|
||||
@.19='VER Vermont' ; @.35='ILL Illinois' ; @.52='WYO Wyoming'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*determine number of entries in array.*/
|
||||
return
|
||||
/*──────────────────────────────────SHELLSORT subroutine──────────────────────*/
|
||||
shellSort: procedure expose @.; parse arg N
|
||||
i=N%2 /*% is integer division in REXX. */
|
||||
do while i\==0
|
||||
do j=i+1 to N; k=j; p=k-i /*P: previous item*/
|
||||
_=@.j
|
||||
do while k>=i+1 & @.p>_; @.k=@.p
|
||||
k=k-i; p=k-i
|
||||
end /*while k≥i+1*/
|
||||
@.k=_
|
||||
end /*j*/
|
||||
/*REXX program sorts a stemmed array using the shell sort (shellsort) algorithm. */
|
||||
call gen /*generate the array elements. */
|
||||
call show 'before sort' /*display the before array elements. */
|
||||
say copies('▒', 75) /*displat a separator line (a fence). */
|
||||
call shellSort # /*invoke the shell sort. */
|
||||
call show ' after sort' /*display the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: @.= /*assign a default value to stem array.*/
|
||||
@.1='3 character abbreviations for states of the USA' /*predates ZIP code.*/
|
||||
@.2='==============================================='
|
||||
@.3='RHO Rhode Island and Providence Plantations' ; @.36='NMX New Mexico'
|
||||
@.4='CAL California' ; @.20='NEV Nevada' ; @.37='IND Indiana'
|
||||
@.5='KAN Kansas' ; @.21='TEX Texas' ; @.38='MOE Missouri'
|
||||
@.6='MAS Massachusetts' ; @.22='VGI Virginia' ; @.39='COL Colorado'
|
||||
@.7='WAS Washington' ; @.23='OHI Ohio' ; @.40='CON Connecticut'
|
||||
@.8='HAW Hawaii' ; @.24='NHM New Hampshire'; @.41='MON Montana'
|
||||
@.9='NCR North Carolina'; @.25='MAE Maine' ; @.42='LOU Louisiana'
|
||||
@.10='SCR South Carolina'; @.26='MIC Michigan' ; @.43='IOW Iowa'
|
||||
@.11='IDA Idaho' ; @.27='MIN Minnesota' ; @.44='ORE Oregon'
|
||||
@.12='NDK North Dakota' ; @.28='MIS Mississippi' ; @.45='ARK Arkansas'
|
||||
@.13='SDK South Dakota' ; @.29='WIS Wisconsin' ; @.46='ARZ Arizona'
|
||||
@.14='NEB Nebraska' ; @.30='OKA Oklahoma' ; @.47='UTH Utah'
|
||||
@.15='DEL Delaware' ; @.31='ALA Alabama' ; @.48='KTY Kentucky'
|
||||
@.16='PEN Pennsylvania' ; @.32='FLA Florida' ; @.49='WVG West Virginia'
|
||||
@.17='TEN Tennessee' ; @.33='MLD Maryland' ; @.50='NWJ New Jersey'
|
||||
@.18='GEO Georgia' ; @.34='ALK Alaska' ; @.51='NYK New York'
|
||||
@.19='VER Vermont' ; @.35='ILL Illinois' ; @.52='WYO Wyoming'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*determine number of entries in array.*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
shellSort: procedure expose @.; parse arg N /*obtain the N from the argument list*/
|
||||
i=N%2 /*% is integer division in REXX. */
|
||||
do while i\==0
|
||||
do j=i+1 to N; k=j; p=k-i /*P: previous item*/
|
||||
_=@.j
|
||||
do while k>=i+1 & @.p>_; @.k=@.p
|
||||
k=k-i; p=k-i
|
||||
end /*while k≥i+1*/
|
||||
@.k=_
|
||||
end /*j*/
|
||||
|
||||
if i==2 then i=1
|
||||
else i=i*5%11
|
||||
end /*while i¬==0*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW subroutine───────-───────────────────*/
|
||||
show: do j=1 for #; say 'element' right(j,length(#)) arg(1)': ' @.j; end; return
|
||||
if i==2 then i=1
|
||||
else i=i*5 % 11
|
||||
end /*while i¬==0*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do j=1 for #; say 'element' right(j,length(#)) arg(1)": " @.j; end; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue