Shiqianfeng against the war in class for the first 13 days;
Saturday today, but to make a lucid lesson, so JAVA_DAY11;
today we ended the notepad to write code phase; started using eclipse to write code ;
learning courses are familiar with the eclipse routine use, and select the sort commonly used class of two-dimensional arrays;
Chinese world refuel refueling!!
I Come!
package homework;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthSpinnerUI;
/**
* 次代码为今天学的剩余两种排序:选择排序+利用导包排序.
*/
public class Test1 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入长度:");
int len = sc.nextInt();
int[] arr = new int[len];
for(int i = 0;i<arr.length;i++){
System.out.println("请输入第"+(i+1)+"个数:");
arr[i] = sc.nextInt();
}
// 函数调用选择排序.
selectionSort(arr);
// 利用Java类来排序.
Arrays.sort(arr);
for(int e :arr){
System.out.print(e+"\t");
}
System.out.println("main ... over");
}
/**
* 选择排序: 思路: 每一轮找个基准值:在基准值上放一个比较小的数.
* 定义一个变量k用于记录每轮最小值的下标,以便交换.
*/
public static void selectionSort(int[] arr){
for(int i =0;i<arr.length;i++){
int k=i;
for(int j = k+1;j<arr.length;j++){
if(arr[k]>arr[j])
k=j;
}
// 防止过度交换:因为一轮后k值没变的话,就没必要交换.
if(k!=i){
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
for(int e :arr){
System.out.print(e+"\t");
}
System.out.println("选择排序...over");
}
}