How to plot average runtimes for sorting algorithms

40 visualizaciones (últimos 30 días)
Harrison
Harrison el 9 de Nov. de 2024 a las 4:30
Comentada: Walter Roberson el 10 de Nov. de 2024 a las 20:01
For an assignment I was asked to run insertion sort, selection sort, and merge sort on random lists of numbers of various sizes ten times each, and then plot the average run times for each on an x,y plot. With what I have currently, I am just trying to get Matlab to create a random list, sort it, and record the runtimes in a table for one list length and one sorting algorithm.
for n = 1:10
random_list = randi([0 1000], 1, 5);
tic;
sorted = insertionsort(random_list);
a = toc;
runtimes = zeroes(1, a);
end
I understand this is rough, I'm very inexperienced with this.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2024 a las 4:56
fcns = {@insertsort, @selsort, @mrgsort};
for INDEX1 = %...
for INDEX2 = %...
tic;
sorted = fcns{INDEX1}(random_list);
a = toc;
runtimes(INDEX1,INDEX2) = a;
%...
plot(times)
  2 comentarios
Harrison
Harrison el 10 de Nov. de 2024 a las 18:45
I think this is on the right track, could you specify what to use for INDEX1 and INDEX2?
Walter Roberson
Walter Roberson el 10 de Nov. de 2024 a las 20:01
INDEX1 is something that is indexing the list of functions.
INDEX2 is something that is indexing number of times each function is called.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by