版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AngeloWolf/article/details/52234586
package com.angelo;
import java.util.Random;
import java.util.Scanner;
public class SelectSort {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入数组大小:");
int k = sc.nextInt();
int array[] = new int[k];
for (int i = 0; i < array.length; i++) {
Random rd = new Random();
int s = rd.nextInt(30);
array[i] = s;
}
System.out.println("数组排序之前:");
print(array);
System.out.println("数组排序之后:");
selectSort(array);
print(array);
}
public static void print(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
System.out.println();
}
public static void selectSort(int [] b){
for(int i=1;i<b.length;i++){
int index=0;
for(int j=0;j<=b.length-i;j++){
if(b[j]>b[index])
index=j;
}
int temp = b[b.length-i];
b[b.length-i] = b[index];
b[index] = temp;
/* 每次循环将最大的数找出放到数组末尾*/
}
}
}
import java.util.Random;
import java.util.Scanner;
public class SelectSort {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入数组大小:");
int k = sc.nextInt();
int array[] = new int[k];
for (int i = 0; i < array.length; i++) {
Random rd = new Random();
int s = rd.nextInt(30);
array[i] = s;
}
System.out.println("数组排序之前:");
print(array);
System.out.println("数组排序之后:");
selectSort(array);
print(array);
}
public static void print(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
System.out.println();
}
public static void selectSort(int [] b){
for(int i=1;i<b.length;i++){
int index=0;
for(int j=0;j<=b.length-i;j++){
if(b[j]>b[index])
index=j;
}
int temp = b[b.length-i];
b[b.length-i] = b[index];
b[index] = temp;
/* 每次循环将最大的数找出放到数组末尾*/
}
}
}