How to arrange the numbers in the array in order from small to large???

Source: Internet
Author: User

After the video learning, summed up a bit, the great god please advise ....

If you want to arrange the numbers in the array in order from small to large.

Complete the idea: put the smallest number in the first item of the array, place the second small array in the second item, repeat it, and know it is done.

So how do you put the smallest number where the array index is 0?

Example:

for (int j = I+1;j < arrays. length;j++)

{

if (Arrays[0] > Arrays[j])

{

int temp = arrays[0];

Arrays[0] = Arrays[j];

ARRAYS[J] = temp;

}

}

Implementation method: The number of the first position is taken out and then compared to the number in the back position, if the number in the back position is large, then swap position.

Code Implementation Analysis: Repeated actions, if the index is 0 of the digital peso cited as the number of J large, then swap. The value range of J is 1-arrays. Length-1.

int i =? ;//array index bits currently being processed

for (int j = i + 1;j < arrays. length;j++)

{

if (Arrays[i] > Arrays[j])

{

int temp = Arrays[i];

Arrays[i] = Arrays[j];

ARRAYS[J] = temp;

}

}

Since 0 in the example is represented as an array index of the current processing, it is proposed to form a variable i, and the position of the variable J should begin with the next item in Index I.

Example: Use Exchange order to implement data arrays from small to large for type int:

for (int i = 0;i < arrays. Length-1; i++)

{

for (int j = i + 1;j < arrays. length;i++)

{

if (Arrays[i] > Arrays[j])

{

int temp = Arrays[i];

Arrays[i] = Arrays[j];

ARRAYS[J] = temp;

}

}

}

Similarly, an example of an array of type double arrays from small to large:

for (int i = 0;i < arrays. length-1;i++)

{

for (int j = i + 1;j < arrays. length;j++)

{

if (Arrays[i] < arrays[j])

{

Double temp = arrays[i];

Arrays[i] = Arrays[j];

ARRAYS[J] = temp;

}

}

}

Some of the above summarizes may be wrong, the gods have seen trouble to inform, grateful ...

How to arrange the numbers in the array in order from small to large???

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.