The array sort in Java is of quicksort bubble sort and selection sort
- 2020-04-01 02:56:25
- OfStack
1. Use sort() in the Arrays class in the JavaApi documentation for quick sort
import java.util.Arrays;
public class TestOne{
public static void main(String [] args){
int [] array={2,0,1,4,5,8};
Arrays.sort(array);//Sort, the static method of Arrays, is called to Sort and Sort in ascending order
for(int show:array){
System.out.println(show);
}
}
}
2. Bubble sort
public class TestTwo{
public static void mian(String []args){
int [] arr={2,0,1,4,6,5};
int [] showArr testMethod(arr);
for(show:showArr){
System.out.println(show);
}
}
public static int [] testMethod(int [] array){
for(int x=0;x<array.length-1;x++){
for(int y=x+1;y<array.length;y++){
if(array [x]>array[y]){
int temp=array[x];// The temporary variable records the maximum value
array[x]=array[y];
array[y]=temp;
}
}
}
return array;
}
}
3. Selection sort
public class TestTwo{
public static void mian(String []args){
int [] arr={2,0,1,4,6,5};
int [] showArr testMethod1(arr);
for(show:showArr){
System.out.println(show);
}
}
public static int [] testMethod1(int [] array){
for(int x=0;x<array.length-1;x++){
int min=x;//Set x as the minimum in the following table
for(int y=x+1;y<array.length;y++){
if(array[max]>array[y]){
min=j
}
}
if(min!=x){
int temp=array[x];
array[x]=array[min];
array[min]=temp;
}
}
return array;
}
}