当前位置:网站首页>冒泡排序
冒泡排序
2022-06-30 02:13:00 【生活需要深度】
概要
本章介绍排序算法中的冒泡排序,重点讲解冒泡排序的思想。
目录
1. 冒泡排序介绍
2. 冒泡排序图文说明
3. 冒泡排序的时间复杂度和稳定性
4. 冒泡排序实现
4.1冒泡排序C实现
4.2冒泡排序C++实现
4.3冒泡排序Java实现
转载请注明出处:冒泡排序 - 如果天空不死 - 博客园
更多内容:数据结构与算法系列 目录
冒泡排序介绍
冒泡排序(Bubble Sort),又被称为气泡排序或泡沫排序。
它是一种较简单的排序算法。它会遍历若干次要排序的数列,每次遍历时,它都会从前往后依次的比较相邻两个数的大小;如果前者比后者大,则交换它们的位置。这样,一次遍历之后,最大的元素就在数列的末尾! 采用相同的方法再次遍历时,第二大的元素就被排列在最大元素之前。重复此操作,直到整个数列都有序为止!
冒泡排序图文说明
冒泡排序C实现一
void bubble_sort1(int a[], int n)
{
int i,j;
for (i=n-1; i>0; i--)
{
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
swap(a[j], a[j+1]);
}
}
}下面以数列{20,40,30,10,60,50}为例,演示它的冒泡排序过程(如下图)。

我们先分析第1趟排序
当i=5,j=0时,a[0]<a[1]。此时,不做任何处理!
当i=5,j=1时,a[1]>a[2]。此时,交换a[1]和a[2]的值;交换之后,a[1]=30,a[2]=40。
当i=5,j=2时,a[2]>a[3]。此时,交换a[2]和a[3]的值;交换之后,a[2]=10,a[3]=40。
当i=5,j=3时,a[3]<a[4]。此时,不做任何处理!
当i=5,j=4时,a[4]>a[5]。此时,交换a[4]和a[5]的值;交换之后,a[4]=50,a[3]=60。
于是,第1趟排序完之后,数列{20,40,30,10,60,50}变成了{20,30,10,40,50,60}。此时,数列末尾的值最大。
根据这种方法:
第2趟排序完之后,数列中a[5...6]是有序的。
第3趟排序完之后,数列中a[4...6]是有序的。
第4趟排序完之后,数列中a[3...6]是有序的。
第5趟排序完之后,数列中a[1...6]是有序的。
第5趟排序之后,整个数列也就是有序的了。
冒泡排序C实现二
观察上面冒泡排序的流程图,第3趟排序之后,数据已经是有序的了;第4趟和第5趟并没有进行数据交换。
下面我们对冒泡排序进行优化,使它效率更高一些:添加一个标记,如果一趟遍历中发生了交换,则标记为true,否则为false。如果某一趟没有发生交换,说明排序已经完成!

void bubble_sort2(int a[], int n)
{
int i,j;
int flag; // 标记
for (i=n-1; i>0; i--)
{
flag = 0; // 初始化标记为0
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
{
swap(a[j], a[j+1]);
flag = 1; // 若发生交换,则设标记为1
}
}
if (flag==0)
break; // 若没发生交换,则说明数列已有序。
}
}
冒泡排序的时间复杂度和稳定性
冒泡排序时间复杂度
冒泡排序的时间复杂度是O(N2)。
假设被排序的数列中有N个数。遍历一趟的时间复杂度是O(N),需要遍历多少次呢?N-1次!因此,冒泡排序的时间复杂度是O(N2)。
冒泡排序稳定性
冒泡排序是稳定的算法,它满足稳定算法的定义。
算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i]在a[j]前面;并且排序之后,a[i]仍然在a[j]前面。则这个排序算法是稳定的!
冒泡排序实现
/**
* 冒泡排序:C 语言
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// 数组长度
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
// 交互数值
#define swap(a,b) (a^=b,b^=a,a^=b)
/*
* 冒泡排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void bubble_sort1(int a[], int n)
{
int i,j;
for (i=n-1; i>0; i--)
{
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
swap(a[j], a[j+1]);
}
}
}
/*
* 冒泡排序(改进版)
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void bubble_sort2(int a[], int n)
{
int i,j;
int flag; // 标记
for (i=n-1; i>0; i--)
{
flag = 0; // 初始化标记为0
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
{
swap(a[j], a[j+1]);
flag = 1; // 若发生交换,则设标记为1
}
}
if (flag==0)
break; // 若没发生交换,则说明数列已有序。
}
}
void main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = LENGTH(a);
printf("before sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
bubble_sort1(a, ilen);
//bubble_sort2(a, ilen);
printf("after sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
}冒泡排序C++实现
实现代码(BubbleSort.cpp)
/**
* 冒泡排序:C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* 冒泡排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void bubbleSort1(int* a, int n)
{
int i,j,tmp;
for (i=n-1; i>0; i--)
{
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
{
// 交换a[j]和a[j+1]
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
/*
* 冒泡排序(改进版)
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
void bubbleSort2(int* a, int n)
{
int i,j,tmp;
int flag; // 标记
for (i=n-1; i>0; i--)
{
flag = 0; // 初始化标记为0
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++)
{
if (a[j] > a[j+1])
{
// 交换a[j]和a[j+1]
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
flag = 1; // 若发生交换,则设标记为1
}
}
if (flag==0)
break; // 若没发生交换,则说明数列已有序。
}
}
int main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = (sizeof(a)) / (sizeof(a[0]));
cout << "before sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
bubbleSort1(a, ilen);
//bubbleSort2(a, ilen);
cout << "after sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}冒泡排序Java实现
实现代码(BubbleSort.java)
/**
* 冒泡排序:Java
*
* @author skywang
* @date 2014/03/11
*/
public class BubbleSort {
/*
* 冒泡排序
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
public static void bubbleSort1(int[] a, int n) {
int i,j;
for (i=n-1; i>0; i--) {
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++) {
if (a[j] > a[j+1]) {
// 交换a[j]和a[j+1]
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
/*
* 冒泡排序(改进版)
*
* 参数说明:
* a -- 待排序的数组
* n -- 数组的长度
*/
public static void bubbleSort2(int[] a, int n) {
int i,j;
int flag; // 标记
for (i=n-1; i>0; i--) {
flag = 0; // 初始化标记为0
// 将a[0...i]中最大的数据放在末尾
for (j=0; j<i; j++) {
if (a[j] > a[j+1]) {
// 交换a[j]和a[j+1]
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
flag = 1; // 若发生交换,则设标记为1
}
}
if (flag==0)
break; // 若没发生交换,则说明数列已有序。
}
}
public static void main(String[] args) {
int i;
int[] a = {20,40,30,10,60,50};
System.out.printf("before sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
bubbleSort1(a, a.length);
//bubbleSort2(a, a.length);
System.out.printf("after sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
}
}上面3种实现的原理和输出结果都是一样的。下面是它们的输出结果:
before sort:20 40 30 10 60 50 after sort:10 20 30 40 50 60
边栏推荐
- Simple distinction between break and continue
- DHU programming exercise
- Internet Crime Complaint Center reports an increase in DDoS Attacks
- Want to change careers, but don't know what you want to do?
- js Array. Five convenient applications of from()
- Vs realize quick replacement function
- Knowledge payment cannot escape the essence of "anxiety"
- The birth of the cheapswap protocol
- Let‘sPlayCurling
- As VoIP became the target, DDoS attacks surged by 35% in the third quarter
猜你喜欢

012_ switch

DMX configuration

What problems can cloud storage architecture solve for Devops?

CTF入门学习(Web方向)

Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)

Unity2d-- add keys to animation and bind events

ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display

Share the source code of the website of graduation student record

013_ slider

Understand AQS principle (flow chart and synchronous queue diagram)
随机推荐
什么是幂等性?四种接口幂等性方案详解!
假離婚變成真離婚,財產怎麼辦
Knowledge payment cannot escape the essence of "anxiety"
ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display
004_ icon
JS reverse case -rus5 logic learning
Upload, use of Avatar
Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
7 — filter
If you want to install a set of monitoring, what is the process? How much is it?
DDoS "fire drill" service urges companies to prepare
If mybaits cannot query the data, it can query how to change it in the database
【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
搞透AQS原理(流程图及同步队列图解)
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
Matlab 2012a drawing line segment with arrow
Simple distinction between break and continue
Geotools wkt coordinate system conversion
Scala基础【入门及安装】
DHU programming exercise