Shellsortering
Udseende
shellsortering (engelsk: Shellsort) er en sorteringsalgoritme som blev opdaget af Donald Shell i 1959.[1][2] Algoritmen kan ses som en forbedring af andre, enklere, sorteringsalgoritmer, typisk indsættelsessortering. Det der gør shellsortering mere effektiv end normal indsættelsessortering er at algoritmen tillader sammenligning mellem to elementer, som ligger langt fra hinanden. Algoritmens afviklingstid er voldsomt afhængig af den anvendte gapsekvens. For mange praktiske varianter er deres tidskompleksitet et uløst problem.[3][4]
Eksempelkode
[redigér | rediger kildetekst]Med brug af Marcin Ciuras gapsekvens, med en indre indsættelsessortering.
# Sort an array a[0...n-1].
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
# Start with the largest gap and work down to a gap of 1
foreach (gap in gaps)
{
# Do a gapped insertion sort for this gap size.
# The first gap elements a[0..gap-1] are already in gapped order
# keep adding one more element until the entire array is gap sorted
for (i = gap; i < n; i += 1)
{
# add a[i] to the elements that have been gap sorted
# save a[i] in temp and make a hole at position i
temp = a[i]
# shift earlier gap-sorted elements up until the correct location for a[i] is found
for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
{
a[j] = a[j - gap]
}
# put temp (the original a[i]) in its correct location
a[j] = temp
}
}
Referencer
[redigér | rediger kildetekst]- ^ Shell, D. L. (1959). "A High-Speed Sorting Procedure" (PDF). Communications of the ACM. 2 (7): 30-32. doi:10.1145/368370.368387. Arkiveret fra originalen (PDF) 30. august 2017. Hentet 26. november 2017.
- ^ Nogle ældre lærebøger og kilder kalder algortimen "Shell-Metzner" efter Marlene Metzner Norton, men Metzner har udtalt at hun intet havde at men den gøre: "I had nothing to do with the sort, and my name should never have been attached to it." Se "Shell sort". National Institute of Standards and Technology. Hentet 17. juli 2007.
- ^ "Shell sort". National Institute of Standards and Technology. Hentet 17. juli 2007.
- ^ Knuth, Donald E. (1997). "Shell's method". The Art of Computer Programming. Volume 3: Sorting and Searching (2nd udgave). Reading, Massachusetts: Addison-Wesley. s. 83-95. ISBN 0-201-89685-0.
Eksterne henvisninger
[redigér | rediger kildetekst]- "Elementary Sorts". algs4.cs.princeton.edu. Hentet 26. november 2017.
- Kennedy, Donald; Norman, Colin (2005), "What Don't We Know?", Science, 309 (5731): 75-75, doi:10.1126/science.309.5731.75, PMID 15994521
{{citation}}
: CS1-vedligeholdelse: url-status (link) - "So much more to know", Science, 309 (5731): 78-102, juli 2005, doi:10.1126/science.309.5731.78b, PMID 15994524
- Open Problem Garden Samling af uløste problemer inden for matematik