Java Classic algorithm rollup bubble sort _java

Source: Internet
Author: User

Principle: Compare two adjacent elements to the right end of the element with a large value.

Train of thought: In turn, compare the adjacent two numbers, place the decimal number in front, the big numbers in the back. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two, the number of decimal places before the large number of put. Repeat the first step until all sorts are completed.

Example: to sort arrays: int[]arr={6,3,8,2,9,1};

First sort of trip:

First sort: 6 and 3 compared, 6 greater than 3, swap position: 368291

Second Order: 6 and 8 compared, 6 less than 8, no swap position: 368291

Third Order: 8 and 2 compared, 8 greater than 2, swap position: 362891

Fourth order: 8 and 9 comparisons, 8 less than 9, no swap position: 362891

Fifth Order: 9 and 1 comparison: 9 greater than 1, swap position: 362819

A total of 5 comparisons were made on the first trip, sorted results: 362819

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

The second sort of trip:

First sort: 3 and 6 compared, 3 less than 6, no swap position: 362819

Second Order: 6 and 2 compared, 6 greater than 2, swap position: 326819

Third Order: 6 and 8 compared, 6 greater than 8, no swap position: 326819

Fourth order: 8 and 1 comparison, 8 greater than 1, swap position: 326189

A total of 4 comparisons were made in the second trip, sorted results: 326189

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

The third sort of trip:

First sort: 3 and 2 compared, 3 greater than 2, swap position: 236189

Second Order: 3 and 6 compared, 3 less than 6, no swap position: 236189

Third Order: 6 and 1 compared, 6 greater than 1, swap position: 231689

A total of 3 comparisons were made in the second trip, sorted results: 231689

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

The four-trip sort:

First sort: 2 and 3 compared, 2 less than 3, no swap position: 231689

Second order: 3 and 1 compared, 3 greater than 1, swap position: 213689

A total of 2 comparisons were made in the second trip, sorted results: 213689

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

The five-trip sort:

First sort: 2 and 1 compared, 2 greater than 1, swap position: 123689

A total of 1 comparisons were made in the second trip, sorted results: 123689

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

Final Result: 123689

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

This shows: N number to sort complete, a total of N-1, the number of the ranking of I trip for (n-i) times, so you can use the double loop statement, the outer control loop how many times, the inner layer control every trip of the cycle, that

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

  (int j=1;j<arr.length-1-i;j++) {

  //swap location

}

The advantage of bubbling sort: each time a trip is sorted, it will be less than once, because each trip will find a larger value. As in the previous example: After the first comparison, the last number must be the largest number, and the second one, you just need to compare the numbers except the last number, and you can also find the largest number of rows after the number of the second comparison, and the third time, You just need to compare the numbers except the last two, and so on ... In other words, without a comparison, 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 positive order, just take a trip to complete the sorting. The required number of comparisons and the number of records moved to a minimum, namely: cmin=n-1; Mmin=0 So, the best time complexity for bubble sort is O (n).

2. If it is unfortunate that our data is reversed, then we need to do a n-1-order. Each trip 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 are maximized: The worst time complexity for bubble sort is: O (n2).

To sum up: the total average time complexity of 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 ("sorted before array:");
for (int num:arr) {
System.out.print (num+ "");
}
for (int i=1;i<arr.length;i++) {//Outer loop control sort number of trips for
(int j=1;j<arr.length-i;j++) {//Inner loop control 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 ();
System.out.println ("Sorted array is:");
 for (int num:arr) {
 System.out.print (num+ "");}}}
 

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.