七大排序学习

import sun.security.x509.IPAddressName;

import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:user
 * DATE:2021-11-14
 * Time:14:31
 */
/*
直接插入排序
希尔排序
选择排序
堆排序
冒泡排序
选择排序
归并排序
 */
public class Test {

    //直接插入排序
    //每次从无序中选取放入到有序中,最终是整个数组有序
    //最坏时间复杂度为O(N^2)  最好时间复杂度为O(N)
    //直接插入排序越有序越快
    //空间复杂度为O(1)
    //稳定的排序 若添加array[j] >= tmp 则为不稳定的排序
    //稳定的排序可以实现为不稳定的排序,一个不稳定的排序是不可能成为稳定的排序的
    //如何快速判断一个排序是由稳定,就是看是否发生跳跃式交换 array[j+1] = array[j];  array[j+5] = array[j];
    //两个相同的数字 排序前合排序后的相对位置
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for ( ;j >= 0 ; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }



    //希尔排序采用分组的思想 对每组数据都进行直接插入排序,缩小增量排序
    //直接插入排序越有序越快
    //希尔排序的时间复杂度为O(N^1.3 --- N^1.5)
    //空间复杂度: 0(1)
    //不稳定
    public  static  void shellSort(int[] array){
        int[]  gap = {5,3,1};//增量序列 不好求 有10000万个数据也是 5 3 1吗
        for (int i = 0; i < gap.length; i++) {
            shell(array,gap[i]);
        }
    }
    public static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for ( ;j >= 0 ; j -= gap) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    //时间复杂度为O(n^2)
    //空间复杂度为O(1)
    //稳定性:不稳定的排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[j]  < array[i]){
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    //时间复杂度:O(n*logn)
    //空间复杂度:O(1)
    //不稳定
    public static void heapSort(int[] array){
        //建立大根堆
        //最后一个元素合第一个元素进行交换’
        //向下调整第一个元素
        createHeap(array);
        int end = array.length - 1;
        while (end > 0){
            int tmp = array[0];
            array[0] = array[end];
            array[end] = tmp;
            shiftDown(array,0,end);
            end--;
        }
    }

    public static  void createHeap(int[] array){
        //建立大根堆 每次向下调整 建堆的时间复杂度是o(N)
        for (int parent = (array.length - 1 - 1) / 2 ; parent >= 0 ; parent--) {
            shiftDown(array,parent,array.length - 1);
        }
    }
    public  static  void shiftDown(int[] array,int parent, int len){
        int child = 2 * parent + 1;
        while (child < len){
            if (child + 1 < len && array[child] < array[child+1]){
                child++;
            }
            if(array[child] > array[parent]){
                int tmp = array[child];
                array[child] = array[parent];
                array[parent] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }


    //时间复杂度:O(N^2)
    //空间复杂度:O(1)
    //稳定的排序
    public  static  void bubbleSort(int[] array){
        for (int i = 0; i < array.length  - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = false;
                }
            }
            if (flag == true){
                break;
            }
        }
    }


    //快排挖坑法
    //O(NlogN)
    //最好情况O(NlogN)每次能够均匀的分割待排序序列
    //最坏情况O(N^2)有序
    //O(logN) -- O(N)
    //不稳定的排序
    public  static  void quickSort(int[] array){
        quick(array,0,array.length - 1);
    }
    public  static  void quick(int[] array,int low,int high){
        if(low >= high)return;

        //若为有序的时候,递归次数再多,把栈挤爆
        //需要进行优化,使得待排序的序列每次趋于左右平衡划分
        //所有的优化改进都是在做一件事情:尽大量的去均匀分割待排序序列
        //三数取中法
        //array[mid] <= array[left] <= array[right]
        //left mid right 之间选取中间大小的数字
        //4 1 7
        //4 7 1
        //注意:中间位置的数字不一定就是三个位置的中间值
        //固定位置选取基准
        //随机选取基准法

        //越来越趋于有序
        //插入排序
        if((high - low + 1) < 48){
            insertSort2(array,low,high);
            return;
        }

        //三数取中法
        mid_three(array,low,high);
        //若不采用这种优化会导致栈溢出

        int pivot = partition(array,low,high);
        quick(array,low,pivot - 1);
        quick(array,pivot + 1 ,high);
    }
    public  static  void insertSort2(int[] array,int low,int high){
        for (int i = low+1; i <= high; i++) {
            int tmp = array[i];
            int j = i - 1;
            for ( ;j >= low; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public  static  void mid_three(int[] array,int left,int right){
        //array[mid] <= array[left] <= array[right]
        int mid = (left  + right) / 2;
        if(array[mid]  > array[left]){
            swap(array,mid,left);
        }
        if(array[mid]  > array[right]){
            swap(array,mid,right);
        }
        if(array[left] > array[right]){
            swap(array,left,right);
        }
    }

    public static  void swap(int[] array,int left,int right){
        int tmp = array[left];
       array[left] = array[right];
       array[right] = tmp;
    }
    public  static int partition(int[] array,int start ,int end){

        int tmp = array[start];
        while (start < end){
            while (start < end && array[end] >= tmp){
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= tmp){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = tmp;
        return  start;
    }

    //借助栈是实现快排的非递归
    //分割一次 左边右边的元素入栈进行处理
    public  static  void quickSort2(int[] array){
       Stack<Integer> stack = new Stack<>();
       int low = 0;
       int high = array.length - 1;
       int pivot = partition(array,low,high);
       if(pivot > low + 1){
           stack.push(low);
           stack.push(pivot - 1);
       }
       if(pivot < high - 1){
           stack.push(pivot+1);
           stack.push(high);
       }
       while (!stack.isEmpty()){
           high = stack.pop();
           low = stack.pop();
           pivot = partition(array,low,high);
           if(pivot > low + 1){
               stack.push(low);
               stack.push(pivot - 1);
           }
           if(pivot < high - 1){
               stack.push(pivot+1);
               stack.push(high);
           }
       }
    }

    //归并排序
    //分解 合并

    //合并 相当于合并两个有序数组
    public static  void merge(int[] array,int low,int mid, int higt){
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = higt;
        int[] tmpArr = new int[higt - low + 1];
        int k = 0;
        while (s1 <= e1 && s2 <= e2){
            //不加等于号就是不稳定的排序
            if(array[s1] <= array[s2]){
                tmpArr[k++] = array[s1++];
               /* k++;
                s1++;*/
            }else{
                tmpArr[k++] = array[s2++];
              /*  k++;
                s2++;*/
            }
        }
        while (s1 <= e1){
            tmpArr[k++]  = array[s1++];
        }

        while (s2 <= e2){
            tmpArr[k++]  = array[s2++];
        }
        //写回数据到原来的数组
        for (int i = 0; i < k; i++) {
            array[i+low] = tmpArr[i];
        }
    }

    //分解
    public  static  void mergeSortIn(int[] array,int low,int high){
        if(low >= high)return;
        int mid = (low + high) / 2;
        mergeSortIn(array,low,mid);
        mergeSortIn(array,mid + 1,high);
        merge(array,low,mid,high);//换句话说就是合并两个有序数组
    }

    //时间复杂度 O(N * log2N)
    //时间复杂度为O(n)
    //稳定的排序
    public  static  void mergeSort(int[] array){
        mergeSortIn(array,0,array.length - 1);
    }

    public  static  void mergeSortNor(int[] array){
       int gap = 1;
       while (gap < array.length){
           for (int i = 0; i < array.length; i = i + gap * 2) {
               int low = i;
               int mid = low + gap - 1;
               int high = mid + gap;
               if(mid >= array.length){
                   mid = array.length - 1;
               }
               if(high >= array.length){
                   high = array.length - 1;
               }
               merge(array,low,mid,high);
           }
           gap  = gap * 2;
       }
    }



    public static void main1(String[] args) {
        int[] array = new int[10_0000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            //array[i] = random.nextInt(10_0000);
            array[i] = i;
        }
        long start = System.currentTimeMillis();
        quickSort(array);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void main(String[] args) {
        int[] array = {5,4,3,2,1};
        //insertSort(array);
        //shellSort(array);
        //selectSort(array);
        //heapSort(array);
        //bubbleSort(array);
        //quickSort(array);
        //mergeSort(array);
        mergeSortNor(array);
        System.out.println(Arrays.toString(array));

    }

    //内部排序:内存上进行排序
    //外部排序:磁盘上进行 大量的数据

    //基数排序
    //按个位数字进行放入对应的位置
    //出的时候按照队列的方式进行
    //按十位数字放入对应位置
    //出的时候按照队列的方式进行
    //按百位数字入位置
    //出的时候按照队列的方式进行


    //计数排序
    //一个表示原数据数组
    //一个表示存放计数个数


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值