Bubble Sort Optimization Java

Source: Internet
Author: User

In this paper, the traditional bubble sort is optimized and the number of cycles is reduced. Time complexity if the initial state of the file is a positive sequence, a scan can be done in order. The required number of keyword comparisons C and the number of record moves m have reached the minimum: C (min) =n-1, m (min) = 0. So, the best time complexity for bubble sorting is O (n); If the initial file is reversed, a sequencing is required. The Sub-keyword comparison (1≤i≤n-1) is performed for each order, 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 (n^2). Overall, the average time complexity for the bubble sort is O (n^2). The algorithm's stability bubble sort is to move the small element forward or the large element to be adjusted backwards. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.
 1/** 2 * Bubble sort JAVA 3 * Optimized bubble sort 4 * Add a issort whether the round is sorted and if not sorted then the sort has ended directly exit 5 * @author Administrator 6 * @time 20160731 7 */8 public abstract class Bubblingsort {9////////////////start bubbling sort ten public static int[] sort (i Nt[] Array) Each {A Boolean issort=false;//determines whether or not to sort, defaults to not being sorted, and if not sorted, ends with a direct (int i = 0; i < a Rray.length-1;                 i++) {issort=false;//the sort identifier before each round of sorting false15 for (int j = 0; J < Array.length-i-1; J + +) {16                     if (array[j]>array[j+1]) + {=array[j+1];19 int temp                 Array[j+1]=array[j];20 array[j]=temp;21 issort=true;//If True then this round is exchanged, continue the next round 22         }23}24 if (!issort) break;//If this round has not been cycled, the result is false jump directly, do not proceed to the next round of retrieval 25}26    return array; return array27}28///////////end bubble Sort} 

I added a judge in the inside whether the sorting is finished, if the sort has been completed, then immediately end the sort, do not continue to cycle detection!

The method I called in another class:

1 public class Arithmetic_testclass {2 public     static void Main (string[] args) {3         //test array int type 4         int array[]= {4,9,5,3,7,6,8,3,2,1};     5  6         //bubble sort optimized over bubble sort 7         array=bubblingsort.sort (array); 8          9 for         (int i:array) {Ten             System.out.print (i+ "  ");         }12     }14}

The results of the implementation are as follows:

1 2 3 3 4 5 6 7 8 9

Successful implementation!

Bubble Sort Optimization Java

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.