點(diǎn)擊上方藍(lán)字和“好玩的MATLAB”一起快樂(lè)玩耍吧!
sspx2p5ljlj6403005010.jpg (236.67 KB, 下載次數(shù): 2)
下載附件
保存到相冊(cè)
sspx2p5ljlj6403005010.jpg
2024-9-3 17:21 上傳
好玩的matlab
帶你解鎖不一樣的matlab新玩法
無(wú)聊寫了個(gè)冒泡、選擇排序的可視化,其實(shí)我想把常見的排序算法可視化都寫一遍,但是發(fā)現(xiàn)有些排序的可視化matlab 并不容易制作,所以我就舉例冒泡和選擇排序的可視化過(guò)程,喜歡此推文的小伙伴們記得點(diǎn)贊+關(guān)注+分享!【尊重作者勞動(dòng)成果,轉(zhuǎn)載請(qǐng)注明推文鏈接和公眾號(hào)名】
冒泡排序clc;clear;close all;%--------------------------------------------------------------------------% @Author: 好玩的Matlab && 豬豬% @公眾號(hào):好玩的Matlab% @Created: 09,18,2023% @Email: 2377389590@qq.com% 尊重勞動(dòng)成果,轉(zhuǎn)載請(qǐng)備注推文鏈接和公眾號(hào)名,嚴(yán)禁商用。%--------------------------------------------------------------------------disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))array = randperm(15); % 生成一個(gè)1至15的隨機(jī)排列數(shù)組bubbleSort(array);
function bubbleSort(array) idxSort = sort(array, 'ascend'); % 對(duì)數(shù)組進(jìn)行升序排序 order = zeros(1, length(array)); % 獲取每個(gè)元素在排序后的位置 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; % 初始化交換標(biāo)志 for j = 1:length(array) - i % 如果需要交換,則執(zhí)行交換并更新顏色數(shù)組 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; % 清除當(dāng)前圖形 % 更新條形圖數(shù)據(jù) b = bar(array); b.EdgeColor = [1, 1, 1] * 0.8; b.FaceColor = 'flat'; b.CData = colors; % 標(biāo)記條形圖的值 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 % 如果在一輪排序中沒(méi)有交換,則結(jié)束排序 if ~swapped break; end end title('冒泡排序結(jié)果'); delete(p); % 刪除紅色標(biāo)線end
function [A, B] = swap(a, b) % 交換函數(shù) A = b; B = a;end
function setDefaultAxes % 設(shè)置默認(rèn)的軸屬性 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
4a0mp3tbxrq6403005111.gif (4.08 MB, 下載次數(shù): 0)
下載附件
保存到相冊(cè)
4a0mp3tbxrq6403005111.gif
2024-9-3 17:21 上傳
選擇排序clc;clear;close all;%--------------------------------------------------------------------------% @Author: 好玩的Matlab && 豬豬% @公眾號(hào):好玩的Matlab% @Created: 09,18,2023% @Email: 2377389590@qq.com% 尊重勞動(dòng)成果,轉(zhuǎn)載請(qǐng)備注推文鏈接和公眾號(hào)名,嚴(yán)禁商用。%--------------------------------------------------------------------------disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))array = randperm(15); % 生成一個(gè)1至15的隨機(jī)排列數(shù)組selectionSort(array);
function selectionSort(array) idxSort = sort(array, 'ascend'); % 對(duì)數(shù)組進(jìn)行升序排序 order = zeros(1, length(array)); % 獲取每個(gè)元素在排序后的位置 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; % 初始化最小值索引為當(dāng)前位置 for j = i:length(array) clf; % 清除當(dāng)前圖形 % 更新條形圖數(shù)據(jù) b = bar(array); b.EdgeColor = [1, 1, 1] * 0.8; b.FaceColor = 'flat'; b.CData = colors; % 標(biāo)記條形圖的值 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 % 如果找到的最小值不在當(dāng)前位置,進(jìn)行交換并更新顏色 if minIndex ~= i [array(i), array(minIndex)] = swap(array(i), array(minIndex)); [colors(i, :), colors(minIndex, :)] = swap(colors(i, :), colors(minIndex, :)); end end title('選擇排序結(jié)果'); delete(p); % 刪除紅色標(biāo)線endfunction [A, B] = swap(a, b) % 交換函數(shù) A = b; B = a;endfunction setDefaultAxes % 設(shè)置默認(rèn)的軸屬性 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
iiob4oy4bm16403005212.gif (4.89 MB, 下載次數(shù): 1)
下載附件
保存到相冊(cè)
iiob4oy4bm16403005212.gif
2024-9-3 17:21 上傳
- -THE END- -
jbopswhifrg6403005312.gif (10.71 KB, 下載次數(shù): 1)
下載附件
保存到相冊(cè)
jbopswhifrg6403005312.gif
2024-9-3 17:21 上傳
|