冒泡法 排序
来源:互联网 发布:服务器状态监控软件 编辑:程序博客网 时间:2024/06/07 03:26
package Sort;
/*
* 简单交换排序 不是标准的冒泡排序
*/
public static void BubbleSort0(int[] a) {
int i, j;
for (i = 0; i < a.length - 1; i++) {
for (j = i + 1; j <= a.length - 1; j++) {
if (a[i] >= a[j]) {
swap(a, i, j);
}
}
}
}
public static void swap(int[] a, int i, int j) {
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
/** 此冒泡排序 从a[0]开始存储数据
* @param a
*/
public static void BubbleSort1(int[] a) {
int i, j;
for (i = 0; i < a.length - 1; i++) {
for (j = a.length - 2; j >= i; j--) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
/** 此冒泡排序 存储数组 从a[0]开始存储数据。
* @param a
*/
public static void BubbleSort2(int[] a) {
int i, j;
boolean flag = true;
for (i = 0; i < a.length - 1 && flag; i++) {
flag = false;// 初始为false。
for (j = a.length - 2; j >= i; j--) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
flag = true;// 如果数据交换 flag变为true
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
/*
* int[] b = { 0, 1, 22, 15, 6, 86, 98, 45, 32 };
* BubbleSort1(b);
* for (int i = 0; i <= b.length - 1; i++)
* System.out.print(b[i] + " ");
*/
int[] a = new int[20];
Random random = new Random();
for (int i = 0; i <= a.length-1; i++)
a[i] = random.nextInt(100);// a[i]为100内 随机数
for (int i = 0; i <= a.length - 1; i++)
System.out.print(a[i] + " ");
System.out.println("begin");
BubbleSort0(a);
for (int i = 0; i <= a.length - 1; i++)
System.out.print(a[i] + " ");
}
}
import java.util.Random;
/*
* 简单交换排序 不是标准的冒泡排序
*/
public static void BubbleSort0(int[] a) {
int i, j;
for (i = 0; i < a.length - 1; i++) {
for (j = i + 1; j <= a.length - 1; j++) {
if (a[i] >= a[j]) {
swap(a, i, j);
}
}
}
}
public static void swap(int[] a, int i, int j) {
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
/** 此冒泡排序 从a[0]开始存储数据
* @param a
*/
public static void BubbleSort1(int[] a) {
int i, j;
for (i = 0; i < a.length - 1; i++) {
for (j = a.length - 2; j >= i; j--) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
/** 此冒泡排序 存储数组 从a[0]开始存储数据。
* @param a
*/
public static void BubbleSort2(int[] a) {
int i, j;
boolean flag = true;
for (i = 0; i < a.length - 1 && flag; i++) {
flag = false;// 初始为false。
for (j = a.length - 2; j >= i; j--) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
flag = true;// 如果数据交换 flag变为true
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
/*
* int[] b = { 0, 1, 22, 15, 6, 86, 98, 45, 32 };
* BubbleSort1(b);
* for (int i = 0; i <= b.length - 1; i++)
* System.out.print(b[i] + " ");
*/
int[] a = new int[20];
Random random = new Random();
for (int i = 0; i <= a.length-1; i++)
a[i] = random.nextInt(100);// a[i]为100内 随机数
for (int i = 0; i <= a.length - 1; i++)
System.out.print(a[i] + " ");
System.out.println("begin");
BubbleSort0(a);
for (int i = 0; i <= a.length - 1; i++)
System.out.print(a[i] + " ");
}
}
0 0
- 【排序】冒泡排序法
- 常用排序-冒泡法排序
- 排序法之冒泡排序
- php排序&冒泡法排序
- 排序之冒泡排序法
- 排序算法--冒泡排序法
- 【PHP】冒泡排序法排序
- 排序算法--冒泡排序法
- 冒泡法排序
- 排序:冒泡法
- 冒泡排序法
- 冒泡法排序
- 冒泡法排序
- 冒泡排序法
- java 冒泡排序法
- 冒泡法排序
- 冒泡法排序
- 冒泡法排序
- Intersection of Two Arrays II
- JAVA微信扫码支付模式二功能实现以及回调
- cc.sequence和this.scheduleOnce连用的缺点
- 关于二叉树与树(森林)的知识点详解
- 稳健回归(Robustness regression)
- 冒泡法 排序
- 如何在Swift开发中使用CocoaPods导入的第三方库
- 查看ORACLE 数据库及表信息
- Context 都没弄明白,还怎么做 Android 开发?
- open函数的flag详解2
- 数据库唯一性约束
- 动态执行表不可访问, 本会话的自动统计被禁止。在执行菜单里你可以禁止统计, 或在v$session
- 我的简单PHP框架——LabPHP v1.0.1
- c++内存泄露检测