java基础(0508)
冒泡排序法
import java.util.Arrays;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] array = new int[100];
int j = set(array);
int n = j;
sort(j, array);
print(n,array);
}
private static void print(int n,int[] array) {
// TODO Auto-generated method stub
int[] array1 = new int[n];
for (int i = 0; i < n; i++) {
array1[i] = array[i];
}
System.out.print(Arrays.toString(array1));
}
private static void sort(int j, int[] array) {
// TODO Auto-generated method stub
int a = 0;
while (j > 0) {
for (int i = 0; i < j - 1; i++) {
if (max(array[i], array[i + 1])) {
a = array[i];
array[i] = array[i + 1];
array[i + 1] = a;
}
}
j--;
}
}
private static int set(int[] array) {
// TODO Auto-generated method stub
int n = 0;
int b = 0;
do {
b = new Scanner(System.in).nextInt();
if (b != -1) {
array[n] = b;
n++;
}
} while (b != -1);
return n;
}
private static boolean max(int a, int b) {
// TODO Auto-generated method stub
if (a > b) {
return true;
} else {
return false;
}
}
}
输入-1结束