Compare sorting algorithms' performance: Difference between revisions

Added Erlang
(Added Erlang)
Line 955:
Built-in Sort: 218268</pre>
(With 10,000 elements in each array. A naive HeapSort seems faster than the built-in sort in all three cases.)
 
=={{header|Erlang}}==
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].
Quicksort handles shuffled numbers best. Bubble sort does ones and ranges best.
<lang Erlang>
-module( compare_sorting_algorithms_performance ).
 
-export( [task/0] ).
 
task() ->
Ns = [100, 1000, 10000],
Lists = [{"ones", fun list_of_ones/1, Ns}, {"ranges", fun list_of_ranges/1, Ns}, {"shuffleds", fun list_of_shuffleds/1, Ns}],
Sorts = [{bubble_sort, fun bubble_sort:list/1}, {insertion_sort, fun sort:insertion/1}, {iquick_sort, fun quicksort:qsort/1}],
Results = [time_list(X, Sorts) || X <- Lists],
[file:write_file(X++".png", egd_chart:graph(Y, [{x_label, "log N"}, {y_label, "log ms"}])) || {X, Y} <- Results].
 
 
list_of_ones( N ) -> [1 || _X <- lists:seq(1, N)].
list_of_ranges( N ) -> [X || X <- lists:seq(1, N)].
list_of_shuffleds( N ) -> [random:uniform(N) || _X <- lists:seq(1, N)].
 
time_list( {List, List_fun, Values}, Sorts ) ->
Results = [{Sort, time_sort(Sort_fun, List_fun, Values)} || {Sort, Sort_fun} <- Sorts],
{List, Results}.
 
time_sort( Sort_fun, List_fun, Values ) ->
[time(Sort_fun, List_fun, X) || X <- Values].
 
time( Fun, List_fun, N ) ->
{Time, _Result} = timer:tc( fun() -> Fun( List_fun(N) ) end ),
{math:log10(N), math:log10(Time)}.
</lang>
 
=={{header|J}}==
Anonymous user