zoukankan      html  css  js  c++  java
  • 快速排序,归并排序

      可以看出,两种排序都采用分治的方法。将一个大的数组逐渐分成小的更小的数组,然后进行排序。

    不同的是,归并排序采用由小到大的策略,把两个小的数组排序合并得出一个新的排好的数组,逐渐合并一个完整的数组。

    快速排序则是由大到小,在数组内部找到一个标量,左小右大分隔开,一直把每个数都这样操作,也就排序完了整个数组。

    归并排序:

    public class mergeSortStandard{
        
        public static void main(String[] arg){
            int[] test={1,3,2,4,7,5,6,-1,9,0};
            int[] result=sort(test,0,test.length-1);
            System.out.println(Arrays.toString(result));
        }
        
        public static int[] sort(int[] num,int low,int high){
            int mid =(low+high)/2;
            
            if (low<high) {
                sort(num,low,mid);
                sort(num,mid+1,high);
                mergeSort(num,low,mid,high);
            }
            return  num;
        }
        
        public static void mergeSort(int[] num,int low,int mid,int high){
            int i=low;
            int j=mid+1;
            int k=0;
            int[] temp=new int[high-low+1];
            
            while (i<=mid && j<=high && i<j) {
                if (num[i]<num[j]) {
                    temp[k++]=num[i++];
                }else{
                    temp[k++]=num[j++];
                }
                
                
            }
            
            while (i<=mid) {
                temp[k++]=num[i++];
            }
            while (j<=high) {
                temp[k++]=num[j++];
            }
            
            for (int l = 0; l < temp.length; l++) {
                num[low+l]=temp[l];
            }
        }
    }

    快速排序

    public class quikSort {
    
        public static void main(String[] args) {
            
        }
        
        public void sort(int[] num,int left,int right){
            if (left>right) {
                return;
            }
            int temp=num[left];
            int i=left;
            int j=right;
            int swapTemp;
            
            while (i!=j) {  //如果i=j了,就需要把哨兵放到中间了
                while (num[j]>=temp && i<j) {
                    j--;
                }//从右边找小的放到左边,因为哨兵在左边,所以先从右边开始找
                
                
                while (num[i] <= temp && i<j) {
                    i++;
                }//从左边找大的放到右边
                
                
                if (i<j) {
                    swapTemp=num[i];
                    
                    num[i]=num[j];
                    num[j]=swapTemp;
                    
                }
            }
            
            num[left]=num[i];//将哨兵放到中间
            num[i]=temp;
            
            sort(num,left,i-1);//哨兵左边的排序
            sort(num,i+1,right);//哨兵右边的排序
            
        }
    
    }
  • 相关阅读:
    解决无法安装Microsoft .Net Framework 3.5
    day11-15,装饰器
    Xmanager Power Suit 6.0.0009 最新版注册激活
    eth
    MySql 8.0 版本使用navicat连不上解决
    day11
    Mybatis使用规则
    nginx的基本配置
    Mybatis分页插件PageHelper使用
    dubbo的使用
  • 原文地址:https://www.cnblogs.com/wanglao/p/5521996.html
Copyright © 2011-2022 走看看