Bubble sort (bubble sort)---go to xingoo Blog

Source: Internet
Author: User

Bubble/' b?b (?) l/n Bubble Foam v makes the bubbling swap/sw?p/n exchange of objects V with .... Exchange Trade Exchange

Bubble sort two ideas (primarily for arrays): two-step

1) Query minimum value (the minimum value of the first position is queried)

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

if (A[0]>a[i]) {

Swap (0, i);

}

}

public void Swap (int i, int j) {

int tmp = A[i];

int min = a[j];

int a[j] = tmp;

}

2) for loop (traverse the minimum value of all positions sequentially

The outer for loop queries all locations (i's last traversal is not necessary because i==a.length-1 time, j==a.length-1 does not loop so i<a.length-1)

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

Inner for loop is the minimum value of the query

for (int j=i+1; j<a.length; J + +) {

if (A[i]<a[j]) {

Swap (I,J)

}

}

}

for (int i=0;i<length-1;i++) {
Inner loop is the maximum value on the last side
for (int j=0;j<length-i-1;j++) { if (a[j]>a[j+1]) swap (j,j+1); }
}

Sample transfer from other blogs

Class array{

int [] A;

int length;

Public Array (int max) {

Int[] A = new Int[max];

length=0;

}

public void Insert (int value) {

A[length] = value;

}

Public String display (int[] a) {

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

System.out.println ("a[" +i+ "] =" +i);

}

}

}

Bubble sort (bubble sort)---go to xingoo blog

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.