Import java.util.*;//Select Sort class switchtest{public static void Main (string[] args) {int[] arr = {3,5,6,23,45, 2}; Sort before PrintArray (arr); Sorted after//Selectsort (arr); Arrays.sort (arr);//java already defined in a sort order, in development, an array of sorts, to use the sentence code//Bubblesort (arr); PrintArray (arr); }/* finds that the element that satisfies the condition needs to be position-displaced regardless of the sort. So this part of the same code can be extracted, individually encapsulated into a function */public static void Swap (int[] Arr,int a,int b) {int temp = Arr[a]; Arr[a] = arr[b]; ARR[B] = temp; }//bubble sort public static void Bubblesort (int[] arr) {for (int x=0;x<arr.length-1;x++) { for (int y=0;y<arr.length-x-1;y++)//-x: Reduces the element for each comparison, 1, avoiding bounds {if (arr[y]>arr[y+1]) {int temp = Arr[y]; Arr[y] = arr[y+1]; ARR[Y+1] = temp; }}}}//select sort public static void selectsORT (int[] arr) {for (int. x=0;x<arr.length-1;x++) {for (int y=x+1;y<arr.length;y++) {if (Arr[x]>arr[y]) {int temp = arr[x]; ARR[X] = Arr[y]; Arr[y] = temp; }}}} public static void PrintArray (int[] arr) {System.out.print ("["); for (int x=0;x<arr.length;x++) {if (x!=arr.length-1) System.out.print (arr[x]+ ","); else System.out.println (arr[x]+ "]"); } }}
Sorting arrays in Java. Knowledge points