Bubble sort of classic algorithms in Java (Bubble sort)

Source: Internet
Author: User

Principle: compare two adjacent elements and exchange large value elements to the right end.

Idea: Compare adjacent two numbers in turn, place decimals in front, and large numbers on the back. That is, in the first trip: first compare the 1th and 2nd numbers, put the decimals before the large number. Then compare the 2nd and 3rd numbers, place the decimal before the large number, and then continue until you compare the last two numbers, put the decimals before the decimal, and put the large number behind. Repeat the first step until all sorts are complete.

Example: To sort an array: int[] arr={6,3,8,2,9,1};

First trip Sort:

First Order: 6 and 3 comparison, 6 greater than 3, swap position: 3 6 8 2 9 1

Second Order: 6 and 8 comparison, 6 less than 8, not swap position: 3 6 8 2 9 1

Third Order: 8 and 2 comparison, 8 greater than 2, swap position: 3 6 2 8 9 1

Fourth order: 8 and 9 comparisons, 8 less than 9, not swapped position: 3 6 2 8 9 1

Fifth Order: 9 and 1 comparison: 9 greater than 1, swap position: 3 6 2 8 1 9

The first trip was a total of 5 comparisons, sorted results: 3 6 2 8 1 9

---------------------------------------------------------------------

Second trip sort:

First Order: 3 and 6 comparison, 3 less than 6, not swap position: 3 6 2 8 1 9

Second Order: 6 and 2 comparison, 6 greater than 2, swap position: 3 2 6 8 1 9

Third Order: 6 and 8 comparison, 6 greater than 8, not swap position: 3 2 6 8 1 9

Fourth order: 8 and 1 comparisons, 8 greater than 1, Exchange position: 3 2 6 1 8 9

The second trip took a total of 4 comparisons, sorted results: 3 2 6 1 8 9

---------------------------------------------------------------------

Third trip Sort:

First Order: 3 and 2 comparison, 3 greater than 2, swap position: 2 3 6 1 8 9

Second Order: 3 and 6 comparison, 3 less than 6, not swap position: 2 3 6 1 8 9

Third Order: 6 and 1 comparison, 6 greater than 1, swap position: 2 3 1 6 8 9

The second trip took a total of 3 comparisons, sorted results: 2 3 1 6 8 9

---------------------------------------------------------------------

Sequence Four:

First Order: 2 and 3 comparison, 2 less than 3, not swap position: 2 3 1 6 8 9

Second order: 3 and 1 comparison, 3 greater than 1, swap position: 2 1 3 6 8 9

The second trip took a total of 2 comparisons, sorted results: 2 1 3 6 8 9

---------------------------------------------------------------------

The order of five times:

First Order: 2 and 1 comparison, 2 greater than 1, swap position: 1 2 3 6 8 9

The second trip took a total of 1 comparisons, sorted results: 1 2 3 6 8 9

---------------------------------------------------------------------

Final Result: 1 2 3 6 8 9

---------------------------------------------------------------------

This shows: N numbers to sort complete, a total of N-1 sequencing, every I trip to the order of (n-i) times, so you can use the double loop statement, the outer control loop how many times, the inner layer control the number of cycles per trip.

The advantage of bubbling sorting: each trip is sorted less once, because a larger value is found for each sort of trip. As in the example above: After the first comparison, the last number must be the largest one, and when the second pass is sorted, only the number other than the last number can be compared, and a maximum number is also found after the number of the second comparison, and the third comparison is You only need to compare the number other than the last two numbers, and so on ... That is, not a trip to compare, each trip less than once, to a certain extent, reduce the amount of the algorithm.

Code implementation:

public class Bubble {
public static void Main (String [] args) {
int [] arr={1,7,9,11,158,97,666};
System.out.print ("Array before sorting:");
for (int num:arr) {
System.out.print (num + "");
}
for (int i=0;i<arr.length-1;i++) {//external loop control loop count
for (int j=0;j<arr.length-i-1;j++) {//Internal loop control comparison number of times
if (Arr[j]>arr[j+1]) {
int TEMP=ARR[J];
ARR[J]=ARR[J+1];
Arr[j+1]=temp;
}
}
}
System.out.print ("Array after sorting:");
for (int temp:arr) {
System.out.print (temp+ "");
}
}
}


Output:
Array before sorting: 1 7 9 11 158 97 666 Sorted Array: 1 7 9 11 97 158 666

Bubble sort of classic algorithms in Java (Bubble sort)

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.