|
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!
31jvvq1miix6408504746.jpg
好玩的matlab
带你解锁不一样的matlab新玩法
无聊写了个冒泡、选择排序的可视化,其实我想把常见的排序算法可视化都写一遍,但是发现有些排序的可视化matlab 并不容易制作,所以我就举例冒泡和选择排序的可视化过程,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】
冒泡排序clc;clear;close all;%--------------------------------------------------------------------------% @Author: 好玩的Matlab && 猪猪% @公众号:好玩的Matlab% @Created: 09,18,2023% @Email: 2377389590@qq.com% 尊重劳动成果,转载请备注推文链接和公众号名,严禁商用。%--------------------------------------------------------------------------disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))array = randperm(15); % 生成一个1至15的随机排列数组bubbleSort(array);
function bubbleSort(array) idxSort = sort(array, 'ascend'); % 对数组进行升序排序 order = zeros(1, length(array)); % 获取每个元素在排序后的位置 for i = 1:length(array) order(i) = find(idxSort == array(i), 1); end colors = jet(length(array)); colors = colors(order, :); % 更新颜色的顺序 figure; % 开始冒泡排序 for i = 1:length(array) swapped = false; % 初始化交换标志 for j = 1:length(array) - i % 如果需要交换,则执行交换并更新颜色数组 if array(j) > array(j + 1) [array(j), array(j+1)] = swap(array(j), array(j+1)); swapped = true; [colors(j, :), colors(j+1, :)] = swap(colors(j, :), colors(j+1, :)); end clf; % 清除当前图形 % 更新条形图数据 b = bar(array); b.EdgeColor = [1, 1, 1] * 0.8; b.FaceColor = 'flat'; b.CData = colors; % 标记条形图的值 xTips = b.XEndPoints; yTips = b.YEndPoints; xticklabels(num2cell(round(yTips, 2))); for k = 1:length(array) text(xTips(k), yTips(k), num2str(array(k), '%.2f'), ... 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', ... 'FontSize', 12, 'FontName', 'Times New Roman'); end title('冒泡排序'); ylim([0, max(array) * 1.04]); hold on; p = plot([j, j + 1], [array(j), array(j + 1)], 'r', 'LineWidth', 2); setDefaultAxes hold off pause(0.2); % 可视化延迟 % 保存为GIF格式 frame = getframe(gcf); im = frame2im(frame); [imind,cm] = rgb2ind(im,256); if i == 1 && j == 1 imwrite(imind,cm,'bubblesort.gif','gif', 'Loopcount', inf); else imwrite(imind,cm,'bubblesort.gif','gif','WriteMode','append'); end end % 如果在一轮排序中没有交换,则结束排序 if ~swapped break; end end title('冒泡排序结果'); delete(p); % 删除红色标线end
function [A, B] = swap(a, b) % 交换函数 A = b; B = a;end
function setDefaultAxes % 设置默认的轴属性 ax = gca; ax.Box = 'off'; ax.XGrid = 'on'; ax.YGrid = 'on'; ax.GridLineStyle = '-.'; ax.GridColor = 'k'; ax.GridAlpha = 0.2; ax.XMinorGrid = 'off'; ax.YMinorGrid = 'off'; ax.MinorGridLineStyle = '-'; ax.MinorGridColor = [0 0 0]; ax.MinorGridAlpha = 0.1; ax.LineWidth = 1; ax.XMinorTick = 'off'; ax.YMinorTick = 'off'; ax.TickDir = 'in'; ax.FontSize = 12; ax.FontName = 'Times New Roman';end
4ifcm00ddnr6408504847.gif
选择排序clc;clear;close all;%--------------------------------------------------------------------------% @Author: 好玩的Matlab && 猪猪% @公众号:好玩的Matlab% @Created: 09,18,2023% @Email: 2377389590@qq.com% 尊重劳动成果,转载请备注推文链接和公众号名,严禁商用。%--------------------------------------------------------------------------disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))array = randperm(15); % 生成一个1至15的随机排列数组selectionSort(array);
function selectionSort(array) idxSort = sort(array, 'ascend'); % 对数组进行升序排序 order = zeros(1, length(array)); % 获取每个元素在排序后的位置 for i = 1:length(array) order(i) = find(idxSort == array(i), 1); end colors = jet(length(array)); colors = colors(order, :); % 更新颜色的顺序 figure; % 开始选择排序 for i = 1:length(array) minIndex = i; % 初始化最小值索引为当前位置 for j = i:length(array) clf; % 清除当前图形 % 更新条形图数据 b = bar(array); b.EdgeColor = [1, 1, 1] * 0.8; b.FaceColor = 'flat'; b.CData = colors; % 标记条形图的值 xTips = b.XEndPoints; yTips = b.YEndPoints; xticklabels(num2cell(round(yTips, 2))); for k = 1:length(array) text(xTips(k), yTips(k), num2str(array(k), '%.2f'), ... 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', ... 'FontSize', 12, 'FontName', 'Times New Roman'); end title('比较的元素中'); ylim([0, max(array) * 1.04]); hold on; p = plot([i, j], [array(i), array(j)], '-r', 'LineWidth', 2); setDefaultAxes hold off pause(0.3); % 可视化延迟 % 保存为GIF格式 frame = getframe(gcf); im = frame2im(frame); [imind,cm] = rgb2ind(im,256); if i == 1 && j == i imwrite(imind,cm,'selectionsort.gif','gif', 'Loopcount', inf); else imwrite(imind,cm,'selectionsort.gif','gif','WriteMode','append'); end % 比较元素,找到最小值的索引 if array(j) minIndex = j; end end % 如果找到的最小值不在当前位置,进行交换并更新颜色 if minIndex ~= i [array(i), array(minIndex)] = swap(array(i), array(minIndex)); [colors(i, :), colors(minIndex, :)] = swap(colors(i, :), colors(minIndex, :)); end end title('选择排序结果'); delete(p); % 删除红色标线endfunction [A, B] = swap(a, b) % 交换函数 A = b; B = a;endfunction setDefaultAxes % 设置默认的轴属性 ax = gca; ax.Box = 'off'; ax.XGrid = 'on'; ax.YGrid = 'on'; ax.GridLineStyle = '-.'; ax.GridColor = 'k'; ax.GridAlpha = 0.2; ax.XMinorGrid = 'off'; ax.YMinorGrid = 'off'; ax.MinorGridLineStyle = '-'; ax.MinorGridColor = [0 0 0]; ax.MinorGridAlpha = 0.1; ax.LineWidth = 1; ax.XMinorTick = 'off'; ax.YMinorTick = 'off'; ax.TickDir = 'in'; ax.FontSize = 12; ax.FontName = 'Times New Roman';end
cxmutfldeu36408504948.gif
- -THE END- -
tntjcxk2a156408505048.gif
|
|