java实现基本的排序算法

/**
 * 排序的算法
 * */
public class TestSort {

    public static void main(String[] args) {
        TestSort t = new TestSort();
        int[] array = new int[]{25, 341, 6, 121,1,99,4};
        t.binaryInsertSort(array);
    }


    //选择排序
    public void testChoice(int[] array) {
        int min = 0;
        for (int i = 0; i < array.length; i++) {
            min = array[i];
            for (int j = i; j < array.length; j++) {
                if (array[j] < min) {
                    min = array[j];
                    int temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    //直接插入排序
    public void testInsertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > temp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = temp;
        }

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    //二分插入排序
    public void binaryInsertSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int temp = array[i];    //待插入到前面有序序列的值

            int left = 0;
            int right = i - 1;
            int mid = 0;
            while (left <= right) {
                mid = (left + right) / 2;
                if (temp < array[mid]) {
                    right = mid - 1;
                } else if (temp > array[mid]) {
                    left = mid + 1;
                }
            }

            for (int j = i - 1; j >= left; j--) {
                //将left右边大的值往后移一位,等待temp的插入
                array[j + 1] = array[j];
            }


            if (left != i) {
                array[left] = temp;
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

    }

    //希尔排序
    public void testHeer(int[] array) {
        int d = array.length; //默认增量
        while (true) {
            d = d / 2;
            for (int i = 0; i < d; i++) {
                for (int j = i; j + d < array.length; j += d) {
                    for (int n = i; n + d < array.length; n += d) {   //根据长度 / 增量后 是每个组分配的数量 在每个组中进行排序
                        int temp;
                        if (array[n] > array[n + d]) {
                            temp = array[n];
                            array[n] = array[n + d];
                            array[n + d] = temp;
                        }
                    }
                }
            }
            if (d == 1)
                break;
        }
    }

    //大堆排序
    /**
     * 根节点从0开始
     * left = 2 * i + 1
     * right = 2 * i + 2
     * */
    public void heapSort(int[] array) {
        if (array == null || array.length <= 1)
            return;

        //创建大堆(父节点要比子结点大)
        buildMaxHeap(array);

        for(int i = array.length - 1;i >= 1;i--){
            //最大元素已经排在了下标为0的位置
            exchangeElements(array,0,i);//每交换一次就沉淀一个大元素
            maxHeap(array,i,0);
        }

        for(int a : array){
            System.out.print(a + " ");
        }
    }

    private void buildMaxHeap(int[] array) {
        int half = (array.length - 1) / 2;
        for(int i = half;i >= 0;i--){
            maxHeap(array,array.length,i);
        }

    }

    //length用于表示构造大堆的数组长度元素数量
    private void maxHeap(int[] array, int length, int i) {
        int left = i * 2 + 1;
        int right = i * 2 + 2;
        int largest = i;    //最大值
        if(left < length && array[left] > array[largest]){
            largest = left;
        }
        if(right < length && array[right] > array[largest]){
            largest = right;
        }

        if(i != largest){
            //进行数据交换
            exchangeElements(array,i,largest);
            maxHeap(array,length,largest);
        }

    }

    //在数组array中进行两个下标元素交换
    private void exchangeElements(int[] array, int i, int largest) {
        int temp = array[i];
        array[i] = array[largest];
        array[largest] = temp;
    }


    //快速排序
    public void testFastOrder(int[] array){
        if(array.length > 0){
            fastOrder(array,0,array.length - 1);
        }

        for (int a : array){
            System.out.print(a + " ");
        }
    }

    public void fastOrder(int[] array,int low,int height){
        if(low < height){
            int middle = getMiddle(array,low,height);
            fastOrder(array,0,middle-1);
            fastOrder(array,middle + 1,height);
        }
    }

    //获取中间下标
    public int getMiddle(int[] array,int low,int height){
        int temp = array[low];  //基准元素
        while(low < height){
            while(low < height && array[height] >= temp){
                height--;
            }
            array[low] = array[height];
            while(low < height && array[low] <= temp){
                low++;
            }
            array[height] = array[low];
        }
        array[low] = temp;
        return low;
    }

    //归并排序
    public void testMergeSort(int[] array){
        if (array.length > 0){
            mergeSort(array,0,array.length - 1);
        }

    }

    public void mergeSort(int[] array,int left,int right){
        if(left < right){
            int middle = (left + right) / 2;
            mergeSort(array,left,middle);
            mergeSort(array,middle + 1,right);
            merge(array,left,middle,right); //进行合并
        }
    }

    public void merge(int[] array,int left,int middle,int right){
        int[] tempArray = new int[array.length];
        int rightStart = middle + 1;
        int temp = left;
        int index = left;
        //比较两个小数组对应下标位置的大小,将小的值放入新的数组中
        while(left <= middle && rightStart <= right){
            if(array[left] <= array[rightStart]){
                tempArray[index++] = array[left];
                left++;
            }else if(array[left] > array[rightStart]){
                tempArray[index++] = array[rightStart];
                rightStart++;
            }

        }
        //如果左边还有数据,需要将剩下的数据拷贝到新的数组
        while(left <= middle){
            tempArray[index++] = array[left++];
        }
        //如果右边还有数据,需要将剩下的数据拷贝到新的数组
        while(rightStart <= right){
            tempArray[index++] = array[rightStart++];
        }

        while(temp <= right){
            array[temp] = tempArray[temp++];
        }
    }



    //基数排序
    public void testBasicSort(int[] array){
        int max = 0;//获取最大值
        for(int i =0;i < array.length;i++){
            if(max < array[i]){
                max = array[i];
            }
        }

        int times = 0;//判断要进行几次排序(获取最大值的位数)
        while(max > 0){
            max = max / 10;
            times++;
        }

        List<ArrayList> queue = new ArrayList<>();  //多维数组
        for(int i = 0;i < 10;i++){
            ArrayList q = new ArrayList();
            queue.add(q);
        }

        for(int i = 0;i < times;i++){
            for(int j = 0;j < array.length;j++){
                //获取对应的值(i为0是个位,1是十位,10是百位)
                int x = array[j] % (int)Math.pow(10,i + 1) / (int)Math.pow(10,i);
                ArrayList q = queue.get(x);
                q.add(array[j]);
                queue.set(x,q);
            }

            //开始收藏
            int count = 0;
            for(int j = 0;j < 10;j++){
                while(queue.get(j).size() > 0){
                    ArrayList<Integer> q = queue.get(j);//拿到每一个数组
                    array[count] = q.get(0);
                    q.remove(0);
                    count++;
                }
            }
        }
        for (int a : array){
            System.out.print(a + " ");
        }
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值