|
| 1 | +# SortingAlgorithms |
| 2 | + |
| 3 | +[](https://travis-ci.org/StefanKarpinski/SortingAlgorithms.jl) |
| 4 | + |
| 5 | +The SortingAlgorithms package provides three sorting algorithms that can be used with Julia's [standard sorting APIs](http://docs.julialang.org/en/latest/stdlib/sort/): |
| 6 | + |
| 7 | +- [HeapSort] – an unstable, general purpose, in-place, O(n log n) comparison sort that works by heapify an array and repeatedly taking the maximal element from the heap. |
| 8 | +- [TimSort] – a stable, general purpose, hybrid, O(n log n) comparison sort that adapts to different common patterns of partially ordered input data. |
| 9 | +- [RadixSort] – a stable, special case, in-place, O(n) non-comparison sort that works by sorting data with fixed size, one digit at a time. |
| 10 | + |
| 11 | +[HeapSort] http://en.wikipedia.org/wiki/Heapsort |
| 12 | +[TimSort] http://en.wikipedia.org/wiki/Timsort |
| 13 | +[RadixSort] http://en.wikipedia.org/wiki/Radix_sort |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | + julia> using SortingAlgorithms |
| 18 | + |
| 19 | + julia> words = map(chomp,[readlines(open("/usr/share/dict/words"))...]) |
| 20 | + 235886-element Array{ASCIIString,1}: |
| 21 | + "A" |
| 22 | + "a" |
| 23 | + "aa" |
| 24 | + "aal" |
| 25 | + "aalii" |
| 26 | + ⋮ |
| 27 | + "zythem" |
| 28 | + "Zythia" |
| 29 | + "zythum" |
| 30 | + "Zyzomys" |
| 31 | + "Zyzzogeton" |
| 32 | + |
| 33 | + julia> sort!(words, alg=TimSort) |
| 34 | + 235886-element Array{ASCIIString,1}: |
| 35 | + "A" |
| 36 | + "Aani" |
| 37 | + "Aaron" |
| 38 | + "Aaronic" |
| 39 | + "Aaronical" |
| 40 | + ⋮ |
| 41 | + "zymotize" |
| 42 | + "zymotoxic" |
| 43 | + "zymurgy" |
| 44 | + "zythem" |
| 45 | + "zythum" |
| 46 | + |
| 47 | + julia> sort!(words, alg=TimSort, by=length) |
| 48 | + 235886-element Array{ASCIIString,1}: |
| 49 | + "A" |
| 50 | + "B" |
| 51 | + "C" |
| 52 | + "D" |
| 53 | + "E" |
| 54 | + ⋮ |
| 55 | + "formaldehydesulphoxylate" |
| 56 | + "pathologicopsychological" |
| 57 | + "scientificophilosophical" |
| 58 | + "tetraiodophenolphthalein" |
| 59 | + "thyroparathyroidectomize" |
| 60 | + |
| 61 | + julia> sort!(words, alg=HeapSort) |
| 62 | + 235886-element Array{ASCIIString,1}: |
| 63 | + "A" |
| 64 | + "Aani" |
| 65 | + "Aaron" |
| 66 | + "Aaronic" |
| 67 | + "Aaronical" |
| 68 | + ⋮ |
| 69 | + "zymotize" |
| 70 | + "zymotoxic" |
| 71 | + "zymurgy" |
| 72 | + "zythem" |
| 73 | + "zythum" |
| 74 | + |
| 75 | + julia> sort!(words, alg=HeapSort, by=length) |
| 76 | + 235886-element Array{ASCIIString,1}: |
| 77 | + "L" |
| 78 | + "p" |
| 79 | + "U" |
| 80 | + "I" |
| 81 | + "q" |
| 82 | + ⋮ |
| 83 | + "pathologicopsychological" |
| 84 | + "formaldehydesulphoxylate" |
| 85 | + "scientificophilosophical" |
| 86 | + "tetraiodophenolphthalein" |
| 87 | + "thyroparathyroidectomize" |
| 88 | + |
| 89 | + julia> sort!(words, alg=RadixSort) |
| 90 | + ERROR: Radix sort only sorts bits types (got ASCIIString) |
| 91 | + in error at error.jl:21 |
| 92 | + in sort! at /Users/stefan/.julia/SortingAlgorithms/src/SortingAlgorithms.jl:54 |
| 93 | + in sort! at sort.jl:328 |
| 94 | + in sort! at sort.jl:329 |
| 95 | + |
| 96 | + julia> floats = randn(1000) |
| 97 | + 1000-element Array{Float64,1}: |
| 98 | + 1.729 |
| 99 | + 0.907196 |
| 100 | + 0.461481 |
| 101 | + -0.204763 |
| 102 | + -0.16022 |
| 103 | + ⋮ |
| 104 | + 0.700683 |
| 105 | + -0.236204 |
| 106 | + -2.15634 |
| 107 | + -0.316188 |
| 108 | + -0.171478 |
| 109 | + |
| 110 | + julia> sort!(floats, alg=RadixSort) |
| 111 | + 1000-element Array{Float64,1}: |
| 112 | + -2.86255 |
| 113 | + -2.72041 |
| 114 | + -2.58234 |
| 115 | + -2.57259 |
| 116 | + -2.53046 |
| 117 | + ⋮ |
| 118 | + 3.08307 |
| 119 | + 3.12902 |
| 120 | + 3.15075 |
| 121 | + 3.20058 |
| 122 | + 3.23942 |
0 commit comments