标签:math rand index out print select sys system package
选择排序package tp.zgz.chapter04;
public class SelectedSort {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (100 + Math.random() * (100 + 1));
}
System.out.printf("排序前的数组:");
for (int i = 0; i < 10; i++) {
System.out.printf(arr[i] + " ");
}
System.out.println();
System.out.printf("排序后的数组:");
SelectedSort.Sort(arr);
}
static void Sort(int arr[]) {
for (int i = 0; i < arr.length-1; i++) {
int index = i;
for (int j = i+1; j < arr.length; j++) {
if (arr[index]>arr[j]){
index=j;
}
}
if (index!=i){
swap(arr,index,i);
}
}
print(arr);
}
static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void print(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.printf("%d ", arr[i]);
}
}
}
标签:math rand index out print select sys system package
原文地址:https://blog.51cto.com/13416247/2444771