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);//哨兵右边的排序
            
        }
    
    }
  • 相关阅读:
    SharePoint 2013 开发——开发并部署Provider-hosted APP
    SharePoint 2013 开发——Provider-hosted APP准备工作
    (转)CString工作原理和常见问题分析
    WinDbg调试 C# dmp
    使用fastcall 代替汇编hook thiscall
    Windows Socket 编程_单个服务器对多个客户端简单通讯
    IOCP模型与网络编
    非阻塞模式(ioctlsocket)
    GDT与LDT
    常见程序入口点(OEP)特征
  • 原文地址:https://www.cnblogs.com/wanglao/p/5521996.html
Copyright © 2011-2022 走看看