Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,8 @@
|
|||
{{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. 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.
|
||||
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.
|
||||
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].
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void shell_sort (int *a, int n) {
|
||||
int h, i, j, k;
|
||||
int h, i, j, t;
|
||||
for (h = n; h /= 2;) {
|
||||
for (i = h; i < n; i++) {
|
||||
k = a[i];
|
||||
for (j = i; j >= h && k < a[j - h]; j -= h) {
|
||||
t = a[i];
|
||||
for (j = i; j >= h && t < a[j - h]; j -= h) {
|
||||
a[j] = a[j - h];
|
||||
}
|
||||
a[j] = k;
|
||||
a[j] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +16,11 @@ void shell_sort (int *a, int n) {
|
|||
int main (int ac, char **av) {
|
||||
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
|
||||
int n = sizeof a / sizeof a[0];
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
shell_sort(a, n);
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +1,54 @@
|
|||
/*REXX program sorts an (stemmed) array using the shellsort method. */
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call shellSort highItem /*invoke the shell sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
call show@ 'before sort' /*show the before array elements.*/
|
||||
call shellSort # /*invoke the shell sort. */
|
||||
call show@ ' after sort' /*show the after array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.= /*assign default value to stem. */
|
||||
@.1='3 character abbreviations for states of the USA' /*predates ZIP.*/
|
||||
@.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'
|
||||
@.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 highItem=1 while @.highItem\=='' /*find how many entries in array.*/
|
||||
end
|
||||
|
||||
highItem=highItem-1 /*adjust the highItem slightly.*/
|
||||
do #=1 while @.#\=='' /*find how many entries in array.*/
|
||||
end /*#*/
|
||||
#=#-1 /*adjust # of entries slightly.*/
|
||||
return
|
||||
/*──────────────────────────────────SHELLSORT subroutine───────---──────*/
|
||||
shellSort: procedure expose @.; parse arg highItem
|
||||
i=highItem%2
|
||||
/*──────────────────────────────────SHELLSORT subroutine────────────────*/
|
||||
shellSort: procedure expose @.; parse arg N
|
||||
i=N%2 /*integer divide N by two. */
|
||||
do while i\==0
|
||||
do j=i+1 to highItem; k=j; kmi=k-i
|
||||
_=@.j
|
||||
do while k>=i+1 & @.kmi>_; @.k=@.kmi
|
||||
k=k-i; kmi=k-i
|
||||
end /*while k>=i+1 & ...*/
|
||||
@.k=_
|
||||
end /*j*/
|
||||
do j=i+1 to N; k=j; kmi=k-i
|
||||
_=@.j
|
||||
do while k>=i+1 & @.kmi>_; @.k=@.kmi
|
||||
k=k-i; kmi=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@: widthH=length(highItem) /*maximum width of any line. */
|
||||
do j=1 for highItem
|
||||
say 'element' right(j,widthH) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
show@: do j=1 for #
|
||||
say 'element' right(j,length(#)) arg(1)': ' @.j
|
||||
end /*j*/
|
||||
say copies('─',79) /*show a separator line (a fence)*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
#lang racket
|
||||
(define (shell-sort! xs)
|
||||
(define (ref i) (vector-ref xs i))
|
||||
(define (new Δ) (if (= Δ 2) 1 (exact-floor (/ (* Δ 5) 11))))
|
||||
(define ref (curry vector-ref xs))
|
||||
(define (new Δ) (if (= Δ 2) 1 (quotient (* Δ 5) 11)))
|
||||
(let loop ([Δ (quotient (vector-length xs) 2)])
|
||||
(unless (= Δ 0)
|
||||
(for ([xi (in-vector xs)] [i (in-naturals)])
|
||||
(for ([xᵢ (in-vector xs)] [i (in-naturals)])
|
||||
(let while ([i i])
|
||||
(cond [(and (>= i Δ) (> (ref (- i Δ)) xi))
|
||||
(cond [(and (>= i Δ) (> (ref (- i Δ)) xᵢ))
|
||||
(vector-set! xs i (ref (- i Δ)))
|
||||
(while (- i Δ))]
|
||||
[(vector-set! xs i xi)])))
|
||||
[else (vector-set! xs i xᵢ)])))
|
||||
(loop (new Δ))))
|
||||
xs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue