Java structure and algorithm bubble sort

Source: Internet
Author: User

First, what is bubble sort: the bubbling sort is compared between two adjacent numbers, where the previous value is defined as before, and the following value is defined as after; when Before>after I, exchange their values, if before<after, do not swap.

The time complexity of bubble sort is indicated by the large O method: O (n^2), low computational efficiency, but the algorithm idea is simple, it is a sort method of comparison basis

Bubble sort Idea Diagram:

        

Bubble Sort Reference Code:

 Public classPopsort {int[] Array =NULL; @Test Public voidTestpopsort () {array=New int[5]; array[0] = 45; array[1] = 22; array[2] = 33; array[3] = 43; array[4] = 34; intNelmes =Array.Length;  for(inti = nElmes-1; i > 1; i--) {             for(intj = 0; J < I; J + +) {                if(Array[j] > array[j + 1]) {swap (J, J+ 1);    }}} System.out.println (Arrays.tostring (array)); }    Private voidSwapintOneintBoth ) {Integer temp=Array[one]; Array[one]=Array[two]; Array[two]=temp; }}

Java structure and algorithm bubble sort

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.