Array ordering: The array itself has the sort () method in the Arrays class, where several common sorting methods are written. (1) Bubble sorting method
public static void Main (string[] args) {Scanner in=new Scanner (system.in), int i,j;//i is used to store the value of the smallest element of an array, j is used to position the element int[] Array=new int[10];//defines an integer array of length 10 int itemp=0;//variable that represents the array element minimum value int ipos=0;//represents the lowest element subscript System.out.println ("Please enter 10 number:"); for (int n=0;n<array.length;n++) {//Traversal 10 elements of the input array Array[n]=in.nextint ();} /* * Sort array */for (i=0;i<9;i++) {//Outer loop itemp=array[i];//Set current element to minimum ipos=i;//record minimum element position for (j=i+1;j<10;j++) {// The inner loop represents the predicted urea to be compared with the smallest element and the number of times if (array[j]<itemp) {//If later elements have a smaller number than the temporary minimum value, then the value of the re-assignment is the minimum itemp=array[j];ipos=j;} The position of the exchange minimum value array[ipos]=array[i];array[i]=itemp;}} Output array System.out.println ("Select order from small to large:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}
(2) Bubble sorting method
public class Maopaopaixu {public static void main (string[] args) {Scanner in=new Scanner (system.in); int i=0,j=0;//to control the inner and outer layers Loop int[] Array=new int[10];int itemp=0;//is used to store 10 elements of the intermediate variable//input array System.out.println ("Please enter 10 numbers:"); for (int n=0;n< array.length;n++) {array[n]=in.nextint ();} Bubble Sort method for (i=0;i<array.length-1;i++) {for (j=i;j<array.length-1;j++) {if (array[j]>array[j+1]) {iTemp= array[j];array[j]=array[j+1];array[j+1]=itemp;}}} System.out.println ("After bubble sort:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}}
(3) Exchange sorting algorithm
Package Arrayssort;import java.util.scanner;/** * Exchange sorting algorithm: The interchange sorting algorithm is to compare each digit to all other numbers, and if the data that meets the criteria is found, the Exchange location * @author Administrator * */public class Jiaohuanpaixu {public static void main (string[] args) {Scanner in=new Scanner (system.in); T i=0,j=0;int[] array=new int[10];int itemp=0; System.out.println ("10 numbers of input arrays:"); for (int n=0;n<array.length;n++) {array[n]=in.nextint ();} Interchange sort for (i=0;i<array.length-1;i++) {for (j=i+1;j<array.length;j++) {if (Array[i]<array[j]) {itemp=array[i ];array[i]=array[j];array[j]=itemp;}}} System.out.println ("The number of digits after the interchange is:"); for (int n=0;n<array.length;n++) {System.out.print (array[n]+ "\ t");}}
(4) Insert Sort method (c basic sort algorithm inside)