Java排序算法之选择排序
程序员文章站
2022-03-24 17:54:07
...
package SelectSort;
import java.util.Random;
public class SelectSort {
private static int size=20;
public static void main(String[] args) {
Random rd=new Random();
int []arr=new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i]=rd.nextInt(100)+1;
}
selectSort(arr);
for (int i : arr) {
System.out.print(i+" ");
}
}
private static void selectSort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = i; j < arr.length; j++) {
int temp=0;
if(arr[i]>arr[j]){
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
}
}