Java Implementation selection and sorting
Public static void main (String [] args) {// array int [] arr = {2, 5, 7, 1, 3, 8, 6, 4} to be sorted }; // control the subscript of each element in the array. The element symbol to be compared for (int I = 0; I <arr. length; I ++) {// all the subscript for (int j = I + 1; j <arr. length; j ++) {// if arr [I] is greater than arr [j] if (arr [I]> arr [j]) {// define the Temporary Variable storage arr [I] and larger int tmp = arr [I] In arr [j]; // exchange the two values of the two comparison positions, arr [I] = arr [j]; arr [j] = tmp ;}}for (int I = 0; I <arr. length; I ++) {// System in ascending order after sorting output. out. println (arr [I]);}
Sorting principle: Compare the elements on the leftmost position with all elements on the right. If the conditions are met, swap the positions of the elements on the two positions to the leftmost value.