Bubble sort, bubble performance optimization--java implementation

Source: Internet
Author: User

Bubble Sort Description

Compare two elements at a time and swap them out if they are in the wrong order.

Repeat until no more swapping is needed, that is, the sorting is complete.

The smaller elements will slowly "float" through the exchange to the top of the sequence.

The bubbling Sorting algorithm works as follows:
    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
A simple bubble sort code:
Importjava.util.Arrays;/*** Achieve bubble sort *@authorDell **/ Public classBubblesort { Public Static voidMain (string[] args) {int[] arr = {1,200,19,55,10,22,3,7}; //API ImplementationArrays.sort (arr);        System.out.println (arrays.tostring (arr)); //Bubbling Implementation        inttemp;  for(intI =0;i<arr.length-1;i++){             for(intj = 0;j<arr.length-1-i;j++){                if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp;    }}} System.out.println (Arrays.tostring (arr)); }}
Performance analysis and algorithm optimization for bubble sequencing (Outer loop optimization) issues:

Some bubbles after the first round of exchange is already orderly, such as: 2 1 3 4. The more data you have, the slower it is, and it's very unsuitable for big data sorting.

Solutions

If a flag is used to determine if the current array is ordered, exiting the loop in an orderly manner can significantly improve the performance of the bubbling sort.

 PackageBubblesort;Importjava.util.Arrays;Importorg.junit.Test;/*** Bubble sequencing performance analysis and algorithm optimization (Outer loop optimization) *@authorDell **/ Public classBubbeSort02 {@Test Public voidtest1 () {BooleanFlag =true; int[] arr = {2,1,3,4,5}; inttemp;  for(inti = 0; i < arr.length-1; i++) {             for(intj = 0; J < Arr.length-1-i; J + +) {                if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; Flag=true; }            }            if(!flag) {                //exits the loop without an exchange;                 Break;    }} System.out.println (Arrays.tostring (arr)); }}
Bubble sort second optimization (Inner loop optimization)
    /*** Bubble sequencing performance analysis and algorithm optimization (inner loop optimization)*/@Test Public voidtest2 () {int[] arr = {22,1,10,5}; //mark the location of the last interchange                         for(inti = 0; i < arr.length-1; i++) {            intFlag = 0; inttemp;  for(intj = 0; J < Arr.length-i-1; J + +) {                if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; //when the position changes, the value of flag changesFlag=1; }            }            //determine if flag flags have changed without directly ending the inner loop            if(flag==0){                return;    }} System.out.println (Arrays.tostring (arr)); }}

Reference links

http://blog.csdn.net/pzhtpf/article/details/7560294

Http://www.jianshu.com/p/ffcf5f97bc36

Bubble sort, bubble performance optimization--java implementation

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.