|
1 | 1 | # Sorting Algorithms |
2 | 2 |
|
3 | | - [](https://github.com/JuliaLang/SortingAlgorithms.jl/actions?query=workflow%3ACI+branch%3Amaster) |
| 3 | +[](https://github.com/JuliaLang/SortingAlgorithms.jl/actions?query=workflow%3ACI+branch%3Amaster) |
4 | 4 | [](https://coveralls.io/r/JuliaLang/SortingAlgorithms.jl) |
| 5 | +[](https://juliahub.com/ui/Packages/SortingAlgorithms/6dCmw?t=2) |
5 | 6 |
|
6 | | -The `SortingAlgorithms` package provides three sorting algorithms that can be used with Julia's [standard sorting API](https://docs.julialang.org/en/v1/base/sort/): |
| 7 | +The `SortingAlgorithms` package provides four sorting algorithms that can be used with Julia's [standard sorting API](https://docs.julialang.org/en/v1/base/sort/): |
7 | 8 |
|
8 | 9 | - [HeapSort] – an unstable, general purpose, in-place, O(n log n) comparison sort that works by heapifying an array and repeatedly taking the maximal element from the heap. |
9 | 10 | - [TimSort] – a stable, general purpose, hybrid, O(n log n) comparison sort that adapts to different common patterns of partially ordered input data. |
10 | | -- [RadixSort] – a stable, special case, O(n) non-comparison sort that works by sorting data with fixed size, one digit at a time. |
| 11 | +- [CombSort] – an unstable, general purpose, in-place, O(n log n) comparison sort with O(n^2) pathological cases that can attain good efficiency through SIMD instructions and instruction level parallelism on modern hardware. |
| 12 | +- [PagedMergeSort] – a stable, general purpose, O(n log n) time and O(sqrt n) space comparison sort. |
11 | 13 |
|
12 | | -[HeapSort]: http://en.wikipedia.org/wiki/Heapsort |
13 | | -[TimSort]: http://en.wikipedia.org/wiki/Timsort |
14 | | -[RadixSort]: http://en.wikipedia.org/wiki/Radix_sort |
| 14 | +[HeapSort]: https://en.wikipedia.org/wiki/Heapsort |
| 15 | +[TimSort]: https://en.wikipedia.org/wiki/Timsort |
| 16 | +[CombSort]: https://en.wikipedia.org/wiki/Comb_sort |
| 17 | +[PagedMergeSort]: https://link.springer.com/chapter/10.1007/BFb0016253 |
15 | 18 |
|
16 | 19 | ## Usage |
17 | 20 |
|
18 | 21 | ```jl |
19 | | - julia> using SortingAlgorithms |
| 22 | +julia> using SortingAlgorithms |
20 | 23 |
|
21 | | - julia> words = map(chomp,[readlines(open("/usr/share/dict/words"))...]) |
22 | | - 235886-element Array{ASCIIString,1}: |
23 | | - "A" |
24 | | - "a" |
25 | | - "aa" |
26 | | - "aal" |
27 | | - "aalii" |
28 | | - ⋮ |
29 | | - "zythem" |
30 | | - "Zythia" |
31 | | - "zythum" |
32 | | - "Zyzomys" |
33 | | - "Zyzzogeton" |
| 24 | +julia> words = map(chomp,[readlines(open("/usr/share/dict/words"))...]) |
| 25 | +235886-element Array{ASCIIString,1}: |
| 26 | + "A" |
| 27 | + "a" |
| 28 | + "aa" |
| 29 | + ⋮ |
| 30 | + "zythum" |
| 31 | + "Zyzomys" |
| 32 | + "Zyzzogeton" |
34 | 33 |
|
35 | | - julia> sort!(words, alg=TimSort) |
36 | | - 235886-element Array{ASCIIString,1}: |
37 | | - "A" |
38 | | - "Aani" |
39 | | - "Aaron" |
40 | | - "Aaronic" |
41 | | - "Aaronical" |
42 | | - ⋮ |
43 | | - "zymotize" |
44 | | - "zymotoxic" |
45 | | - "zymurgy" |
46 | | - "zythem" |
47 | | - "zythum" |
| 34 | +julia> sort!(words, alg=TimSort) |
| 35 | +235886-element Array{ASCIIString,1}: |
| 36 | + "A" |
| 37 | + "Aani" |
| 38 | + "Aaron" |
| 39 | + ⋮ |
| 40 | + "zymurgy" |
| 41 | + "zythem" |
| 42 | + "zythum" |
48 | 43 |
|
49 | | - julia> sort!(words, alg=TimSort, by=length) |
50 | | - 235886-element Array{ASCIIString,1}: |
51 | | - "A" |
52 | | - "B" |
53 | | - "C" |
54 | | - "D" |
55 | | - "E" |
56 | | - ⋮ |
57 | | - "formaldehydesulphoxylate" |
58 | | - "pathologicopsychological" |
59 | | - "scientificophilosophical" |
60 | | - "tetraiodophenolphthalein" |
61 | | - "thyroparathyroidectomize" |
| 44 | +julia> sort!(words, alg=TimSort, by=length) |
| 45 | +235886-element Array{ASCIIString,1}: |
| 46 | + "A" |
| 47 | + "B" |
| 48 | + "C" |
| 49 | + ⋮ |
| 50 | + "scientificophilosophical" |
| 51 | + "tetraiodophenolphthalein" |
| 52 | + "thyroparathyroidectomize" |
62 | 53 |
|
63 | | - julia> sort!(words, alg=HeapSort) |
64 | | - 235886-element Array{ASCIIString,1}: |
65 | | - "A" |
66 | | - "Aani" |
67 | | - "Aaron" |
68 | | - "Aaronic" |
69 | | - "Aaronical" |
70 | | - ⋮ |
71 | | - "zymotize" |
72 | | - "zymotoxic" |
73 | | - "zymurgy" |
74 | | - "zythem" |
75 | | - "zythum" |
| 54 | +julia> sort!(words, alg=HeapSort) |
| 55 | +235886-element Array{ASCIIString,1}: |
| 56 | + "A" |
| 57 | + "Aani" |
| 58 | + "Aaron" |
| 59 | + ⋮ |
| 60 | + "zymurgy" |
| 61 | + "zythem" |
| 62 | + "zythum" |
76 | 63 |
|
77 | | - julia> sort!(words, alg=HeapSort, by=length) |
78 | | - 235886-element Array{ASCIIString,1}: |
79 | | - "L" |
80 | | - "p" |
81 | | - "U" |
82 | | - "I" |
83 | | - "q" |
84 | | - ⋮ |
85 | | - "pathologicopsychological" |
86 | | - "formaldehydesulphoxylate" |
87 | | - "scientificophilosophical" |
88 | | - "tetraiodophenolphthalein" |
89 | | - "thyroparathyroidectomize" |
| 64 | +julia> sort!(words, alg=HeapSort, by=length) |
| 65 | +235886-element Array{ASCIIString,1}: |
| 66 | + "L" |
| 67 | + "p" |
| 68 | + "U" |
| 69 | + ⋮ |
| 70 | + "scientificophilosophical" |
| 71 | + "tetraiodophenolphthalein" |
| 72 | + "thyroparathyroidectomize" |
90 | 73 |
|
91 | | - julia> sort!(words, alg=RadixSort) |
92 | | - ERROR: Radix sort only sorts bits types (got ASCIIString) |
93 | | - in error at error.jl:21 |
94 | | - in sort! at /Users/stefan/.julia/SortingAlgorithms/src/SortingAlgorithms.jl:54 |
95 | | - in sort! at sort.jl:328 |
96 | | - in sort! at sort.jl:329 |
97 | | - |
98 | | - julia> floats = randn(1000) |
99 | | - 1000-element Array{Float64,1}: |
100 | | - 1.729 |
101 | | - 0.907196 |
102 | | - 0.461481 |
103 | | - -0.204763 |
104 | | - -0.16022 |
105 | | - ⋮ |
106 | | - 0.700683 |
107 | | - -0.236204 |
108 | | - -2.15634 |
109 | | - -0.316188 |
110 | | - -0.171478 |
111 | | - |
112 | | - julia> sort!(floats, alg=RadixSort) |
113 | | - 1000-element Array{Float64,1}: |
114 | | - -2.86255 |
115 | | - -2.72041 |
116 | | - -2.58234 |
117 | | - -2.57259 |
118 | | - -2.53046 |
119 | | - ⋮ |
120 | | - 3.08307 |
121 | | - 3.12902 |
122 | | - 3.15075 |
123 | | - 3.20058 |
124 | | - 3.23942 |
| 74 | +julia> sort!(randn(1000), alg=CombSort) |
| 75 | +1000-element Array{Float64,1}: |
| 76 | + -2.86255 |
| 77 | + -2.72041 |
| 78 | + -2.58234 |
| 79 | + ⋮ |
| 80 | + 3.15075 |
| 81 | + 3.20058 |
| 82 | + 3.23942 |
125 | 83 | ``` |
126 | 84 |
|
| 85 | +## Other packages that provide sorting algorithms |
| 86 | + |
| 87 | +While SortingAlgorithms.jl is the most widely used sorting package in the Julia ecosystem, other packages are available: |
| 88 | +- https://github.com/xiaodaigh/SortingLab.jl |
| 89 | +- https://github.com/JeffreySarnoff/SortingNetworks.jl |
| 90 | +- https://github.com/nlw0/ChipSort.jl |
0 commit comments