Java array ordering sample sharing _java

Source: Internet
Author: User

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 + "");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.