In Java, there are 4 ways to sort the numbers by using arrays: Choosing the sorting method, bubbling method, fast sorting method and inserting sort method.
Select Sort is the first number in the array as the maximum or minimum number, and then by a circular comparison of the maximum number of exchanges or the minimum number and the first digit in a round comparison of the position of the order;
Bubble sort is also the first number in the array as the largest or smallest number, the loop compares the size of two adjacent numbers, meet the conditions on the interchange position, the maximum number or the minimum number sink;
The fast sorting method is mainly implemented by using the Arrays.sort method () in the Arrays class.
The insertion sort is the selection of data in an array, which is then sorted by the constant insertion comparison.
Select Sort method
PackageCom.lovo; Public classTest01 { Public Static voidMain (string[] args) {int[] A =New int[5]; for(inti = 0; i < a.length; i++) {A[i]= (int) (Math.random () * 99 + 1); System.out.print (A[i]+ " "); } //Simple Selection Sorting for(inti = 0; i < a.length-1; i++) { intMinindex =i; for(intj = i + 1; J < A.length; J + +) { if(A[j] <A[minindex]) {Minindex=J; } } if(Minindex! =i) {inttemp =A[i]; A[i]=A[minindex]; A[minindex]=temp; } } for(intx:a) {System.out.print (x+ " "); } }}
Bubble Sort Method
Package Com.lovo;public class Test02 {public static void main (string[] args) {int[] a = new Int[5];for (int i = 0; i < a . length; i++) {A[i] = (int) (Math.random () * 99 + 1); System.out.print (A[i] + " ");} Bubble sort Boolean swapped = true;//has not occurred interchange for (int i = 1; swapped && i <= a.length-1; i++) {swapped = False;for (int j = 0; J < A.length-i; J + +) {if (A[j] > a[j + 1]) {int temp = A[j];a[j] = a[j + 1];a[j + 1] = Temp;swapped = True;}}} for (int x:a) {System.out.print (x + " ");}}}
Quick Sort Method
Package Com.lovo;import Java.util.arrays;public class Test03 {//Quick sort public static void main (string[] args) {int[] a = NE W Int[5]; System.out.print ("Before sorting:"); for (int i = 0; i < a.length; i++) {A[i] = (int) (Math.random () * 99 + 1); System.out.print (A[i] + " ");} System.out.print ("\ n sort after:"); Arrays.sort (a); Sort for (int x:a) {System.out.print (x + " ");}}}
Java---Numeric sorting