常见排序java实现

public class Sort
{

    public static void main(String[] args) 
    {
        int[] data = {49,38,65,97,76,13,27,49};
        int[] res = data;
        final int N = data.length;
        bubbleSort(data,N);
        printData(data,N);

        directInsertSort(data,N);
        printData(data,N);

        binaryInsertSort(data,N);
        printData(data,N);

        shellSort(  data,N);
        printData(data,N);

        quickSortWithRecursion(data,N);
        printData(data,N);

        mergeSort(data,N);
        printData(data,N);

        heapSort(data,N);
        printData(data,N);
    }

    public static void printData(int[] data, int n) 
    {
        for (int e :data ) {
            System.out.print(e+"\t");
        }
        System.out.println();
        System.out.println();
    }
    public static void bubbleSort(int[] data, int n)
    {
        System.out.print("改进冒泡排序:\t");
        int flag = n-1;
        int k = n-1;
        int temp;

        while(flag>0)
        {
            k = flag;
            flag = 0;
            for(int j=0;j<k;j++)
            {
                if(data[j]>data[j+1])
                {
                    temp = data[j];
                    data[j] = data[j+1];
                    data[j+1] = temp;
                    flag = j+1;
                }
            }
        }

    }

    public static void directInsertSort(int[] data, int n)
    {
        System.out.print("直接插入排序:\t");
        int temp;
        int i;
        int j;
        for(i=1;i<n;i++)
        {
            temp = data[i];

            for(j=i-1;j>=0 && data[j]>temp;--j)
            {
                data[j+1]  =data[j];
            }
            data[j+1] = temp;
        }
    }


    public static void binaryInsertSort(int[] data, int n)  
    {
        System.out.print("折半插入排序:\t");
        int i,j,temp;
        int location;
        int l,h,m;
        for(i=1;i<n;i++)
        {
            temp = data[i];

            l = 0;
            h = i-1;
            while(l<=h)
            {
                m = (l+h)/2;
                if(temp==m) location = m+1;
                else if (temp<m) {
                    h = m-1;    
                }
                else l = m+1;
            }
            location = l;

            for(j=i-1;j>=location;--j)
            {
                data[j+1] = data[j];
            }
            data[location] = temp;
        }
    }

    public static void shellSort(int[] data, int n) 
    {
        System.out.print("希尔排序:\t\t");
        int i,j,temp;
        for(int gap=n/2;gap>0;gap/=2)
        for(i=gap;i<n;i+=gap)
        {
            temp = data[i];
            for(j=i-gap;j>=0&&data[j]>temp;j-=gap)
            {
                data[j+gap] = data[j];
            }
            data[j+gap] = temp;
        }
    }

    public static void quickSortWithRecursion(int[] data, int n)
    {
        System.out.print("递归快速排序:\t");
        quickSortWithRecursion1(data,0,n-1);
    }

    public  static void quickSortWithRecursion1(int[] data, int low, int high)
    {
        if(low>=high) return;  //这句很重要,否则会栈溢出
        int location = locate(data,low,high);
        quickSortWithRecursion1(data,low,location-1);
        quickSortWithRecursion1(data,location+1,high);
    }

    public static int locate(int[] data, int low, int high)
    {
        int temp = data[low];
        while(low<high)
        {
            while(low<high && data[high]>temp) --high;
            if(low<high) data[low] = data[high];

            while(low<high && data[low]<=temp) ++low;
            if(low<high) data[high]=data[low];
        }
        data[low] = temp;
        return low;
    }


    public static void mergeSort(int[] data, int n)
    {
        System.out.print("归并排序:\t\t");
        mergeSortImpl(data,0,n-1);
    }
    public static void mergeSortImpl(int[] data, int low, int high)
    {
        if(low>=high) return;
        int mid = (low+high)/2;
        int k = -1;
        int index;
        mergeSortImpl(data,low,mid);
        mergeSortImpl(data,mid+1,high);
        if(data[mid]<=data[mid+1]) return;  //等号关键

        int[] temp = new int[high-low+1];
        int i=low,j=mid+1;
        while(i<=low && j<=high)
        {
            temp[++k] = max(data[i],data[j]);
        }
        while(i<=low) temp[++k] = data[i];
        while(j<=high) temp[++k] = data[j];
        for(index=0;index<=k;++index)
        {
            data[low+index] = temp[index];
        }
    }
    public static int max(int a, int b)
    {
        return a>b?a:b;
    }

    public static void heapSort(int[] data, int n)
    {
        System.out.print("堆排序:\t\t\t");
        int[] arr = new int[n+1];
        System.arraycopy(data,0,arr,1,n);
        heapSortImpl(arr,1,n);
        System.arraycopy( arr,1,data,0,n);
    }

    public static void heapSortImpl(int[] data, int low, int high)
    {
        int temp;
        for(int i=high/2;i>=1;--i)
        {
            adjust(data,i,high);
        }
        for(int i=high;i>=2;--i)
        {
            temp = data[1];
            data[1] = data[i];
            data[i] = temp;
            adjust(data,1,i-1);
        }
    }

    public static void adjust(int[] data, int low, int high)
    {
        int i=low;
        int j=low*2;
    /*  int temp=data[i];
        while(j<=high)
        {
            if(j<high && data[j]<data[j+1]) ++j;
            if(temp<data[j]) 
            {
                data[i] = data[j];
                i = j;
                j = 2*i;
            }
            else 
                break;
        }
        data[i] = temp;*/
        int temp;

        while(j<=high)
        {
            if(j<high && data[j]<data[j+1]) ++j;  //j<high不能取等号

            if(data[i]<data[j])
            {
                temp = data[i];
                data[i] = data[j];
                data[j] = temp;
                i = j;
                j = i*2;
            }
            else break;
        }

    }
}

posted @ 2016-07-21 19:22  岳麓丹枫  阅读(119)  评论(0编辑  收藏  举报