Talking about bubble sort

Source: Internet
Author: User

Bubble sort is a simple sorting algorithm that compares elements of order 22 to each other.

If it is from the big to the small order, then the two elements compare each other, the big one will be in front;

Instead, it will be in the back. Bubble sorting is categorized from large to small and sorted from smallest to largest .

Code Implementation section:

From big to small

1: Declaring an array

int nums[]={1,3,-8,2,,4,5};

2: Loop through the length of the array, and determine if the previous value is less than the last value, then the largest swap to the final face

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

for (int j=i+1,j<nums.length,j++) {

If the previous value is less than the next value, replace the large value with the back.

if (Nums[i]<nums[j]) {

int temp=nums[i];

NUMS[I]=NUMS[J];

Nums[j]=temp;

}

}

}

Take out the ordered values.

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

System.out.print (Nums[i]);

}

From small to large

And from the big to the small difference is the small change to the front, big row after.

1: Declaring an array

int nums[]={1,3,-8,2,,4,5};

2: Loop through the length of the array and determine if the previous value is less than the last value, then switch the small one to the final face.

for (int i=0,i<nums.length-1,i++) {

If the previous value is less than the last value, replace the small value with the back

if (Nums[i]>nums[i+1]) {

int temp=nums[i];

NUMS[I]=NUMS[I+1];

Nums[i+1]=temp;

}

}

Take out the ordered values.

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

System.out.print (Nums[i]);

}

Talking about 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.