Bubble Sort algorithm

Source: Internet
Author: User

The bubble sort principle is to assume that an array has n digits to be sorted, compare the first two numbers in the array, exchange them if they do not conform to the rules, then swap the second and third numbers, and so on, and so on, the first round of comparisons is that the largest or smallest value in this array is the last one in the array. Then the second round or repeat the first round of action, the final result is the unordered array of the largest or smallest value, stored in the second-to-last bit of the array. Then repeat the work in turn. The last round is a comparison of the first two numbers, resulting in a larger or smaller deposit in the second place. The entire array is sorted to completion. The code implemented with Java is as follows:

 public  static  void  bubblesort ([] array) { Span style= "color: #0000ff;" >for  (int  i =0; i<array.length-1;i++) { for  (int  J=0;j<array.lengt H-i-1; J++ if  (Array[j] > array[j+1< Span style= "color: #000000;"                    >] { int  temp = Array[j];                    ARRAY[J]  = Array[j+1];                Array[j  +1] = temp; }            }        }    }

The time complexity of the bubble sort is O (n^2) and the space complexity is: O (1)

Bubble Sort algorithm

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.