Bubble Sorting by sorting algorithm (JAVA)

Source: Internet
Author: User

[Java] public class BubbleSort {/*** because the sorting process always places a decimal number forward and a large number backward, it is equivalent to a bubble rising, so it is called a Bubble sorting. * Time Complexity: O (n ^ 2) * stable sorting mode * @ param nums array to be sorted * @ return output ordered array */public static void sort (int [] nums) {boolean isChanged; for (int I = 0; I <nums. length; I ++) {isChanged = false; // if a large element is found, move it back for (int j = 0; j <nums. length-i-1; j ++) {if (nums [j]> nums [j + 1]) {int temp = nums [j + 1]; nums [j + 1] = nums [j]; nums [j] = temp; isChanged = true ;}// if not moved, the sequence is ordered, jump out of the loop if (! IsChanged) {break ;}}}}

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.