Bubble sort of classic algorithms in Java (Bubble sort)

Source: Internet
Author: User

Bubble sort of classic algorithms in Java (Bubble sort)

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 sort: 6 and 3 comparison, 6 3 3  6  8  2   9  1

second sort: 6 and 8 6 less than 8 3  6   8  2  9  1

third sort: 8 and 2 8 greater than 2 3   6  2  8  9  1

Fourth order: 8 and 9 8 less than 9 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 sort: 6 and 2 6 greater than 2 3   2  6  8  1  9

third sort: 6 and 8 6 greater than 8 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 sort: 3 and 6 3 less than 6 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 , the number of orders per I trip is (n-i) times, So you can use the double loop statement, the outer control loop how many times, the inner layer controls the number of cycles per trip, that is

for (int i=1;i<arr.length;i++) {for    (int j=1;j<arr.length-i;j++) {    //swap location}    

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.

In terms of time complexity:

1. If our data is in the right order, we need to take a trip to complete the sequencing. The required number of comparisons and the number of records moved to the minimum value, namely: cmin=n-1; Mmin=0; Therefore, the best time complexity for bubble sorting is O (n).

  2. If unfortunately our data is reversed, we need to n-1 sort of a trip. Each order is n-i compared (1≤i≤n-1), and each comparison must move the record three times to reach the Exchange record location. In this case, the comparison and the number of moves reach the maximum: the worst time complexity for the bubbling sort is: O (n2).

Summary: The total average time complexity of the bubble sort is: O (n2) .

Code implementation:

/* * Bubble sort */public class Bubblesort {public static void main (string[] args) {int[] arr={6,3,8,2,9,1};    SYSTEM.OUT.PRINTLN ("Array before sorting is:");    for (int num:arr) {System.out.println (num); } for (int i=1;i<arr.length;i++) {//Outer loop control sort pass number for (int j=1;j<arr.length-i;j++) {//Inner loop controls how many times each trip is sorted 
      if (Arr[j-1]>arr[j]) {int temp=arr[j];          ARR[J]=ARR[J-1];        Arr[j-1]=temp;    }}} System.out.println ("Sorted array is:");    for (int num:arr) {System.out.println (num); }  }}

Bubble sort of classic algorithms in Java (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.