Advertisement
Guest User

StupidSort.m

a guest
Oct 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.40 KB | None | 0 0
  1. clc;
  2. close all;
  3.  
  4. timer0=(now-datenum(1970,1,1))*86400;
  5.  
  6. rng('shuffle');
  7.  
  8. %% Data generation
  9.  
  10. lower = -10000;
  11. upper =  10000;
  12. dataSize = 100000;
  13.  
  14. x = 1:dataSize;
  15.  
  16. data=zeros(1,dataSize);
  17. for i=1:dataSize
  18.     data(i)=lower+((upper-lower)*rand());
  19.         if mod(i,10000)==0
  20.         fprintf('Generating random numbers\n');
  21.         end
  22. end
  23.  
  24. fprintf('\nData set generated!\n\n');
  25.  
  26. dataUnsorted = data;
  27. timer1=(now-datenum(1970,1,1))*86400;
  28. timer2=(now-datenum(1970,1,1))*86400;
  29. %% Sorting
  30. for i=1:1:dataSize
  31.     if mod(i,round(dataSize/20))==0
  32.         fprintf('Sorting... Elapsed time: %.3f\n',(now-datenum(1970,1,1))*86400-timer1);
  33.         timer1=(now-datenum(1970,1,1))*86400;
  34.     end
  35.     max=data(i);
  36.     j=i;
  37.     for j = i:dataSize
  38.         if (data(j)>max)
  39.             max=data(j);
  40.             index=j;
  41.         end
  42.     end
  43.     tmp=data(i);
  44.     data(index)=tmp;
  45.     data(i)=max;
  46. end
  47. %%
  48. figure;
  49.  
  50.     subplot(2,1,1);
  51.     bar(x,dataUnsorted,'r');
  52.     xlim([1 dataSize]);
  53.     ylim([lower upper]);
  54.     title('Unsorted values');
  55.     grid on;
  56.    
  57.     subplot(2,1,2);
  58.     bar(x,data,'g');
  59.     xlim([1 dataSize]);
  60.     ylim([lower upper]);
  61.     title('Sorted values');
  62.     grid on;
  63.    
  64. %%
  65. fprintf('\nSorting complete!\n');
  66. fprintf('Total sorting time: %.4f\n',(now-datenum(1970,1,1))*86400-timer2);
  67. fprintf('Total run time    : %.4f\n',(now-datenum(1970,1,1))*86400-timer0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement