public static void Main (string[] args) {//array to sort int[] arr = {2, 5, 7, 1, 3, 8, 6, 4};//controls the subscript of each element in the array, the element symbol to be compared for the t i = 0; i < arr.length; i++) {//operation subscript for all elements compared with I element for (int j = i + 1; j < Arr.length; J + +) {//if arr[i] greater than arr[j]if (Arr[i] > Arr[j]) {//define temporary variable save Stored arr[i] and arr[j], the larger value of the int tmp = arr[i];//compares the two value of the interchange position arr[i] = arr[j];arr[j] = tmp;}}} for (int i = 0; i < arr.length; i++) {//output sorted from small to large order System.out.println (Arr[i]);}}
Sorting principle: The leftmost element is compared to all the elements on the right, and if the condition is met the position of the element on the two positions is exchanged, the maximum value is the leftmost.
Java implementation selection sorting