Four types of java array sorting and four types of java Array
Four sorting types of Arrays
1. Quick sorting method Arrays. sort ();
2. Bubble Sorting
1 int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51}; 2 int temp=0; 3 for (int i = 0; i < a.length - 1; i++) 4 { 5 for (int j = 0; j < a.length - 1 - i; j++) 6 { 7 if (a[j] > a[j + 1]) 8 { 9 temp = a[j];10 a[j] = a[j + 1];11 a[j + 1] = temp;12 }13 }14 }
3. Select sort
1 int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51}; 2 for(int i=0;i<a.length;i++){ 3 for(int j=i+1;j<a.length;j++){ 4 if(a[i]>a[j]){ 5 int temp=a[i]; 6 a[i]=a[j]; 7 a[j]=temp; 8 } 9 }10 }
4. Insert sorting
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,18,23,34,15,35,25,53,51}; for (int i = 1; i < a.length; i++) { for (int j = i; j > 0; j--) { if (a[j] < a[j - 1]) { int temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; } else break; } }
JAVA array sorting
Arrays. sort (x);, in upper case!
In addition, you lack the header file import java. lang. reflect. Array;
You can do this.
Import java. lang. reflect. Array;
Import java. util .*;
Class aa {
Public static void main (String [] args ){
Int [] x = new int [] {3, 2, 4, 5, 7 };
Arrays. sort (x );
For (int j = 0; j <x. length; j ++)
{
System. out. println (x [j]);
}
}
}
How to sort class arrays in java
Use the for nested loop and then write another temp variable. Like the array attribute, store the median value. The bubble method is the simplest,
First, compare the first value with the following value. The first loop will find the smallest integer value, and then store the smallest array in temp, store the External Loop array to this array, and then put the temp value into the External Loop array, then compare the second value with the third ------- to the last value to find the smaller one. The exchange method is the same as the previous one,
After the loop is complete, sort the order,
For example, if the array subscript is 10, the External Loop is 1-9 from 0-8.