Java's bubble sort

Source: Internet
Author: User

Bubble sort: can be imagined as boiling water, bubbles at the bottom of the bottle is relatively small, reached the water when the maximum.

Bubble sort idea: first determine whether ascending or descending, here ascending as an example. Each two adjacent numbers are compared, the previous number is larger than the next one, the two digits are exchanged, otherwise the position remains unchanged. After a round of sorting, the largest number in the array must be at the end.

Pseudo code:

Array a={10,9,8,7,6,5,4,3,2,1}

for (int i=0;i<a.length;i++) {

for (int j=0;j<a.length-i-j;j++) {//Why is j<a.length-i-1; first interpreted minus 1, to the last two digits of the data set when A[j] and a[j++] are compared, and when there is no minus 1, the subscript is out of bounds. Why reduce I, each round of sequencing will produce (I+1) a well-sequenced data

Swap data: Swap (a[j],a[j++])

}

}

Output sorted Data: Ascending

Code:

 PackageMath;/*** Created by Administrator on 2018/3/2. * Bubble sort * thought: bubble sort is the comparison of two adjacent, according to the defined rules, decide to put the large number in front or behind, and then to Exchange order, * each order will be that The largest or smallest number of rows at the last * time complexity of bubble sort: O (n2)*/ Public classBubbletest { Public Static voidMain (string[] args) {/*//console for input data Scanner s=new Scanner (system.in);        The number entered is stored in the array Integer array[]=new integer[10];        Integer count=0;       System.out.println ("Please enter 10 number:");           while (S.hasnextint ()) {Integer tempnum=s.nextint ();           Array[count++]=tempnum;           if (count>9) {break;        }} System.out.println ("Input 10 number is:");        for (int i=0;i<10;i++) {System.out.print (array[i]+ ","); }*/Integer Count=100000; Integer array[]=NewInteger[count];  for(intw=0;w<array.length;w++) {Array[w]=count--; }        /*Bubble Sort*/        LongStarttime=System.currenttimemillis ();        System.out.println (StartTime);  for(Integer i=0;i<array.length;i++){             for(Integer j=0;j<array.length-i-1;j++){                if(array[j]>array[j+1]) {Integer temp; Temp=Array[j]; ARRAY[J]=array[j+1]; Array[j+1]=temp; }            }           /*System.out.println (The Order of "+i+" after Ordering: ");            for (Integer k=0;k<array.length;k++) {System.out.print (array[k]+ ","); }*/} Long endTime=System.currenttimemillis ();        System.out.println (EndTime); System.out.println ("total time = = =" + (endtime-startTime)); /*System.out.println ("Order after sorting:");        for (Integer f=0;f<array.length;f++) {System.out.print (array[f]+ ","); }*/    }}

Java's 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.