数组排序——冒泡排序
2013-06-25 12:17
183 查看
冒泡排序
本质:较小的值会逐渐冒泡至数组的顶端,同时较大的数会下沉到低端。
算法如下:
1、比较相邻的两个元素,如果第一个比第二个大,则交换位置。
2、针对所有元素重复以上的步骤,除了最后一个。
3、重复以上步骤遍历数组,直到没有任何一个数字需要比较。
时间复杂度为O(n^2) 比较慢
稳定排序算法
程序如下
本质:较小的值会逐渐冒泡至数组的顶端,同时较大的数会下沉到低端。
算法如下:
1、比较相邻的两个元素,如果第一个比第二个大,则交换位置。
2、针对所有元素重复以上的步骤,除了最后一个。
3、重复以上步骤遍历数组,直到没有任何一个数字需要比较。
时间复杂度为O(n^2) 比较慢
稳定排序算法
程序如下
// Bubble sort import java.io.IOException; public class BubbleSort { public static void main( String args[] ) throws IOException { int a[ ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int temp; // print the original array System.out.println( "The original array: " ); for ( int i = 0; i < a.length; i++ ) System.out.print( " " + a[ i ] + " " ); // sort the array for ( int k = 0; k < a.length; k++) { for ( int j = 0; j < a.length - 1; j++ ) { if ( a[ j + 1 ] < a[ j ] ) { temp = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = temp; } } } // print the sorted array System.out.println( "\nThe sorted array: " ); for ( int i = 0; i < a.length; i++ ) System.out.print( " " + a[ i ] + " " ); } }结果如下:
相关文章推荐
- JAVA数组的两种常见排序(冒泡排序和选择排序)
- 动态数组实现冒泡排序与选择排序
- 数组排序-冒泡排序-选择排序-插入排序-希尔排序-快速排序-Java实现
- C语言数组实现冒泡排序和选择排序程序
- [javaSE] 数组(排序-冒泡排序)
- 定义一个数组工具类!数组选择排序、冒泡排序、获取最大、最小值!
- Java数组遍历、求最大值、选择排序、冒泡排序、二分查找
- java类实现数组的五种排序 冒泡排序、选择排序、插入排序、希尔排序、数组排序
- 常见的两种数组排序:选择排序和冒泡排序
- 【Java】数组的冒泡排序和选择排序(原理+代码实现)
- 数组排序-冒泡排序
- C#委托的简单学习:定义委托和使用、Func<int f1,out r1>、给自定义的类或数组排序(冒泡排序)
- 数组的常见操作:冒泡排序 选择排序与取最值
- 数组排序(冒泡排序)
- 关于数组的几个小题目-冒泡排序、二分查找、直接选择排序、反转数组
- 例题:输入学生的各项资料,然后根据学生的分数,重新排序。重新复习结构体,集合,数组,for循环,冒泡排序,水平符的使用。
- 数组排序之冒泡排序
- 数组中排序的一些方法,选择排序,冒泡排序,和数组中自己的方法
- 数组的常见操作:冒泡排序 选择排序与取最值
- Java实现数组排序含冒泡排序、选择排序