62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class BubbleSort {
public static void bubbleSort(int[] a) {
int in, out;// 内循环,外循环
for (out = a.length - 1; out > 0; out--)
for (in = 0; in < out; in++) {
if (a[in] > a[in + 1])
swap(a, in, in + 1);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入元素的个数:");
int n = input.nextInt();
int[] data = new int[n];
System.out.println("请输入" + n + "个数,用空格分隔:");
for(int i=0; i<data.length; i++) {
data[i] = input.nextInt();
}
bubbleSort(data);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 因为不知道要输入多少个数据,所以用List
List<Integer> nsList = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
try {
// 随便输入一个非数字按下回车结束输入,进行排序
System.out.println("输入整数进行排序:");
while (true) {
nsList.add(scanner.nextInt());
}
} catch (Exception e) {
System.out.println("开始排序.");
}
// 把List中的数据保存到数组中
int[] ns = new int[nsList.size()];
for (int i = 0; i < ns.length; ++i) {
ns[i] = nsList.get(i);
}
// 进行排序
BubbleSort.bubbleSort(ns);
System.out.println(Arrays.toString(ns));
}
}
class BubbleSort {
public static void bubbleSort(int[] a) {
int in, out;// 内循环,外循环
for (out = a.length - 1; out > 0; out--)
for (in = 0; in < out; in++) {
if (a[in] > a[in + 1]) swap(a, in, in + 1);
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
public static void main(String[] args) {
int[] data = { 11, 3, 82, 38, 5, 333, 22, 11 };
bubbleSort(data);
}
}
import java.util.Scanner;
public class BubbleSort {
public static void bubbleSort(int[] a) {
int in, out;// 内循环,外循环
for (out = a.length - 1; out > 0; out--)
for (in = 0; in < out; in++) {
if (a[in] > a[in + 1])
swap(a, in, in + 1);
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
public static void main(String[] args) {
int[] data = new int[10];
Scanner input = new Scanner(System.in);
System.out.println("请输入10个数,用空格分隔:");
for(int i=0; i<data.length; i++) {
data[i] = input.nextInt();
}
bubbleSort(data);
}
}
import java.util.Arrays;
import java.util.Scanner;
public class F {
public static void main(String[] agrs) {
/*接收数据*/
System.out.println("数组的大小?");
Scanner r = new Scanner(System.in);
int n = r.nextInt();
System.out.println("输入数组:");
int[] num = new int[n];
for (int i = 0; i < n; i++) {
num[i] = r.nextInt();
}
/*排序*/
Arrays.sort(num);
/*输出*/
for (int i : num) {
System.out.println(i);
}
}
}