Numbers from small to large in Java arrays--bubble Sort method

Source: Internet
Author: User

Defines an array of type int and arranges the number of them from small to large.

Train of thought: Take two numbers from the array to compare and keep the smallest value to compare with the other values in the group, get the minimum value, and put him first (I loop once, J makes all the comparisons of all values)

The first for loop can only complete the arrangement of a minimum value at a time, and when the loop completes, the sort is completed.

Public int[] Bubblesortball (int[] array) {//bubble sort, sort the numbers from small to large for (int i = 0; i < Array.Length; i++) {//Remove the first of the array A value
for (int j = i+1 J < array.length-1; J + +) {//Remove second value from array
if (Array[i]>array[j]) {//the size of two values is compared in the first loop, two values are interchanged if the first value is greater than the second value, otherwise the first value is compared with the value of the next loop
Array[i] = Array[i]^array[j];
ARRAY[J] = Array[i]^array[j]; Three times different or budget, the completion is worth exchanging.
Array[i] = Array[i]^array[j];
}
}
}

return array;

}

Related Article

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.