利用java實現插入排序、歸併排序、快排和堆排序

qq_1191691379發表於2020-12-06
package example;

import java.util.Arrays;

public class AllSort {
    //插入排序
    public static void insert_sort(int []data)
    {
        int N=data.length;
        for(int j=1;j<N;j++)
        {
            int key=data[j];
            int i=j-1;
            while(i>=0 && data[i]>key)
            {
                data[i+1]=data[i];
                i-=1;
            }
            data[i+1]=key;
        }
    }
    
 
    //歸併
    public static void Merge(int A[],int p,int q,int r)
    {
        int L[]=Arrays.copyOfRange(A,p,q+1);//注意to不包含
        int R[]=Arrays.copyOfRange(A,q+1,r+1);
        int i=0,j=0;
        for(int k=p;k<=r;k++)
        {
            if(i==L.length)
            {
                System.arraycopy(R,j,A,k,r-k+1);//需要指定長度
                break;
            }
            if(j==R.length)
            {
                System.arraycopy(L,i,A,k,r-k+1);
                break;
            }
            if(L[i]<=R[j])
            {
                A[k]=L[i];
                i+=1;
            }
            else
            {
                A[k]=R[j];
                j+=1;
            }
        }

    }

    //歸併排序(自頂向下)
    public static void Merge_Sort(int A[],int p,int r)
    {
        if(p<r)
        {
            int q=(p+r)/2;
            Merge_Sort(A,p,q);
            Merge_Sort(A,q+1,r);
            Merge(A,p,q,r);//呼叫Merge
        }
    }

    //歸併排序(自底向上)
    public static void Merge_Sort_down2top(int []A)
    {
        int width=1;
        int size=A.length;
        while(width<size){
            int index=0;
            while(index<size-width)
            {
                int low=index;
                int high=index+2*width-1;
                int mid=(low+high)/2;
                Merge(A,low,mid,high);//呼叫Merge
                index+=2*width;
            }
            width*=2;
        }
    }
    //

   
    public static int PARTITION(int A[],int p,int r)
    {
        int x=A[r];
        int i=p-1;
        int tmp=0;
        for(int j=p;j<r;j++)
        {
            if(A[j]<=x)
            {
                i+=1;
                tmp=A[i];
                A[i]=A[j];
                A[j]=tmp;
            }
        }
        tmp=A[i+1];//再次使用tmp
        A[i+1]=A[r];
        A[r]=tmp;
        return i+1;
    }

    //快排
    public static void QuickSort(int A[],int p,int r)
    {
        if(p<r)
        {
            int q=PARTITION(A,p,r);
            QuickSort(A,p,q-1);
            QuickSort(A,q+1,r);
        }
    }

    /

    static int heap_size=0;

    public static int left(int i)
    {
        return 2*i+1;
    }

    public static int right(int i)
    {
        return 2*i+2;
    }
    
    //最大堆調整
    public static void max_heapify(int A[],int i)
    {
        int l=left(i);
        int r=right(i);
        int largest=0;
        if(l<=heap_size-1 && A[l]>A[i])
        {
            largest=l;
        }
        else{
            largest=i;
        }
        if(r<=heap_size-1 && A[r]>A[largest])
        {
            largest=r;
        }
        if(largest!=i)
        {
            int tmp=A[i];
            A[i]=A[largest];
            A[largest]=tmp;
            max_heapify(A,largest);
        }
    }
    
    //構建最大堆
    public static void build_max_heap(int A[])
    {
        for(int i=A.length/2-1;i>=0;i--)
        {
            max_heapify(A,i);
        }
    }

    //堆排序
    public static void heapsort(int A[])
    {
        build_max_heap(A);
        for(int j=A.length-1;j>0;j--)
        {
            int tmp=A[0];
            A[0]=A[j];
            A[j]=tmp;
            heap_size-=1;
            max_heapify(A,0);
        }
    }

    

    public static void main(String[]args)
    {
        int []data={1,5,2,7,6,3,9,10};
        //Merge_Sort(data,0,data.length-1);
        //Merge_Sort_down2top(data);
        //insert_sort(data);
        //QuickSort(data,0,data.length-1);

        heap_size=data.length;//賦值堆大小(全域性變數)
        heapsort(data);

        for(int e:data){
            System.out.print(e+" ");
        }
    }
}




相關文章