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

程序结果:

相关推荐
Zane19942 小时前
暴力字符串匹配失配就要把文本指针往回拉,KMP凭什么能让它一直往前走
算法
Tim_103 小时前
【LeetCode】338、比特位计数
c++·算法·leetcode
木子算法3 小时前
不是重心也不是中点:费马—韦伯点、它的最优性条件与迭代求解
人工智能·算法·目标跟踪
tryxr4 小时前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_34 小时前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Selvaggia4 小时前
DMD(Distribution Matching Distillation,分布匹配蒸馏)
算法
Navigator_Z4 小时前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.4 小时前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.5 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
wzdark5 小时前
数据压缩算法的时间复杂度与压缩率权衡4
算法