Bubble sort--java Version __java

Source: Internet
Author: User
Directory: 1. Introduction: 2. Steps: 3, sorting effect: 4, Code display 5, the results show ———————————————————————————— 1. Introduction:

Bubble sort (Bubble sort, Taiwan translation: bubble sort or bubble sort) is a simple sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence. 2. Steps:

1, compare the adjacent elements.
2, if the first one is larger than the second one, swap them both until the maximum number appears at the end.
3, repeat "second step", each cycle minus a number 3, sorting effect:

4, Code display

Package COM.DATASTRUCTURE.SOLR;

Import Java.util.Arrays;

/**
 * Created by Yuhui on 2017/4/24.
 * *
 * bubble Sort
 */public
class A0_bubblesort {public

    static void Main (String ars[]) {
        int[] a={ 3,6,4,2,10,11,5};

        SYSTEM.OUT.PRINTLN ("Initial state:" +arrays.tostring (a));
        Int[] B = bubblesort (a);
        System.out.println ("Final state:" +arrays.tostring (b));

    }

    public static int[] Bubblesort (int[] a) {

        int tmep,num = 0;
        int I, J;
        for (i = a.length-1;i>0;i--) {for
            (j=0;j<i;j++) {
                if (A[j] > A[j+1]) {
                  TMEP = a[j];
                  A[J] = a[j+1];
                  A[J+1]=TMEP
              }  
            }
            System.out.println ("First" + (++num) + "trip Sort:" +arrays.tostring (a));
        }
        return A;
    }
}
5, the results show

Initial state: [3, 6, 4, 2, 10, 11, 5]
1th trip Sort: [3, 4, 2, 6, 10, 5, 11]
2nd trip Sort: [3, 2, 4, 6, 5, 10, 11]
3rd trip Sort: [2, 3, 4, 5, 6, 10, 11]
4th trip Sort: [2, 3, 4, 5, 6, 10, 11]
5th trip Sort: [2, 3, 4, 5, 6, 10, 11]
6th trip Sort: [2, 3, 4, 5, 6, 10, 11]
Final status: [2, 3, 4, 5, 6, 10, 11]

If you like the blog I wrote, after reading the harvest is very big, may wish to small sponsor me, let me have the power to continue to write High-quality blog, thank you for your appreciation ...

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.