• 数组和排序算法(冒泡、选择、插入排序)


    1)冒泡排序

    public static int[] bubbleSort(int[]a)
        {
            int temp;
            for(int i=0;i<a.length-1;i++)
            {
                for(int j=0;j<a.length-1-i;j++)
                {
                    if(a[j]>a[j+1])
                    {
                        temp=a[j];
                        a[j]=a[j+1];
                        a[j+1]=temp;
                    }
                    
                }
                System.out.println(Arrays.toString(a));
            }
            return a;
        }
    public static int[] bubbleSort2(int[] a)
        {
            boolean bool = true;
            while(bool)
            {
                bool=false;//当下面代码没执行就说明,排序都排好了,就不用再循环了
                for(int i = 0;i<a.length-1;i++)
                {
                    if(a[i]>a[i+1])
                    {
                        int temp = a[i];
                        a[i] = a[i+1];
                        a[i+1]=temp;
                        bool=true;
                    }
                }
                System.out.println(Arrays.toString(a));
            }
            return a;
        }

    2)选择排序

    public static void selectSort(int[] a)
        {
            int length = a.length;
            for(int i=0;i<length-1;i++)
            {
                int min=i;
                for(int j=i+1;j<length;j++)
                {
                    if(a[min]>a[j])
                    {
                        min=j;
                    }
                }
                if(min!=i)
                {
                    int temp = a[min];
                    a[min]=a[i];
                    a[i]=temp;
                }
                System.out.println(Arrays.toString(a));
            }
        }

     3)插入排序

        public static int[] insertSort(int[] a )
        {
            if(a == null || a.length < 2)return a;
            int length = a.length;
            for(int i = 1; i < length; i++)
            {
                for(int j = i;j > 0; j--)
                {
                    if(a[j]<a[j-1])
                    {
                        int temp = a[j];
                        a[j] = a[j-1];
                        a[j-1] = temp;
                    }
                    else break;
                }
                System.out.println(Arrays.toString(a));
            }
            return a;
        }
    public static int[] insertSort(int[] a )
        {
            if(a==null||a.length<2) return a;
            int length=a.length;
            for(int i=1;i<length;i++)
            {
                int temp=a[i];
                int index;
                for(int j=i;j>0;j--)
                {
                    if(a[j]<a[j-1])
                    {
                        a[j]=a[j-1];
                        index=j-1;
                    }
                    else break;
                    a[index]=temp;
                }
                System.out.println(Arrays.toString(a));
            }
            return a;
        }
  • 相关阅读:
    实用函数,array_column。从二维数组中获取想要的一位数组。
    解决小程序swiper层级太高问题
    小程序模拟领红包
    小程序,红包弹出层布局
    小程序核销功能
    小程序 text标签中一定 不要换行,不要随便格式化!!!
    小程序动态修改json中的配置
    小程序支付
    docker常用命令
    ssh修改默认端口
  • 原文地址:https://www.cnblogs.com/lhh666/p/11579133.html
Copyright © 2020-2023  润新知