Advertisement
zielo

Sortowanie bąbelkowe

Apr 20th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 0.79 KB | None | 0 0
  1. N = 80;
  2. rand_tab = rand(1,N)*20-10; #zapełniam wektor rand_tab warościami losowymi w przedziale [-10,10]
  3. #rand_tab = sort(rand_tab);
  4. x = 1:N; #zakres x na wykresie
  5. #plot(x,rand_tab(x),"."); #wykres
  6. rand_tab_d = rand_tab(rand_tab>=0); #tworzę nowy wektor z warości nieujemnych
  7. rand_tab_d1 = rand_tab_d; #tworzę kopię tablicy
  8. #sortowanie bąbelkowe
  9. n = length(rand_tab_d);
  10. x = 1:n;
  11.  #    plot(x,rand_tab_d(x),"b.");
  12. for i=1:(n-1)
  13.   for j=1:n-i
  14.     if (rand_tab_d(j) > rand_tab_d(j+1))
  15.       z = rand_tab_d(j);
  16.       rand_tab_d(j) = rand_tab_d(j+1);
  17.       rand_tab_d(j+1) = z;
  18.  #     drawnow;
  19.          hold on;
  20.          cla;
  21.           plot(x,rand_tab_d(x),"bo");
  22.         pause(0.1);
  23. #      hold off;
  24.     endif
  25.  
  26.   endfor
  27. endfor
  28. #x = 1:n;
  29. #plot(x,rand_tab_d(x),"b."); #wykres
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement