Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Ordering of three numerical values written by Jqva.
Ordering of three numerical values written by Jqva.
Category array test {

public static void select sort(int[]arr){

for(int x = 0; X< array length; x++) {

for(int j = x+ 1; J< array length; j++) {

if(arr[x]& gt; arr[j]) {

int temp = arr[x];

arr[x]= arr[j];

arr[j]= temp;

}

}

}

}

public static void print array(int[]arr){

for(int x = 0; X< array length; x++) {

If (x! = array length-1) {

System.out.print(arr[x] +",");

}

Otherwise {

system . out . print(arr[x]); //So there is no comma in the last digit.

}

}

}

public static void bubble sort(int[]arr)

{

for(int x = 0; x & ltarr . length- 1; x++)

{

for(int y = 0; Y< array length-x-1; Y++) //-x: Reduce the number of elements in each comparison,-1: Avoid sorting corner labels.

{

if(arr[y]& gt; arr[y+ 1]){

int temp = arr[y];

arr[y]= arr[y+ 1];

arr[y+ 1]= temp;

}

}

}

}

Public static void main(String[] args) {

int[] arr = {4,2, 1 };

Print array (arr);

system . out . println();

select sort(arr);

Print array (arr);

system . out . println();

bubbleSort(arr);

Print array (arr);

}

}

There are two methods: selective sorting and bubble sorting. You can choose for yourself.