Java排序查找

public class M1 {
    public static void main(String[] args) {
        int[] a=new int[]{8,2,1,6,3,9,5,0,4,7};
        shellSort(a);
        for(int i:a)System.out.printf("%d\t",i);
    }
    public static void bubbleSort1(int[] a)
    {
        for(int i=0,n=a.length;i<n-1;i--)
        {
            for(int j=0;j<n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    a[j]^=a[j+1];
                    a[j+1]^=a[j];
                    a[j]^=a[j+1];
                }
            }
        }
    }
    public static void bubbleSort2(int[] a)
    {
        int low = 0;
        int high= a.length-1;
        while(low<high)
        {
            for(int i=low ;i<high;i++)
            {
                if(a[i]>a[i+1])
                {
                    a[i]^=a[i+1];
                    a[i+1]^=a[i];
                    a[i]^=a[i+1];
                }
            }
            high--;
            for(int i=high;i>low;i--)
            {
                if(a[i-1]>a[i])
                {
                    a[i-1]=a[i];
                    a[i]=a[i-1];
                    a[i-1]=a[i];
                }
            }
            low++;
        }
    }
    public static void bubbleSort3(int[] a)
    {
        for(int i=0,n=a.length,exchange=1;i<n-1&&exchange==1;i++)
        {
            exchange=0;
            for(int j=0;j<n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    a[j]^=a[j+1];
                    a[j+1]^=a[j];
                    a[j]^=a[j+1];
                    exchange=1;
                }
            }
        }
    }
    public static void bubbleSort4(int[] a)
    {
        for(int i=0,n=a.length;i<n-1;i++)
        {
            boolean b=false;
            for(int j=0;j<n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    a[j]^=a[j+1];
                    a[j+1]^=a[j];
                    a[j]^=a[j+1];
                    b=true;
                }
            }
            if(!b)return;
        }
    }
    public static void bubbleSort5(int[] a)
    {
        for(int i=0,pos=0,n=a.length;i<n-1;i=n-pos)
        {
            pos=0;
            for(int j=0;j<n-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    a[j]^=a[j+1];
                    a[j+1]^=a[j];
                    a[j]^=a[j+1];
                    pos=j+1;
                }
            }
        }
    }
    public static void selectionSort(int[] a)
    {
        for(int i=0,n=a.length;i<n-1;i++)
        {
            int MIN=i;
            for(int j=i;j<n;j++)if(a[MIN]>a[j])MIN=j;
            if(i!=MIN)
            {
                a[MIN]^=a[i];
                a[i]^=a[MIN];
                a[MIN]^=a[i];
            }
        }
    }
    public static void insertionSort(int[] a)
    {
        for(int i=1,n=a.length;i<n;i++)
        {
            int j=i-1;
            int temp=a[i];
            for(;j>=0&&temp<a[j];j--)a[j+1]=a[j];
            a[j+1]=temp;
        }
    }
    public static void quickSort1(int[] a,int start,int end)
    {
        int i=start;
        int j=end;
        boolean b=true;
        if(i>=j)return;
        while(i<j)
        {
            if(a[i]>a[j])
            {
                a[i]^=a[j];
                a[j]^=a[i];
                a[i]^=a[j];
            }
            b=!b;
            if(!b)i++;
            else j--;
        }
        quickSort1(a,start,i-1);
        quickSort1(a,j+1,end);
    }
    public static void quickSort2(int[] a,int start,int end)
    {
        int i=start;
        int j=end;
        int temp=a[i];
        if(i>=j)return;
        while(i<j)
        {
            while(i<j&&a[j]>=temp)j--;
            a[i]=a[j];
            while(i<j&&a[i]<=temp)i++;
            a[j]=a[i];
        }
        a[i]=temp;
        quickSort2(a,start,i-1);
        quickSort2(a,j+1,end);
    }
    public static void shellSort(int[] a)
    {
        for(int n=a.length,r=n/2;r>=1;r/=2)
        {
            for(int i=r;i<n;i++)
            {
                int temp=a[i];
                int j=i-r;
                while(j>=0 && temp<a[j])
                {
                    a[j+r]=a[j];
                    j-=r;
                }
                a[j+r]=temp;
            }
        }
    }
    public static void binaryInsertionSort(int[] a)
    {
        //最初的第一个元素想象成已经排好的序列
        //遍历后面的元素,将其插入到已排序好的序列中
        for(int i=1;i<a.length;i++)
        {
            int low=0;//已经排序号的序列的最低位置
            int high=i-1;//已经排序号的序列的最高位置
            int temp=a[i];//每次将array[i]插入到以排序的序列中
            while(low<=high)//二分查找
            {
                int mid=(low+high)/2;
                if(a[mid]>temp)high=mid-1;
                else low=mid+1;
            }
            //找到插入点后,为该值腾出地方,将high+1到i的所有元素后移一位
            for(int j=i-1;j>high;j--)a[j+1]=a[j];
            a[high+1]=temp;
        }
    }
    public static void gnomesort(int[] a)
    {
        int i=0;
        while(i<a.length)
        {
            if(i==0||a[i-1]<=a[i])i++;
            else
            {
                int tmp = a[i];
                a[i] = a[i-1];
                a[--i] = tmp;
            }
        }
    }
    public static int binarySearch(int[] a,int x){
        int low=0;
        int high=a.length-1;
        while(low<=high){
            int mid=(low & high) + ((low ^ high)>>1);
            if(x<a[mid]){
                high=mid-1;
            }else if(x>a[mid]){
                low=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值