六大排序算法

package com.ds.exam;

public class test2 {

    /*直接插入排序*/
    public static void InsertSort(int A[],int n){
        int i,j;
        for (i=1;i<n;i++){
            int temp=A[i];
            for (j=i-1;j>=0;j--)
                if (temp<A[j])
                    A[j+1]=A[j];
                else break;

            A[j+1]=temp;
        }
    }
    /*快速排序*/
    public static void QuickSort(int A[],int low,int high){
        if (low<high){
            int mid=partition(A,low,high);
            QuickSort(A,low,mid-1);
            QuickSort(A,mid+1,high);
        }
    }
    private static int partition(int[] A, int low, int high) {
        while (low<high){
            int temp=A[low];
        while (high>low&&temp<=A[high]) high--;
        A[low]=A[high];
        while (low<high&&temp>=A[low]) low++;
        A[high]=A[low];
        A[low]=temp;
        }
        return low;
    }


    public static void main(String[] args) {
        int a[]={5,9,1,5,2,7};
        System.out.print("直接插入排序");
        InsertSort(a,a.length);
        print(a);
        /*---------------------------------------------*/
        int b[]={5,9,1,5,2,7};
        System.out.print("希尔排序");
        ShellSort(b);
        print(b);
        /*---------------------------------------------*/
        int c[]={5,9,1,5,2,7};
        System.out.print("快速排序");
        QuickSort(c,0,c.length-1);
        print(c);
        /*---------------------------------------------*/
        int d[]={5,9,1,5,2,7};
        System.out.print("冒泡排序");
        BulletSort(d);
        print(d);
        /*---------------------------------------------*/
        int e[]={5,9,1,5,2,7};
        System.out.print("选择排序");
        SelectSort(e);
        print(e);
        /*---------------------------------------------*/
        int f[]={5,9,1,5,2,7};
        System.out.print("二路归并排序");
        MergeSort(f,0,e.length-1);
        print(f);
        /*---------------------------------------------*/
    }

    private static void MergeSort(int[] f, int low, int high) {
        if (low<high){
            int mid=(low+high)/2;
            MergeSort(f,low,mid);
            MergeSort(f,mid+1,high);
            merge(f,low,mid,high);
        }
    }
    public static void merge(int a[],int low,int mid,int high){
        int b[]=new int[a.length];
        for (int q=0;q<a.length;q++)
            b[q]=a[q];

        int i=low,j=mid+1,k=low;
        while (i<=mid&&j<=high)
            if (b[i]<b[j])
                a[k++]=b[i++];
            else
                a[k++]=b[j++];

         while (i<=mid) a[k++]=b[i++];
         while (j<=high) a[k++]=b[j++];


    }

    private static void SelectSort(int[] e) {
        for (int i=0;i<e.length-1;i++){
            int min=i;
            for (int j=e.length-1;j>i;j--){
                if (e[j]<e[min])
                    min=j;
            }
            if (i!=min){
                int temp=e[i];
                e[i]=e[min];
                e[min]=temp;
            }
        }
    }

    /*冒泡排序*/
    private static void BulletSort(int[] d) {
        int n=d.length,i,j;
        for (i=0;i<n-1;i++)
            for (j=n-1;j>i;j--){
                if (d[j]<d[j-1]){
                    int temp=d[j];
                    d[j]=d[j-1];
                    d[j-1]=temp;
                }
            }
    }

    private static void ShellSort(int[] b) {
        int i,j,dk;
        for ( dk=b.length/2;dk>=1;dk=dk/2)
            for ( i=dk;i<b.length;i++){
                int temp=b[i];
                for ( j=i-dk;j>=0&&temp<b[j];j-=dk)
                    b[j+dk]=b[j];
                b[j+dk]=temp;
            }
    }

    public static void print(int a[]){
        for (int i=0;i<a.length;i++)
            System.out.print(a[i]+" ");
        System.out.println();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值