[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Edwardlinge/JavaArithmetic/master/src/sort/BubbleSort.java [Back]  [Original]

package sort;

/**
 * Created by ozc on 2018/3/14.
 *
 * @author ozc
 * @version 1.0
 */
public class BubbleSort {


    public void  bubble() {

        int[] arrays = {2, 5, 1, 3, 4};


        /*//
        int temp;

        //
        if (arrays[0] > arrays[1]) {
            //
            temp = arrays[0];
            arrays[0] = arrays[1];
            arrays[1] = temp;
        }

        //
        if (arrays[1] > arrays[2]) {
            temp = arrays[1];
            arrays[1] = arrays[2];
            arrays[2] = temp;
        }

        //
        if (arrays[2] > arrays[3]) {
            temp = arrays[2];
            arrays[2] = arrays[3];
            arrays[3] = temp;
        }

        //
        if (arrays[3] > arrays[4]) {
            temp = arrays[3];
            arrays[3] = arrays[4];
            arrays[4] = temp;
        }
        System.out.println(arrays);

        //----------------------------

        //
        if (arrays[0] > arrays[1]) {
            //
            temp = arrays[0];
            arrays[0] = arrays[1];
            arrays[1] = temp;
        }

        //
        if (arrays[1] > arrays[2]) {
            temp = arrays[1];
            arrays[1] = arrays[2];
            arrays[2] = temp;
        }

        //
        if (arrays[2] > arrays[3]) {
            temp = arrays[2];
            arrays[2] = arrays[3];
            arrays[3] = temp;
        }

        //

        System.out.println(arrays);
*/


        //
        int temp;

        // 0  1 
        int isChange;


        //
        int num = 0;


        //
        for (int i = 0; i < arrays.length -1 ; i++) {

            //0
            isChange = 0;

            //
            for (int j = 0; j < arrays.length - i - 1; j++) {

                //
                if (arrays[j] > arrays[j + 1]) {
                    temp = arrays[j];
                    arrays[j] = arrays[j + 1];
                    arrays[j + 1] = temp;


                    //
                    isChange = 1;

                }
            }
            //
            if (isChange == 0) {
                break;
            }
            num++;

        }

        System.out.println(num);
        System.out.println(arrays);

    }
}

Web Proxy Viewer  |  New URL  |  Original Page