The idea of sorting is to compare the current data with all the data behind it, choose the smallest one, and put it in the current position.
Import Java.util.Arrays;
Import Java.util.Random;
Select sort
public class Selectsort {
public static void Main (string[] args) {
//1. Generates an array of random numbers between 1-100, with an array length of ten
Int[] Arr=new int[10];
Random ran=new random ();
for (int i=0;i<10;i++) {
Arr[i]=ran.nextint (100) +1;
System.out.print (arr[i]+ ",");
}
System.out.println ();
//2. Call Selection Sorting algorithm
Selectsort (arr);
3. Output sorted Array
System.out.print (arrays.tostring (arr));
}
//Select sorting algorithm
public static void Selectsort (int[] arr) {
The first number in the array is compared to the second-to-last number, and the outer-for-loop can be swapped from the smallest number in the data after Arr[i], and Arr[i].
for (int i=0;i<arr.length-1;i++) {
Each data that is taken out of the external for Loop Arr[i] is compared to all the data behind it, and the smallest data arr[i] is swapped out from it.
The scope of the inner for loop is the data from the outer for loop corresponding to the data Arr[i] begins until the last element of the array.
for (int j=i+1;j<arr.length;j++) {
if (Arr[j]<arr[i]) {
int temp=arr[i];
ARR[I]=ARR[J];
Arr[j]=temp;
}
}
}
}
}
Java Foundation------Select sort