Java-based Bubble sorting and java-based Bubble Sorting

Source: Internet
Author: User

Java-based Bubble sorting and java-based Bubble Sorting

I. Bubble Sorting:

Sort arrays by bubble sort

Ii. Basic concepts:

Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). Before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.

Iii. Implementation ideas:

It is implemented using a double loop. The External Loop Variable is set to I and the inner loop variable is set to j. If there are n number needs to be sorted, then the outer loop repeats n-1 times, the inner loop repeats n-1, N-2,..., 1 times in turn. The two elements for each comparison are related to the inner loop j, which can be identified by a [j] And a [j + 1] respectively, the values of I are 1, 2 ,..., n-1. For each I, j values are 0, 1, 2 ,... n-I.

Set the array length to N:
1. Compare two adjacent data records. If the preceding data is greater than the subsequent data, the two data records are exchanged.
2. In this way, the first data of the array to the N-1 after a traversal, the largest data is "sink" to the array of the N-1 position.
3. N = N-1, if N is not 0, repeat the first two steps, otherwise the sorting is completed.

4. java code implementation:

Package Bubble Sorting; import java. util. arrays;/*** Bubble Sorting ** @ author mmz **/public class BubbleSort {public static void BubbleSort (int [] arr) {int temp; // define a temporary variable for (int I = 0; I <arr. length-1; I ++) {// Number of bubble values for (int j = 0; j <arr. length-i-1; j ++) {if (arr [j + 1] <arr [j]) {temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}}} public static void main (String [] args) {int arr [] = new int [] {, 5}; BubbleSort. bubbleSort (arr); System. out. println (Arrays. toString (arr ));}}

V. Performance analysis:

If the initial status of the record sequence is "positive", the Bubble sorting process only needs to sort the sequence. During the sorting process, only n-1 comparisons are required and records are not moved. Otherwise, if the initial status of the record sequence is "backward", You need to perform n (n-1)/2 comparisons and record movement. Therefore, the total time complexity of Bubble Sorting is O (n * n ).

6. Algorithm Optimization:

Limitations of the Bubble sorting method and improvement methods:
First, in the sorting process, although the data has been fully sorted after the final sorting, the program cannot determine whether to complete the sorting. To solve this problem, you can set a flag. The initial value is not 0, indicating that the table to be sorted is an unordered table. The flag value is set to 0 before each sorting, when data is exchanged, the flag is not 0. When a new round of sorting starts, check this flag. If this flag is 0, it indicates that data has not been exchanged in the previous round, and the sorting is terminated; otherwise, the sorting is performed;

Package Bubble Sorting; import java. util. arrays;/*** bubble sort version * @ author mmz **/public class BubbleSort1 {public static void BubbleSort (int [] arr) {boolean flag = true; while (flag) {int temp; // defines a temporary variable for (int I = 0; I <arr. length-1; I ++) {// Number of bubbles, n-1 for (int j = 0; j <arr. length-i-1; j ++) {if (arr [j + 1] <arr [j]) {temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp; flag = true ;}} if (! Flag) {break; // exit the loop if no switching occurs. }}} public static void main (String [] args) {int arr [] = new int [] {, 5}; BubbleSort. bubbleSort (arr); System. out. println (Arrays. toString (arr ));}}

Second, in Bubble sorting, A scan may have no data exchange, or one or more data exchanges, in the traditional Bubble Sorting Algorithm and some improved algorithms in recent years, only the information of data exchange is recorded for one scan, and the location information of data exchange is not processed. To make full use of this information, you can perform partial Bubble Sorting for each reverse data pair in a global scan, which is called partial Bubble sorting. The local Bubble sorting algorithm has the same time complexity as the Bubble Sorting Algorithm. In the case of positive and reverse order, the number of comparisons and moves of the required keywords are exactly the same. Because the data movement times of partial Bubble sorting and Bubble sorting are always the same, the number of comparisons of keywords required for partial Bubble Sorting is usually less than that of Bubble sorting, this means that the partial Bubble Sorting is likely to improve the average number of comparisons. The advantage of a relatively small number of comparisons is not enough to offset the additional overhead caused by the complexity of the program, when the data volume is large, the time performance of partial Bubble Sorting is significantly better than that of Bubble sorting. For N unordered data, when we perform a bubble sort, if the k Data and k + 1 data are in reverse order, then, sort the k + 1 data in a bubble sequence to move it to a proper position. That is to say, adjust the previous k + 1 data to a positive order. Because this method only bubbles the first k + 1 data, we call it local bubble.

I hope this article will help you with java programming.

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.