java冒泡排序 java冒泡排序代码 要能用的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void sort(int[] data) { int temp; for (int i = 0; i < data.length; i++) { for (int j = data.length - 1; j > i; j--) { if (data[i] > data[j]) { temp = data[i]; data[i] = data[j]; data[j] = temp; } } } } public static void main(String[] args) { int[] a = { 4, 2, 3, 1, 5 }; sort(a); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); } public class Sort { public static void main(String[] args) { int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 }; System.out.println("冒泡排序的结果:"); maoPao(i); System.out.println(); System.out.println("选择排序的结果:"); xuanZe(i); System.out.println(); System.out.println("插入排序的结果:"); chaRu(i); System.out.println(); System.out.println("希尔排序的结果:"); shell(i); } // 冒泡排序算法 public static void maoPao(int[] x) { for (int i = 0; i < x.length; i++) { for (int j = i + 1; j < x.length; j++) { if (x[i] > x[j]) { int temp = x[i]; x[i] = x[j]; x[j] = temp; } } } for (int i : x) { System.out.print(i + " "); } } // 选择排序算法 public static void xuanZe(int[] x) { for (int i = 0; i < x.length; i++) { int lowerIndex = i; // 找出最小的一个索引 for (int j = i + 1; j < x.length; j++) { if (x[j] < x[lowerIndex]) { lowerIndex = j; } } // 交换 int temp = x[i]; x[i] = x[lowerIndex]; x[lowerIndex] = temp; } for (int i : x) { System.out.print(i + " "); } } // 插入排序算法 public static void chaRu(int[] x) { for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦 for (int j = i; j > 0; j--) { if (x[j] < x[j - 1]) { int temp = x[j]; x[j] = x[j - 1]; x[j - 1] = temp; } } } for (int i : x) { System.out.print(i + " "); } } // 希尔排序算法 public static void shell(int[] x) { // 分组 for (int increment = x.length / 2; increment > 0; increment /= 2) { // 每个组内排序 for (int i = increment; i < x.length; i++) { int temp = x[i]; int j = 0; for (j = i; j >= increment; j -= increment) { if (temp < x[j - increment]) { x[j] = x[j - increment]; } else { break; } } x[j] = temp; } } for (int i : x) { System.out.print(i + " "); } }} public class BubbleSort { private Number []source; public BubbleSort(Number []source) { this.source=source; } public Number[] doSort() { for(int i=source.length-1;i>1;i--) { for(int j = 0; j < i; j++) if(source[j].doubleValue() > source[j + 1].doubleValue()){ Number tmp=source[j]; source[j]=source[j+1]; source[j+1]=tmp; } } return source; } public static void display(Number []source) { for(int i=0;i<source.length;i++) { System.out.println("source["+i+"] = "+source[i]); } } public static void main(String[] args) { Number []source={new Integer(4),new Double(2.56),new Float(9.11), new Long(2),new Integer("2"),new Double(5.999999999)}; BubbleSort.display(source); BubbleSort bubble=new BubbleSort(source); source=bubble.doSort(); BubbleSort.display(source); } } 求推荐一本靠谱的讲jsp的好书 请帮忙推荐几本webwork的书籍 给一个字符串,怎么获得它的字符编码 急,这都是什么错?要导什么包? JAVA 文件操作问题 JTable中的celleditor用jcombobox来实现,然後在jcombobox上面添加ItemListener,为什么这个监听会执行两遍 大家可以试试 不是个别代码问题 做代码就是当牛做马 字符串处理的问题 JDK 与 JSWDK 急救!!JSP表单提交后的内容无法插入数据库 java 超时处理 拜求一个统计的SQL(二)
public static void sort(int[] data) {
int temp;
for (int i = 0; i < data.length; i++) {
for (int j = data.length - 1; j > i; j--) {
if (data[i] > data[j]) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = { 4, 2, 3, 1, 5 };
sort(a);
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };
System.out.println("冒泡排序的结果:");
maoPao(i);
System.out.println();
System.out.println("选择排序的结果:");
xuanZe(i);
System.out.println();
System.out.println("插入排序的结果:");
chaRu(i);
System.out.println();
System.out.println("希尔排序的结果:");
shell(i);
} // 冒泡排序算法
public static void maoPao(int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i + 1; j < x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
} // 选择排序算法
public static void xuanZe(int[] x) {
for (int i = 0; i < x.length; i++) {
int lowerIndex = i;
// 找出最小的一个索引
for (int j = i + 1; j < x.length; j++) {
if (x[j] < x[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = x[i];
x[i] = x[lowerIndex];
x[lowerIndex] = temp;
}
for (int i : x) {
System.out.print(i + " ");
}
} // 插入排序算法
public static void chaRu(int[] x) {
for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j > 0; j--) {
if (x[j] < x[j - 1]) {
int temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
} // 希尔排序算法
public static void shell(int[] x) {
// 分组
for (int increment = x.length / 2; increment > 0; increment /= 2) {
// 每个组内排序
for (int i = increment; i < x.length; i++) {
int temp = x[i];
int j = 0;
for (j = i; j >= increment; j -= increment) {
if (temp < x[j - increment]) {
x[j] = x[j - increment];
} else {
break;
}
}
x[j] = temp;
}
} for (int i : x) {
System.out.print(i + " ");
}
}
}
private Number []source;
public BubbleSort(Number []source) {
this.source=source;
}
public Number[] doSort() {
for(int i=source.length-1;i>1;i--) {
for(int j = 0; j < i; j++)
if(source[j].doubleValue() > source[j + 1].doubleValue()){
Number tmp=source[j];
source[j]=source[j+1];
source[j+1]=tmp;
}
}
return source;
}
public static void display(Number []source) {
for(int i=0;i<source.length;i++) {
System.out.println("source["+i+"] = "+source[i]);
}
}
public static void main(String[] args) {
Number []source={new Integer(4),new Double(2.56),new Float(9.11),
new Long(2),new Integer("2"),new Double(5.999999999)};
BubbleSort.display(source);
BubbleSort bubble=new BubbleSort(source);
source=bubble.doSort();
BubbleSort.display(source);
}
}