Class: Nums Permissions: Public
Method: Main Permission: Public
Parameters: Nums,i,j,num;
Parameter introduction:
Nums, data type int[], used to store a series of arrays of type int;
I, the data type int, as the loop variable of the For loop, stores the number of rotations for the sort comparison;
j, data type int, as a loop variable for a for loop, that stores the number of times the order of the wheel is compared;
num, the data type int, as a third-party variable of two-value interchange.
Method Features:
Defines a int[] array;
Set a loop variable I to record the number of rounds;
Set a loop variable J to record the number of comparisons in the wheel comparison;
Compares the first number that is not sorted in the array with the other numbers that follow;
If the first number that is not sorted is smaller than the number compared to him, the position is exchanged to ensure that the first number of unsorted is always the largest in the number of comparisons;
After the loop completes, the result is sorted by iteration loop output.
Copy Code code as follows:
public class Nums {
public static void Main (string[] arge) {
Defines an int with an array of nums, and assigns an initial value;
int[] Nums = new int[] {12,24,34,4,45,17,65,51,25};
Set up a loop to record the number of rounds compared;
for (int i = 0; i < nums.length-1;i++) {
Set a loop to record the number of comparisons in the wheel comparison;
for (int j = 0; J < nums.length-1-i;j++) {
Compares the first number that is not sorted in the array with the other numbers that follow, and executes the following block of code if the other number is larger than it is;
if (Nums[j] < nums[j+1]) {
The first digit of the unsorted completion is exchanged with the larger number, which guarantees that the first number of unsorted is always the largest;
int num = nums[j];
NUMS[J] = nums[j+1];
NUMS[J+1] = num;
}
}
}//sorting completed;
After the iteration loop output sort completes the
for (int num:nums) {
System.out.print (num + "");
}
}
}