Class:Nums Permission:public
Method: main permission: public
Parameters: nums,i,j,num;
Parameter introduction:
nums, the data type int[], is used to store a series of arrays of int type;
i, the data type int, as the loop variable of the for loop, stores the number of rounds for sorting and comparison;
j, data type int, as a loop variable for the for loop, storing the number of times the round sort and comparison is performed;
num, data type int, as a third-party variable that interchanges between two values.
Method Function:
Define an int[] array;
Set a loop variable i to record the number of comparison rounds;
Set a loop variable j to record the number of comparisons in this round of comparisons;
Compare the first number that is not sorted in the array with other numbers that follow;
If the first number that is not sorted is smaller than the number that is compared with it, exchange their positions to ensure that the first number that is not sorted is always the largest number that has participated in the comparison;
After the loop is completed, the sort results are output using the iterative loop.
The code copy is as follows:
public class Nums {
public static void main(String[] arge ){
//Define an int number with type array nums and assign an initial value;
int[] nums = new int[] {12,24,34,4,45,17,65,51,25};
//Set a cycle to record the number of comparison rounds;
for (int i = 0; i < nums.length-1;i++){
//Set a cycle to record the number of comparisons in this round of comparison;
for(int j = 0; j < nums.length-1-i;j++){
//Compare the first number that is not sorted in the array with other numbers afterwards. If the other numbers are larger than it, execute the following code block;
if(nums[j] < nums[j+1]){
//Exchange the unsorted first number with a number larger than it to ensure that the unsorted first number is always the largest;
int num = nums[j];
nums[j] = nums[j+1];
nums[j+1] = num;
}
}
}//Sorting is completed;
//Sorted by iterative loop output
for(int num :nums){
System.out.print(num + " ");
}
}
}