冒泡排序算法原理及JAVA实现
2015-09-10 12:56
369 查看
冒泡排序法:关键字较小的记录好比气泡逐趟上浮,关键字较大的记录好比石块下沉,每趟有一块最大的石块沉底。
算法本质:(最大值是关键点,肯定放到最后了,如此循环)每次都从第一位向后滚动比较,使最大值沉底,最小值上升一次,最后一位向前推进(即最后一位刚确定的最大值不再参加比较,比较次数减1)
复杂度: 时间复杂度 O(n2)
,空间复杂度O(1)
JAVA源代码(成功运行,需要Date类)
[java] view
plaincopyprint?
public static void bubbleSort(Date[] days) {
int len = days.length;
Date temp;
for (int i = len - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (days[j].compare(days[j + 1]) > 0) {
temp = days[j + 1];
days[j + 1] = days[j];
days[j] = temp;
}
}
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
public int compare(Date date) {
return year > date.year ? 1 : year < date.year ? -1
: month > date.month ? 1 : month < date.month ? -1
: day > date.day ? 1 : day < date.day ? -1 : 0;
}
public void print() {
System.out.println(year + " " + month + " " + day);
}
}
[java] view
plaincopyprint?
package testSortAlgorithm;
public class BubbleSort {
public static void main(String[] args) {
int array[] = { 5, 6, 8, 4, 2, 4, 9, 0 };
bubbleSort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void bubbleSort(int array[]) {
int temp;
for (int i = array.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
转载于:http://blog.csdn.net/xuxurui007/article/details/7640490
算法本质:(最大值是关键点,肯定放到最后了,如此循环)每次都从第一位向后滚动比较,使最大值沉底,最小值上升一次,最后一位向前推进(即最后一位刚确定的最大值不再参加比较,比较次数减1)
复杂度: 时间复杂度 O(n2)
,空间复杂度O(1)
JAVA源代码(成功运行,需要Date类)
[java] view
plaincopyprint?
public static void bubbleSort(Date[] days) {
int len = days.length;
Date temp;
for (int i = len - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (days[j].compare(days[j + 1]) > 0) {
temp = days[j + 1];
days[j + 1] = days[j];
days[j] = temp;
}
}
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
public int compare(Date date) {
return year > date.year ? 1 : year < date.year ? -1
: month > date.month ? 1 : month < date.month ? -1
: day > date.day ? 1 : day < date.day ? -1 : 0;
}
public void print() {
System.out.println(year + " " + month + " " + day);
}
}
[java] view
plaincopyprint?
package testSortAlgorithm;
public class BubbleSort {
public static void main(String[] args) {
int array[] = { 5, 6, 8, 4, 2, 4, 9, 0 };
bubbleSort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void bubbleSort(int array[]) {
int temp;
for (int i = array.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
转载于:http://blog.csdn.net/xuxurui007/article/details/7640490
相关文章推荐
- java3
- 什么导致了Context泄露:Handler&内部类
- eclipse中查看storm0.9.3示例代码storm-start-05.docx
- SysLog简介和java操作实例
- Java实现图的遍历(深搜与广搜)
- java简单的输入语句(字符串)
- JAVA基础应用——POI操作Excel
- PWC6345: There is an error in invoking javac
- Spring MVC 总结一(spring mvc 基本例子)
- java-list去除重复的对象
- Eclipse JAVA文件注释乱码
- java 排序算法--冒泡排序
- MyEclipse使用汇总——MyEclipse10设备SVN插入
- Java简单的输入语句练习(整型数据)
- java 值交换
- java类文件的单元测试
- java设计模式系列--单例模式
- Java7线程学习笔记(一)
- java中文所占字节数
- 路径分隔符 关于Java的File.separator