Compare sorting algorithms' performance: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 34: | Line 34: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{in progress|lang=AutoHotkey|day=16|month=1|year=2010}} |
{{in progress|lang=AutoHotkey|day=16|month=1|year=2010}} |
||
< |
<syntaxhighlight lang="ahk">; BUGGY - FIX |
||
#Persistent |
#Persistent |
||
Line 337: | Line 337: | ||
Sort, var, N D`, |
Sort, var, N D`, |
||
Return var |
Return var |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> HIMEM = PAGE + 2000000 |
||
INSTALL @lib$+"SORTLIB" |
INSTALL @lib$+"SORTLIB" |
||
INSTALL @lib$+"TIMERLIB" |
INSTALL @lib$+"TIMERLIB" |
||
Line 503: | Line 503: | ||
ENDWHILE |
ENDWHILE |
||
a%() += l% |
a%() += l% |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 547: | Line 547: | ||
===Sequence generators=== |
===Sequence generators=== |
||
<tt>csequence.h</tt> |
<tt>csequence.h</tt> |
||
< |
<syntaxhighlight lang="c">#ifndef _CSEQUENCE_H |
||
#define _CSEQUENCE_H |
#define _CSEQUENCE_H |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 555: | Line 555: | ||
void fillwithrrange(double *v, int n); |
void fillwithrrange(double *v, int n); |
||
void shuffledrange(double *v, int n); |
void shuffledrange(double *v, int n); |
||
#endif</ |
#endif</syntaxhighlight> |
||
<tt>csequence.c</tt> |
<tt>csequence.c</tt> |
||
< |
<syntaxhighlight lang="c">#include "csequence.h" |
||
static double fill_constant; |
static double fill_constant; |
||
Line 587: | Line 587: | ||
v[r] = t; |
v[r] = t; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===Write timings=== |
===Write timings=== |
||
We shall use the code from [[Query Performance]]. Since the ''action'' is a generic function with a single argument, we need ''wrappers'' which encapsule each sorting algorithms we want to test. |
We shall use the code from [[Query Performance]]. Since the ''action'' is a generic function with a single argument, we need ''wrappers'' which encapsule each sorting algorithms we want to test. |
||
<tt>writetimings.h</tt> |
<tt>writetimings.h</tt> |
||
< |
<syntaxhighlight lang="c">#ifndef _WRITETIMINGS_H |
||
#define _WRITETIMINGS_H |
#define _WRITETIMINGS_H |
||
#include "sorts.h" |
#include "sorts.h" |
||
Line 630: | Line 630: | ||
}; |
}; |
||
typedef struct seqdef seqdef_t; |
typedef struct seqdef seqdef_t; |
||
#endif</ |
#endif</syntaxhighlight> |
||
<tt>writetimings.c</tt> |
<tt>writetimings.c</tt> |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 735: | Line 735: | ||
free(tobesorted); |
free(tobesorted); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
This code produce several files with the following naming convention: |
This code produce several files with the following naming convention: |
||
* data_''algorithm''_''sequence''.dat |
* data_''algorithm''_''sequence''.dat |
||
Where ''algorithm'' is one of the following: insertion, merge, shell, quick, qsort (the quicksort in the libc library) (bubble sort became too slow for longest sequences). ''Sequence'' is ''c1'' (constant value 1), ''rr'' (reverse range), ''sr'' (shufled range). These data can be easily plotted by Gnuplot, which can also do fitting. Instead we do our fitting using [[Polynomial Fitting]]. |
Where ''algorithm'' is one of the following: insertion, merge, shell, quick, qsort (the quicksort in the libc library) (bubble sort became too slow for longest sequences). ''Sequence'' is ''c1'' (constant value 1), ''rr'' (reverse range), ''sr'' (shufled range). These data can be easily plotted by Gnuplot, which can also do fitting. Instead we do our fitting using [[Polynomial Fitting]]. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 768: | Line 768: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Here we search for a fit with C<sub>0</sub>+C<sub>1</sub>x "in the log scale", since we supposed the data, once plotted on a logscale graph, can be fitted by a line. We can use e.g. a shell one-liner to produce the parameters for the line for each data file previously output. In particular I've used the following |
Here we search for a fit with C<sub>0</sub>+C<sub>1</sub>x "in the log scale", since we supposed the data, once plotted on a logscale graph, can be fitted by a line. We can use e.g. a shell one-liner to produce the parameters for the line for each data file previously output. In particular I've used the following |
||
<pre>for el in *.dat ; do fitdata <$el >${el%.dat}.fd ; done</pre> |
<pre>for el in *.dat ; do fitdata <$el >${el%.dat}.fd ; done</pre> |
||
Line 794: | Line 794: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.container, std.datetime, |
||
std.random, std.typetuple; |
std.random, std.typetuple; |
||
Line 941: | Line 941: | ||
nRuns.benchmark!(randomBubble, randomInsertion, |
nRuns.benchmark!(randomBubble, randomInsertion, |
||
randomHeap, randomBuiltIn).show; |
randomHeap, randomBuiltIn).show; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Timings in microseconds: |
<pre>Timings in microseconds: |
||
Line 966: | Line 966: | ||
The sort routines are borrowed from [http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort bubble sort], [http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort insertion sort] and [http://rosettacode.org/wiki/Sorting_algorithms/Quicksort quick sort]. Plots by [https://github.com/psyeugenic/eplot eplot]. |
The sort routines are borrowed from [http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort bubble sort], [http://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort insertion sort] and [http://rosettacode.org/wiki/Sorting_algorithms/Quicksort quick sort]. Plots by [https://github.com/psyeugenic/eplot eplot]. |
||
Bubble sort does [http://github.com/ebengt/rosettacode/tree/master/graphs/ones.png ones] and [http://github.com/ebengt/rosettacode/tree/master/graphs/ranges.png ranges] best. Insertion sort does [http://github.com/ebengt/rosettacode/tree/master/graphs/reversed_ranges.png reversed ranges] best. Quick sort handles [http://github.com/ebengt/rosettacode/tree/master/graphs/shuffleds.png shuffled numbers] best. |
Bubble sort does [http://github.com/ebengt/rosettacode/tree/master/graphs/ones.png ones] and [http://github.com/ebengt/rosettacode/tree/master/graphs/ranges.png ranges] best. Insertion sort does [http://github.com/ebengt/rosettacode/tree/master/graphs/reversed_ranges.png reversed ranges] best. Quick sort handles [http://github.com/ebengt/rosettacode/tree/master/graphs/shuffleds.png shuffled numbers] best. |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( compare_sorting_algorithms ). |
-module( compare_sorting_algorithms ). |
||
Line 994: | Line 994: | ||
{Time, _Result} = timer:tc( fun() -> Fun( List_fun(N) ) end ), |
{Time, _Result} = timer:tc( fun() -> Fun( List_fun(N) ) end ), |
||
{math:log10(N), math:log10(Time)}. |
{math:log10(N), math:log10(Time)}. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{libheader|gonum/plot}} |
{{libheader|gonum/plot}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,219: | Line 1,219: | ||
ps.Shape = plotutil.DefaultGlyphShapes[dx] |
ps.Shape = plotutil.DefaultGlyphShapes[dx] |
||
p.Add(pl, ps) |
p.Add(pl, ps) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[[file:GoComp.png|right|Comparison]] |
[[file:GoComp.png|right|Comparison]] |
||
Line 1,230: | Line 1,230: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Time.Clock |
||
import Data.List |
import Data.List |
||
Line 1,259: | Line 1,259: | ||
where |
where |
||
step x = (x * a + c) `mod` m |
step x = (x * a + c) `mod` m |
||
(a, c, m) = (1103515245, 12345, 2^31-1)</ |
(a, c, m) = (1103515245, 12345, 2^31-1)</syntaxhighlight> |
||
As a result of testing we get the list of tuples: length of a list and time in microseconds: |
As a result of testing we get the list of tuples: length of a list and time in microseconds: |
||
Line 1,269: | Line 1,269: | ||
Different sorting methods: |
Different sorting methods: |
||
< |
<syntaxhighlight lang="haskell">-- Naive quick sort |
||
qsort :: Ord a => Sorter a |
qsort :: Ord a => Sorter a |
||
qsort [] = [] |
qsort [] = [] |
||
Line 1,285: | Line 1,285: | ||
-- Insertion sort |
-- Insertion sort |
||
isort :: Ord a => Sorter a |
isort :: Ord a => Sorter a |
||
isort = foldr insert []</ |
isort = foldr insert []</syntaxhighlight> |
||
Finally, charting routines and the task implementation: |
Finally, charting routines and the task implementation: |
||
< |
<syntaxhighlight lang="haskell">-- chart appears to be logarithmic scale on both axes |
||
barChart :: Char -> [(Int, Time)] -> [String] |
barChart :: Char -> [(Int, Time)] -> [String] |
||
barChart ch lst = bar . scale <$> lst |
barChart ch lst = bar . scale <$> lst |
||
Line 1,326: | Line 1,326: | ||
run rand |
run rand |
||
where |
where |
||
run = comparison [sort, isort, qsort, bsort] "siqb"</ |
run = comparison [sort, isort, qsort, bsort] "siqb"</syntaxhighlight> |
||
<pre>λ> main |
<pre>λ> main |
||
Line 1,482: | Line 1,482: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
NB. extracts from other rosetta code projects |
NB. extracts from other rosetta code projects |
||
ts=: 6!:2, 7!:2@] |
ts=: 6!:2, 7!:2@] |
||
Line 1,565: | Line 1,565: | ||
(_*pts) + n +./ .*1 0 2|:coef&(p."1) plot x |
(_*pts) + n +./ .*1 0 2|:coef&(p."1) plot x |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 1,718: | Line 1,718: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript"> |
||
function swap(a, i, j){ |
function swap(a, i, j){ |
||
var t = a[i] |
var t = a[i] |
||
Line 1,904: | Line 1,904: | ||
console.log(sOut) |
console.log(sOut) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,935: | Line 1,935: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Julia comes with the InsertionSort, MergeSort, and QuickSort routines built into the Base.Sort module. Here is a comparison using those system algorithms and with integers. |
Julia comes with the InsertionSort, MergeSort, and QuickSort routines built into the Base.Sort module. Here is a comparison using those system algorithms and with integers. |
||
< |
<syntaxhighlight lang="julia"> |
||
function comparesorts(tosort) |
function comparesorts(tosort) |
||
a = shuffle(["i", "m", "q"]) |
a = shuffle(["i", "m", "q"]) |
||
Line 1,972: | Line 1,972: | ||
println("Average sort times for 40000 randomized:") |
println("Average sort times for 40000 randomized:") |
||
println("\tinsertion sort:\t$iavg\n\tmerge sort:\t$mavg\n\tquick sort\t$qavg") |
println("\tinsertion sort:\t$iavg\n\tmerge sort:\t$mavg\n\tquick sort\t$qavg") |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre>Average sort times for 40000 ones: |
<pre>Average sort times for 40000 ones: |
||
insertion sort: 0.00036058316000000005 |
insertion sort: 0.00036058316000000005 |
||
Line 1,997: | Line 1,997: | ||
Although it would be easy enough to plot the results graphically using an external library such as JFreePlot, there doesn't seem much point when we can no longer upload images to RC. I've therefore presented the results in tabular form on the terminal which is good enough to detect significant trends. |
Although it would be easy enough to plot the results graphically using an external library such as JFreePlot, there doesn't seem much point when we can no longer upload images to RC. I've therefore presented the results in tabular form on the terminal which is good enough to detect significant trends. |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.31 |
||
import java.util.Random |
import java.util.Random |
||
Line 2,138: | Line 2,138: | ||
println("\n") |
println("\n") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,177: | Line 2,177: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Comparing bubble and shell sort: |
Comparing bubble and shell sort: |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[BubbleSort,ShellSort] |
||
BubbleSort[in_List]:=Module[{x=in,l=Length[in],swapped},swapped=True; |
BubbleSort[in_List]:=Module[{x=in,l=Length[in],swapped},swapped=True; |
||
While[swapped,swapped=False; |
While[swapped,swapped=False; |
||
Line 2,204: | Line 2,204: | ||
ListLogLogPlot[Transpose@times[[All,1]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Ones"] |
ListLogLogPlot[Transpose@times[[All,1]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Ones"] |
||
ListLogLogPlot[Transpose@times[[All,2]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Ascending integers"] |
ListLogLogPlot[Transpose@times[[All,2]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Ascending integers"] |
||
ListLogLogPlot[Transpose@times[[All,3]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Shuffled"]</ |
ListLogLogPlot[Transpose@times[[All,3]],PlotLegends->{"Bubble","Shell"},PlotLabel->"Shuffled"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Outputs three graphs on a log-log scales showing the size of the array and the time taken, for each of the three different arrays. |
Outputs three graphs on a log-log scales showing the size of the array and the time taken, for each of the three different arrays. |
||
Line 2,215: | Line 2,215: | ||
We have also added the array as first parameter of the internal function “sorter” as Nim compiler doesn’t allow direct access to this mutable array in function “quicksort” (memory safety violation). |
We have also added the array as first parameter of the internal function “sorter” as Nim compiler doesn’t allow direct access to this mutable array in function “quicksort” (memory safety violation). |
||
< |
<syntaxhighlight lang="nim">import algorithm |
||
import random |
import random |
||
import sequtils |
import sequtils |
||
Line 2,367: | Line 2,367: | ||
stdout.write &"{time:8d} " |
stdout.write &"{time:8d} " |
||
echo "" |
echo "" |
||
echo '\n'</ |
echo '\n'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,408: | Line 2,408: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Compare_sorting_algorithms.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Compare_sorting_algorithms.exw</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">XQS</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">01</span> <span style="color: #000080;font-style:italic;">-- (set to 1 to exclude quick_sort and shell_sort from ones)</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">XQS</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">01</span> <span style="color: #000080;font-style:italic;">-- (set to 1 to exclude quick_sort and shell_sort from ones)</span> |
||
Line 2,765: | Line 2,765: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
===Conclusions=== |
===Conclusions=== |
||
I knew bubblesort and insertion sort would be bad, but not so bad that you cannot meaningfully plot them against better sorts. |
I knew bubblesort and insertion sort would be bad, but not so bad that you cannot meaningfully plot them against better sorts. |
||
Line 2,782: | Line 2,782: | ||
{{works with|Python|2.5}} |
{{works with|Python|2.5}} |
||
===Examples of sorting routines=== |
===Examples of sorting routines=== |
||
< |
<syntaxhighlight lang="python">def builtinsort(x): |
||
x.sort() |
x.sort() |
||
Line 2,800: | Line 2,800: | ||
if size < 2: return seq |
if size < 2: return seq |
||
low, middle, up = partition(seq, random.choice(seq)) |
low, middle, up = partition(seq, random.choice(seq)) |
||
return qsortranpart(low) + middle + qsortranpart(up)</ |
return qsortranpart(low) + middle + qsortranpart(up)</syntaxhighlight> |
||
===Sequence generators=== |
===Sequence generators=== |
||
< |
<syntaxhighlight lang="python">def ones(n): |
||
return [1]*n |
return [1]*n |
||
Line 2,813: | Line 2,813: | ||
x = range(n) |
x = range(n) |
||
random.shuffle(x) |
random.shuffle(x) |
||
return x</ |
return x</syntaxhighlight> |
||
===Write timings=== |
===Write timings=== |
||
< |
<syntaxhighlight lang="python">def write_timings(npoints=10, maxN=10**4, sort_functions=(builtinsort,insertion_sort, qsort), |
||
sequence_creators = (ones, range, shuffledrange)): |
sequence_creators = (ones, range, shuffledrange)): |
||
Ns = range(2, maxN, maxN//npoints) |
Ns = range(2, maxN, maxN//npoints) |
||
Line 2,821: | Line 2,821: | ||
for make_seq in sequence_creators: |
for make_seq in sequence_creators: |
||
Ts = [usec(sort, (make_seq(n),)) for n in Ns] |
Ts = [usec(sort, (make_seq(n),)) for n in Ns] |
||
writedat('%s-%s-%d-%d.xy' % (sort.__name__, make_seq.__name__, len(Ns), max(Ns)), Ns, Ts)</ |
writedat('%s-%s-%d-%d.xy' % (sort.__name__, make_seq.__name__, len(Ns), max(Ns)), Ns, Ts)</syntaxhighlight> |
||
Where ''writedat()'' is defined in the [[Write float arrays to a text file]], ''usec()'' - [[Query Performance]], ''insertion_sort()'' - [[Insertion sort]], ''qsort'' - [[Quicksort]] subtasks, correspondingly. |
Where ''writedat()'' is defined in the [[Write float arrays to a text file]], ''usec()'' - [[Query Performance]], ''insertion_sort()'' - [[Insertion sort]], ''qsort'' - [[Quicksort]] subtasks, correspondingly. |
||
Line 2,827: | Line 2,827: | ||
{{libheader|matplotlib}} |
{{libheader|matplotlib}} |
||
{{libheader|NumPy}} |
{{libheader|NumPy}} |
||
< |
<syntaxhighlight lang="python">import operator |
||
import numpy, pylab |
import numpy, pylab |
||
def plotdd(dictplotdict): |
def plotdd(dictplotdict): |
||
Line 2,849: | Line 2,849: | ||
pylab.savefig(figname+'.png') |
pylab.savefig(figname+'.png') |
||
pylab.savefig(figname+'.pdf') |
pylab.savefig(figname+'.pdf') |
||
print figname</ |
print figname</syntaxhighlight> |
||
See [[Plot x, y arrays]] and [[Polynomial Fitting]] subtasks for a basic usage of ''pylab.plot()'' and ''numpy.polyfit()''. |
See [[Plot x, y arrays]] and [[Polynomial Fitting]] subtasks for a basic usage of ''pylab.plot()'' and ''numpy.polyfit()''. |
||
< |
<syntaxhighlight lang="python">import collections, itertools, glob, re |
||
import numpy |
import numpy |
||
def plot_timings(): |
def plot_timings(): |
||
Line 2,881: | Line 2,881: | ||
# actual plotting |
# actual plotting |
||
plotdd(df) |
plotdd(df) |
||
plotdd(ds) # see ``plotdd()`` above</ |
plotdd(ds) # see ``plotdd()`` above</syntaxhighlight> |
||
===Figures: log2( time in microseconds ) vs. log2( sequence length )=== |
===Figures: log2( time in microseconds ) vs. log2( sequence length )=== |
||
Line 2,887: | Line 2,887: | ||
[[File:Range.png|300px|thumb|right|log(Time) vs. log(N): Relative performance on range(N) as an input]] |
[[File:Range.png|300px|thumb|right|log(Time) vs. log(N): Relative performance on range(N) as an input]] |
||
[[File:Shuffledrange.png|300px|thumb|right|log(Time) vs. log(N): Relative performance on random permutation of range(N) as an input]] |
[[File:Shuffledrange.png|300px|thumb|right|log(Time) vs. log(N): Relative performance on random permutation of range(N) as an input]] |
||
< |
<syntaxhighlight lang="python">sort_functions = [ |
||
builtinsort, # see implementation above |
builtinsort, # see implementation above |
||
insertion_sort, # see [[Insertion sort]] |
insertion_sort, # see [[Insertion sort]] |
||
Line 2,904: | Line 2,904: | ||
sort_functions=sort_functions, |
sort_functions=sort_functions, |
||
sequence_creators = (ones, range, shuffledrange)) |
sequence_creators = (ones, range, shuffledrange)) |
||
plot_timings()</ |
plot_timings()</syntaxhighlight> |
||
Executing above script we get belowed figures. |
Executing above script we get belowed figures. |
||
====ones==== |
====ones==== |
||
Line 2,935: | Line 2,935: | ||
The number of ranges can be increased at the expense of a wider display of output. |
The number of ranges can be increased at the expense of a wider display of output. |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm compares various sorts for 3 types of input sequences: ones/ascending/random.*/ |
||
parse arg ranges start# seed . /*obtain optional arguments from the CL*/ |
parse arg ranges start# seed . /*obtain optional arguments from the CL*/ |
||
if ranges=='' | ranges=="," then ranges= 5 /*Not Specified? Then use the default.*/ |
if ranges=='' | ranges=="," then ranges= 5 /*Not Specified? Then use the default.*/ |
||
Line 3,206: | Line 3,206: | ||
if i==2 then i= 1 |
if i==2 then i= 1 |
||
else i= i * 5 % 11 |
else i= i * 5 % 11 |
||
end /*while i¬==0*/; return</ |
end /*while i¬==0*/; return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
Line 3,232: | Line 3,232: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class Array |
||
def radix_sort(base=10) # negative value is inapplicable. |
def radix_sort(base=10) # negative value is inapplicable. |
||
ary = dup |
ary = dup |
||
Line 3,324: | Line 3,324: | ||
puts |
puts |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Array#sort is a built-in method. |
Array#sort is a built-in method. |
||
Line 3,376: | Line 3,376: | ||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
{{tcllib|struct::list}} |
{{tcllib|struct::list}} |
||
< |
<syntaxhighlight lang="tcl">############################################################################### |
||
# measure and plot times |
# measure and plot times |
||
package require Tk |
package require Tk |
||
Line 3,597: | Line 3,597: | ||
create_log10_plot "Sorting a '$type' list" size time $sizes $times $algorithms $shapes $colours |
create_log10_plot "Sorting a '$type' list" size time $sizes $times $algorithms $shapes $colours |
||
} |
} |
||
puts "\ntimes in microseconds, average of $runs runs"</ |
puts "\ntimes in microseconds, average of $runs runs"</syntaxhighlight> |
||
{{omit from|GUISS}} |
{{omit from|GUISS}} |
||
Line 3,646: | Line 3,646: | ||
Results presented in tabular form as Wren doesn't have a plotting library available at the present time. |
Results presented in tabular form as Wren doesn't have a plotting library available at the present time. |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/sort" for Sort |
import "/sort" for Sort |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 3,768: | Line 3,768: | ||
} |
} |
||
System.print("\n") |
System.print("\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |