import java.util.Arrays;
public class maopaoSort {
public static void bubbleSort1(
int[] arr) {
for (
int i =
0; i < arr.length -
1; i++) {
for (
int j =
0; j < arr.length - i -
1; j++) {
if (arr[j] > arr[j +
1]) {
int tmp = arr[j];
arr[j] = arr[j +
1];
arr[j +
1] = tmp;
}
}
}
}
public static void bubbleSort2(
int[] arr) {
boolean flag;
for (
int i =
0; i < arr.length -
1; i++) {
flag =
false;
for (
int j =
0; j < arr.length - i -
1; j++) {
if (arr[j] > arr[j +
1]) {
int tmp = arr[j];
arr[j] = arr[j +
1];
arr[j +
1] = tmp;
flag =
true;
}
}
if (!flag) {
break;
}
}
}
public static void bubbleSort3(
int[] arr) {
int last = arr.length -
1;
int last_temp =
0;
for (
int i =
0; i < arr.length -
1; i++) {
last_temp = last;
for (
int j =
0; j < last_temp; j++) {
if (arr[j] > arr[j +
1]) {
int tmp = arr[j];
arr[j] = arr[j +
1];
arr[j +
1] = tmp;
last = j;
}
}
if (last == last_temp) {
break;
}
}
}
public static void main(String[] args) {
int[] a = {
0,
1,
2,
3,
4,
9,
7,
56,
89,
6,
7,
9 };
bubbleSort3(a);
System.
out.println(Arrays.toString(a));
}
}
转载请注明原文地址: https://ju.6miu.com/read-2769.html