MATLAB冒泡法排序程序

冒泡法是经典排序算法, 顾名思义,就是把最小的数字像气泡一样往上冒,最终实现排序.

本程序为降序排序,也就是把最大值往上冒, MATLAB实现如下:

clc;close all;clear all;warning off;%清除变量

rand('seed', 100);

randn('seed', 100);

format long g;

disp('随机产生一个待排序数列');

timemat201=randi([3,20],10,1)

n2=size(timemat201,1);

tic;

state201=0;

while state201==0% 冒泡法

state201=1;

for i201=2:n2

t201=timemat201(i201-1,:);

t202=timemat201(i201,:);

if t201<t202

% 交换

timemat201(i201-1,:)=t202;

timemat201(i201,:)=t201;

state201=0;

end

end

end

toc

disp('排序后');

timemat201

程序结果:

相关推荐
I_LPL21 小时前
hot100贪心专题
数据结构·算法·leetcode·贪心
颜酱21 小时前
DFS 岛屿系列题全解析
javascript·后端·算法
WolfGang00732121 小时前
代码随想录算法训练营 Day16 | 二叉树 part06
算法
2401_831824961 天前
代码性能剖析工具
开发语言·c++·算法
Sunshine for you1 天前
C++中的职责链模式实战
开发语言·c++·算法
qq_416018721 天前
C++中的状态模式
开发语言·c++·算法
2401_884563241 天前
模板代码生成工具
开发语言·c++·算法
2401_831920741 天前
C++代码国际化支持
开发语言·c++·算法
m0_672703311 天前
上机练习第51天
数据结构·c++·算法
ArturiaZ1 天前
【day60】
算法·深度优先·图论