JavaSE 第三十五講 冒泡排序、交換排序及快速排序原理及實現(xiàn) 10.23 10.25

public class ArrayCopy
{
    public static void main(String[] args)
    {
        int[] a = new int[]{1, 2, 3, 4};
        int[] b = new int[]{4};

        System.arraycopy(a, 0, b, 0, 4);

        for(int i = 0; i < b.length; i++)
        {
            System.out.print(b[i] + " ");//可編譯,無法執(zhí)行??
        }
    }
}

2、

public class ThreeDimensionArrayTest
{
    public static void main(String[] args)
    {
        int[][][] a = new int[2][3][4];

        System.out.println(a instanceof int[][][]);
        System.out.println(a[0] instanceof int[][]);
        System.out.println(a[0][0] instanceof int[]);

        for(int i = 0; i < a.length; i++)
        {
            for (int j = 0; j < a[i].length; j++)
            {
                for(int k = 0; k < a[i][j].length; k++)
                {
                    a[i][j][k] = 100;
                }
            }
        }
    }
}

3、冒泡排序

public class BubbleSortTest
{
    public static void bubbleSort(int[] array)
    {
        for(int i = 0; i < array.length - 1; i++)
        {
            for(int j = 0; j < array.length - i - 1; j++)
            {
                if(array[j] > array[j + 1])
                {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }

            System.out.println("第" + (i + 1) + "趟排序");
            for(int k = 0; k < array.length; k++)
            {
                System.out.print(array[k] + " ");//第一趟排序
                                                 //4 7 3 2 8
                                                //第二趟排序
                                                //4 3 2 7 8
                                               //第三趟排序
                                              //3 2 4 7 8
                                             //第四趟排序
                                             //2 3 4 7 8
            }
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        int[] array = {4, 7, 8, 3, 2};

        bubbleSort(array);
    }

}

對比:

public class A
{
    public static void bubbleSort(int[] array)
    {
        for(int i = 0; i < array.length - 1; i++)
        {
            if(array[i] > array[i + 1])
            {
                int temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
            }

            System.out.print(array[i] + " ");//0143
        }

    }

    public static void main(String[] args)
    {
        int[] array = {1, 0, 4, 8, 3};
        
        bubbleSort(array);
    }
}

3、作業(yè):交換排序和快速排序的原理與實現(xiàn)方式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容