% Animation of bubble sort % Roger Jang, 980703 clear all close all data_n = 10; x = 1:data_n; y = rand(1, data_n); y = randperm(length(x)); %y = [5 2 4 6 1 3] ; r = 0.3; rect = [-r -r r r] + sqrt(-1)*[0 1 1 0]; patchH = []; for i = 1:data_n, xx = real(rect)+x(i); yy = imag(rect)*y(i); h = patch(xx, yy, 'c'); patchH = [patchH, h]; end axis([0 data_n+1 0 max(y)]); title('Animation of insertion sort'); set(gca, 'box', 'on'); set(patchH, 'erasemode', 'xor'); % switch two bars fprintf('%d ', y); fprintf('\n'); for i = 2:1:data_n, j = i-1; if (y(i)