排序

一 插入排序

时间复杂度 O(n^2)

空间复杂度O(1)

稳定性:稳定

//插入排序
    public static void inSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int tmp=arr[i];
            int j = i+1;
            for (; j >=0 ; j--) {
                if (arr[j]<tmp){
                    arr[j+1]=arr[j];
                }else {
                    break;
                }
            }
            arr[j+1]=tmp;

        }
    }

 

二 希尔排序

时间复杂度 O(n^1.3  --- n^1.5)

空间复杂度O(1)

稳定性:不稳定

 public static void shell(int[]arr,int gap){
        for (int i = 0; i < arr.length ; i++) {
            int j=i-gap;
            int tmp=arr[i];
            for (; j >=0 ; j-=gap) {
                if (tmp<arr[j]){
                    arr[j+gap]=arr[j];
                }else {
                   break;
                }
            }
            arr[j+ gap]=tmp;
        }
    }

    public static void shellSort(int[]arr){
        int gap= arr.length;
        if (gap>1){
            shell(arr,gap);
            gap/=2;
        }
        shell(arr,1);
    }

 

三 选择排序(两种方法)

时间复杂度 O(n^2)

空间复杂度O(1)

稳定性:不稳定

 

 //选择排序
    public static void choice(int[] arr){
        for (int i = 0; i < arr.length ; i++) {
            int j =i+1;
            for (; j < arr.length; j++) {
                if (arr[i]>arr[j]){
                    int tmp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=tmp;
                }
            }
        }

    }
    public static void choice1(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int minindex=0;
            for (int j = 0; j < arr.length ; j++) {
                if (arr[j]<arr[minindex]){
                    minindex=j;
                }

            }
            swap(i,minindex,arr); //交换
        }
    }

 四 堆排序

时间复杂度 O(n*logn)

空间复杂度O(1)

稳定性:不稳定

/堆排序
    public static void haepSort(int[] arr){
        //建堆
        createHeap(arr);
        int end= arr.length-1;
        //交换然后调整
        while (end>0){
            swap(0,end,arr);
            shiftDown(arr,0,end);
            end--;
        }
    }
    public static void createHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }

    public static void shiftDown(int[] arr,int parent,int len){
        int child=2*parent+1; //左孩子下标
        while (child<len){
            if (child+1<len && arr[child]<arr[child+1]){
                child++; //child下标是左右孩子最大值的下标
            }
            if (arr[child]>arr[parent]){
                swap(child,parent,arr);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }

    public static void swap(int i,int j,int[]arr){
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }

五 冒泡排序

时间复杂度 O(n*logn)

空间复杂度O(1)

稳定性:稳定 

 public static void bublSort(int[] arr){
for
(int i = 0; i < arr.length-1 ; i++) { boolean tmp =false; for (int j = 0; j < arr.length-1-i ; j++) { if (arr[j+1]<arr[j]){ swap(j,j+1,arr); tmp=true; } } if (tmp=false){ break; } }
}

 

六 快速排序

时间复杂度 最好 O(n*logn)  最坏 O(n^2)

空间复杂度 最好O(logn)   最坏 O(n)

稳定性:不稳定  

 

 

 public static void quickSort(int[] arr){
        quick(arr,0, arr.length-1);
    }
    public static void quick(int[] arr,int left,int right){
        if (left>=right){
            return;
        }
        int pivot =partition(arr, left, right);
        quick(arr,left,pivot-1);
        quick(arr,pivot+1,right);
    }
    public static int partition(int[] arr,int left,int right){
        int tmp=arr[left];
        while (left<right){
            while (left<right && tmp<=arr[right]){
                right--;
            }
            arr[left]=arr[right];
            while ((left<right && tmp>= arr[left])){
                left++;
            }
            arr[right]=arr[left];
        }
        arr[left]=tmp;
        return tmp;

    }

 

 

7 归并排序 (将数组分分成一份一份 然后以2*gap(数组长度)合并排序 最后合成一个有序数组)

时间复杂度 O(n*logn)
空间复杂度 O(n)
稳定性 稳定()

递归方法

  public static void mergeSort(int[] arr){
        mergeSortInter(arr,0, arr.length-1);
    }
    public static void mergeSortInter(int[] arr,int low,int high){
        if (low>=high){
            return;
        }
        int mid=low+((high-low)>>>1);
        //左边
        mergeSortInter(arr,low,mid);
        //右边
        mergeSortInter(arr,mid+1,high);
        //合并
        merge(arr,low,mid,high);

    }
    //合并(重要)
    public static void merge(int[] arr,int left,int mid,int right){
        int s1=left;
        int s2=mid+1;
        int e1= mid;
        int e2= right;
        int [] tmp=new int[right-left+1];
        int k = 0;
        while (s1<=e1 && s2<=e2){
            if (arr[s1]<=arr[s2]){
                tmp[k++]=arr[s1++];
            }else {
                tmp[k++]=arr[s2++];
            }
        }
        while (s1<=e1){
            tmp[k++]=arr[s1++];
        }
        while (s2<=e2){
            tmp[k++]=arr[s2++];
        }
        for (int i = 0; i < k; i++) {
            arr[left+i]=tmp[i];
        }
    
    }

 

 非递归方法

 //非递归方法
    public static void megre(int[] arr,int low,int high){
        int num=1;
        while (num< arr.length){
            for (int i = 0; i < arr.length ; i+=2*num) {
                int left=i;
                int mid=left+num-1;
                if (mid>=arr.length){//防止越界
                    mid= arr.length-1;
                }
                int right=mid+num;
                if (right>= arr.length){//防止越界
                    right= arr.length-1;
                }

                merge(arr,left,mid,right);

            }
            num*=2;
        }

    }

 

 

 八 计数排序

 

 public static void countingSort(int[] arr){
        int min=0;
        int max=0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]>arr[min]){
                arr[min]=arr[i];
            }
            if (arr[i]>arr[max]){
                arr[max]=arr[i];
            }
        }
        //找到了最大值和最下值的小标,防止空间不合理使用,如932到950
        int[] ret=new int[max-min+1];
        //开始遍历将arr数组出现数字的次数计算到ret上
        for (int i = 0; i <arr.length ; i++) {
            int index=arr[i];
            ret[index-min]++;
        }
        //将ret里的数字遍历,打印一次就次数减一次,指针++
        int indexArray=0;
        for (int i = 0; i <ret.length ; i++) {
            arr[indexArray]=i+min;
            ret[i]--;
            indexArray++;
        }

 

  

总结:排序方法中只有3个是稳定的:冒泡排序,插入排序,归并排序。

 

posted @ 2022-09-07 13:23  possiblely  阅读(68)  评论(0)    收藏  举报