使用java排序的几种方式

<1>
归并排序的算法的关键也在于对递归的利用

//归并排序算法
public class mergSort {
    public static void mergeSort(int[] list)
    {
        if(list.length>1)
        {
            //归并排序上半部分
            int[] firstHalf=new int[list.length/2];
            System.arraycopy(list,0,firstHalf,0,list.length/2);
            mergeSort(firstHalf);
            //归并排序下半部分
            int secondHalfLength=list.length-list.length/2;
            int[] secondHalf=new int[secondHalfLength];          System.arraycopy(list,list.length/2,secondHalf,0,secondHalfLength);
            mergeSort(secondHalf);
            //归并排序上半部分和下半部分,此算法用来合并
            int[] temp=merge(firstHalf,secondHalf);
            System.arraycopy(temp,0,list,0, temp.length);
        }
    }

    private static int[] merge(int[] list1, int[] list2) {
        int[] temp=new int[list1.length+list2.length];
        //标志1
        int current1=0;
        //标志2
        int current2=0;
        //标志3
        int current3=0;
           while(current1if(list1[current1]else
            {
                temp[current3++]=list2[current2++];
            }
        }
        while(current1while(current2return temp;
    }
    public static void main(String[] args)
    {
        //测试数据
        int[] list={2,3,2,5,6,1,-2,3,14,12};
        mergeSort(list);
        for(int i=0;i<list.length;i++)
        {
            System.out.println(list[i]+" ");
        }
    }
}

<2>快速排序

这里写代码片

你可能感兴趣的:(算法)