个性化阅读
专注于IT技术分析

冒泡排序算法实现全解

点击下载

本文概述

在冒泡排序中, 将数组的每个元素与其相邻元素进行比较。该算法以遍历方式处理列表。具有n个元素的列表需要n-1次传递才能进行排序。考虑由n个元素组成的数组A, 其元素将通过使用冒泡排序进行排序。该算法的过程如下。

  1. 在通道1中, 将A [0]与A [1]比较, 将A [1]与A [2]比较, 将A [2]与A [3]比较, 依此类推。在第1遍结束时, 列表的最大元素位于列表的最高索引处。
  2. 在通道2中, 将A [0]与A [1]比较, 将A [1]与A [2]比较, 依此类推。在第2遍结束时, 列表的第二大元素位于列表的第二高索引处。
  3. 在通道n-1中, 将A [0]与A [1]比较, 将A [1]与A [2]比较, 依此类推。在此通行证的结尾。列表的最小元素位于列表的第一个索引处。

算法

  • 步骤1:对于i = 0到N-1, 重复步骤2
  • 第2步:对J = i +1到N-I重复
  • 步骤3:如果A [J]> A [i]交换A [J]和A [i] [内部循环结束] [外部循环结束
  • 步骤4:退出

复杂

情境 复杂
Space O(1)
最坏情况下的运行时间 O(n2)
平均案件运行时间 O(n)
最佳案例运行时间 O(n2)

C程序

#include<stdio.h>
void main ()
{
	int i, j, temp; 
	int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; 
	for(i = 0; i<10; i++)
	{
		for(j = i+1; j<10; j++)
		{
			if(a[j] > a[i])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp; 
			} 
		} 
	} 
	printf("Printing Sorted Element List ...\n");
	for(i = 0; i<10; i++)
	{
		printf("%d\n", a[i]);
	}
}

输出:

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

C ++程序

#include<iostream>
using namespace std;
int main ()
{
	int i, j, temp; 
	int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; 
	for(i = 0; i<10; i++)
	{
		for(j = i+1; j<10; j++)
		{
			if(a[j] < a[i])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp; 
			} 
		} 
	} 
	cout <<"Printing Sorted Element List ...\n";
	for(i = 0; i<10; i++)
	{
		cout <<a[i]<<"\n";
	}
	return 0;
}

输出:

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

Java程序

public class BubbleSort {
	public static void main(String[] args) {
	int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
	for(int i=0;i<10;i++)
	{
		for (int j=0;j<10;j++)
		{
			if(a[i]<a[j])
			{
				int temp = a[i];
				a[i]=a[j];
				a[j] = temp; 
			}
		}
	}
	System.out.println("Printing Sorted List ...");
	for(int i=0;i<10;i++)
	{
		System.out.println(a[i]);
	}
}
}

输出:

Printing Sorted List . . . 
7
9
10
12
23
34
34
44
78 
101

C#程序

using System;
					
public class Program
{
	public static void Main()
	{
		int i, j, temp; 
	int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; 
	for(i = 0; i<10; i++)
	{
		for(j = i+1; j<10; j++)
		{
			if(a[j] > a[i])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp; 
			} 
		} 
	} 
	Console.WriteLine("Printing Sorted Element List ...\n");
	for(i = 0; i<10; i++)
	{
		Console.WriteLine(a[i]);
	}
	}
}

输出:

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

Python程序

a=[10, 9, 7, 101, 23, 44, 12, 78, 34, 23]
for i in range(0, len(a)):
    for j in range(i+1, len(a)):
        if a[j]<a[i]:
            temp = a[j]
            a[j]=a[i]
            a[i]=temp
print("Printing Sorted Element List...")
for i in a: 
    print(i)

输出:

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

休息计划

fn main()
{
    let mut temp;
 let mut a: [i32; 10] = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
	for i in 0..10
	{
		for j in (i+1)..10
		{
			if a[j] < a[i]
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp; 
			}
		} 
	} 
	println!("Printing Sorted Element List ...\n");
	for i in &a
	{
		println!("{} ", i);
	}
}

输出:

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101
4

JavaScript程序

<html>
<head>
<title> 
Bubble Sort 
</title> 
</head> 
<body>
<script> 
	var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
	for(i=0;i<10;i++)
	{
		for (j=0;j<10;j++)
		{
			if(a[i]<a[j])
			{
				 temp = a[i];
				a[i]=a[j];
				a[j] = temp; 
			}
		}
	}
	txt = "<br>";
	document.writeln("Printing Sorted Element List ..."+txt);
	for(i=0;i<10;i++)
	{
		document.writeln(a[i]);
		document.writeln(txt);
	}
	</script> 
	</body>
</html>

输出:

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

PHP程序

<html>
<head>
<title>Bubble Sort</title>
</head>
<body>
<?php
	$a = array(10, 9, 7, 101, 23, 44, 12, 78, 34, 23);
	for($i=0;$i<10;$i++)
	{
		for ($j=0;$j<10;$j++)
		{
			if($a[$i]<$a[$j])
			{
				 $temp = $a[$i];
				$a[$i]=$a[$j];
				$a[$j] = $temp; 
			}
		}
	}
	echo "Printing Sorted Element List ...\n";
	for($i=0;$i<10;$i++)
	{
		echo $a[$i];
		echo "\n";
	}
?>
</body>
</html>

输出:

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101
赞(0)
未经允许不得转载:srcmini » 冒泡排序算法实现全解

评论 抢沙发

评论前必须登录!