62,626
社区成员
发帖
与我相关
我的任务
分享/*数组排序
* 1. 选择排序 selectSort
* 2. 冒泡排序 bubbleSort
* 3. 插入排序 insertSort
*/
public class Demo6_12 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("Please Enter the numbers: ");
java.util.Scanner input = new java.util.Scanner(System.in);
double[] numbers = new double[5];
//录入测试化数据
for(int i=0; i<numbers.length; i++){
numbers[i] = input.nextDouble();
}
//selectSort 后 输出
System.out.println("The selectSort result: " );
numbers = selectSort(numbers);
for(double num :numbers )
System.out.print(num+" ");
System.out.println();
//bubbleSort 后 输出
//insertSort 后 输出
}
//selectSort 数组
public static double[] selectSort (double[] x){
// double min = 0;
int index = 0;
double temp = 0;
for(int i=0; i<x.length; i++){
double min = x[i];
for(int j=i+1; j<x.length; j++)
//记录最小值
if (x[j] < min){
index = j;
min = x[j];
}
//交换数据
temp = x[i];
x[i] = min;
x[index] = temp;
}
return x;
}// close selectSort
}
public class demo6 {
public static void main(String[] args) {
System.out.print("Please Enter the numbers: ");
java.util.Scanner input = new java.util.Scanner(System.in);
double[] numbers = new double[5];
// 录入测试化数据
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextDouble();
}
// selectSort 后 输出
System.out.println("The selectSort result: ");
numbers = selectSort(numbers);
for (double num : numbers) {
System.out.print(num + " ");
System.out.println();
}
}
// selectSort 数组
public static double[] selectSort(double[] x) {
// double min = 0;
// int index = 0;
// double temp = 0;
//
// for (int i = 0; i < x.length; i++) {
// double min = x[i];
// for (int j = i + 1; j < x.length; j++) {
// // 记录最小值
// if (x[j] < min) {
// index = j;
// min = x[j];
// }
// // 交换数据
// temp = x[i];
// x[i] = min;
// x[index] = temp;
// }
//
// }
// return x;
// }
for (int i = 0; i < x.length; i++) {
double temp = 0;
for (int j = i + 1; j < x.length; j++) {
if (x[i] > x[j]) {
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
return x;
}
}