package ArrayDemo;
/**
* @author pplsunny
* @category .21
*/
public class ArrayDemo {
/**
* 用增强for循环输出排序结果
*/
public static void main(String[] args) {
int[] a = { 2, 4, 76, 12, 34, 23, 86 };
ArrayDemo.bubbleSort(a);
for (int b : a) {
System.out.print(b + " ");
}
}
/*
* 冒泡排序函数,定义为静态的方便使用,
* 也是开发中定义工具类的一个方法
*/
public static void bubbleSort(int a[]) {
for (int i = 1; i < a.length; i++) {
//这是控制趟数
for (int j = 0; j < a.length - i; j++) {
//j < a.length - i,比较元素的个数
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
/*
* 冒泡排序函数改进版
*/
public static void BubbleSort(int[] a) {
boolean flag = true;
while (flag) {
flag = false;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i ; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = true;
}
}
if (!flag)
break; // 如果没有发生交换,则退出循环
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有