常规排序

public class Sort{
    public static void main(String[] args){
        int[] x = new int[6] ;
        System.out.println("排序前:");
        for(int i=0;i<x.length;i++){
            x[i] = (int)(java.lang.Math.random()*100) ;
            System.out.print(x[i]+" ") ;
        }
        insertSort(x) ;
        System.out.println() ;
        print(x) ;
    }

    //打印数组
    public static void print(int[] a){
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ") ;
        }
    }

    /**冒泡排序(稳定排序)
     * 时间复杂度:O(n^2)
     * 适合于数据量小或者是已有序的序列
    */
    public static void bubbleSort(int[] a){
        boolean isSwap = false ;    //定义标记位,记录每次扫描时是否发生交换
        for(int i=a.length-1;i>1;i--){
            isSwap = false ;    //每次扫描前,都重置flag
            for(int j=0;j<i;j++){
                if(a[j]>a[j+1]){    //若a[j]>a[j+1]则交换
                    int tmp = a[j] ;
                    a[j] = a[j+1] ;
                    a[j+1] = tmp ;
                    isSwap = true ;
                }
            }
            if(!isSwap){    //若此次扫描未发生交换,则表明已有序,无需进行下次扫描
                return ;
            }
        }
    }

    /**选择排序(不稳定排序)
     * 时间复杂度:O(n^2)
     * 改善了冒泡排序,降低了交换次数
     */
    public static void selectSort(int[] a){
        for(int i=0;i<a.length-1;i++){
            int min = a[i] ;    
            for(int j=i+1;j<a.length;j++){
                if(a[j]<min){
                    min = a[j] ;    //若a[j]小于min则将a[j]置为min
                    int tmp = a[j] ;
                    a[j] = a[i] ;
                    a[i] = tmp ;
                }
            }
        }
    }
    /**插入排序(稳定排序)
     * 时间复杂度:O(n^2)
     * 适合于数据量小的序列
    */
    public static void insertSort(int[] a){
        int j ;
        for(int i=1;i<a.length;i++){
            int tmp = a[i] ;
            for(j=i-1;j>=0&&tmp<a[j];j--){
                a[j+1] = a[j] ;    //若符合条件则后移
            }
            a[j+1] = tmp ;    //找到下标为i的位置插入元素
        }
    }

    /**希尔排序(不稳定排序)
     * 时间复杂度:不稳定
     * 适合中等规模的排序
     */
     public static void shellSort(int[] a){
        int len = a.length ;
        int j ;
        for(int h=len/2;h>0;h=h/2){
            for(int i=h;i<len;i++){
                int tmp = a[i] ;
                for(j=i-h;j>=0&&tmp<a[j];j-=h){
                    a[j+h] = a[j] ;
                }
                a[j+h] = tmp ;
            }
        }
     }

    //划分法
    private static int partition(int[] a,int low,int high){
        int pivot = a[low] ;    //使用a[low]作为枢轴
        while(low<high)    //从两端交替向内扫描
            while(low<high&&a[high]>=pivot)    //判断a[high]>=pivot和a[low]<pivot的顺序不能调换
                high-- ;
            a[low] = a[high] ;    //将比pivot小的元素移至低端
            while(low<high&&a[low]<pivot)
                low++ ;
            a[high] = a[low] ;    //将比pivot大的元素移至高端
        
        a[low] = pivot ;    //设置枢轴
        return low ;
    }

     /**快速排序(不稳定排序)
      * 时间复杂度:最好:O(nlogn),最坏:O(n^2)
      *    采用分治法
      */
      public static void quickSort(int[] a,int low,int high){
          if(low<high){
            int pos = partition(a,low,high) ;
            quickSort(a,low,pos-1) ;    //对左区间递归
            quickSort(a,pos+1,high) ;    //对右区间递归
          }
      }

      /**
       * 归并排序(稳定排序)
       * 时间复杂度:O(nlogn)
       * 采用分治法
       */
       public static void mergeSort(int[] a,int low,int high){
           int mid = (low+high)/2 ;
            if(low<high){
                mergeSort(a,low,mid) ;
                mergeSort(a,mid+1,high) ;
                merge(a,low,mid,high) ;
            }
       }
       //分治划分
       private static void merge(int[] a,int low,int mid,int high){
            int[] result = new int[high-low+1] ;
            int p = low ;
            int q = mid+1 ;
            int k = 0 ;
            while(p<=mid&&q<=high){
                if(a[p]<a[q]){
                    result[k++] = a[p++] ;
                }else{
                    result[k++] = a[q++] ;
                }
            }
            while(p<=mid)
                result[k++] = a[p++] ;
            while(q<=high)
                result[k++] = a[q++] ;
            for(int e:result){
                a[low++] = e ;
            }
       }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值