归并/快速/堆/插入排序--Java实现

复习这几种排序的写法以及过程

import java.util.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    static int ans = 0,n;
    static int[] a = new int[10005];
    static void mergesort(int l,int r,int[] t){
        if(r-l>1){
            int m = l + (r-l)/2;
            int p = l,q = m,i = l;
            mergesort(l,m,t);
            mergesort(m,r,t);
            while(p<m||q<r){
                if(q>=r||(p<m&&a[p]<a[q])) t[i++] = a[p++];
                else {
                    t[i++] = a[q++];
                    ans += m-p;//逆序数
                }
            }
            for (int j = l; j < r; j++) {
                a[j] =t[j];
//                System.out.print(a[j]+" ");
            }
//            System.out.println();
        }

    }
    static void quickSort(int l,int r){
        if(l>r) return;
        int th = a[l],i = l,j = r,temp;
        while(i<j) {
            while (i < j && a[j] >= th) j--;
            while (i < j && a[i] <= th) i++;
            if (i < j) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        a[l] = a[i];
        a[i] = th;
//        for (int k = l; k <= r; k++) {
//                System.out.print(a[k]+" ");
//        }
//        System.out.println();
        quickSort(l,i-1);
        quickSort(i+1,r);
    }
    static void shiftDown(int i){
        int t = i;
        boolean f =false;
        while (!f&&i*2<=n){
            if(a[i]<a[i*2]) t = i*2;
            else t = i;
            if(i*2+1<=n&&a[t]<a[i*2+1]) t = i*2+1;
            if(t!=i){
                int te = a[i];
                a[i] = a[t];
                a[t] = te;
                i = t;
            }else{
                f = true;
            }
        }
    }
   static void heapSort(){
        while (n>1){
            int te = a[1];
            a[1] = a[n];
            a[n] = te;
//            for (int k = 1; k <= n; k++) {
//                System.out.print(a[k]+" ");
//            }
//            System.out.println();
            n--;
            shiftDown(1);
        }
   }
   static void create(){
       for (int i = n/2; i >= 1; i--) {
          shiftDown(i);
       }
   }

    static void insertSort(int n){
        int i,j;
        for(i = 2;i <= n;i++){
            if(a[i]<a[i-1]){
                a[0] = a[i];
                for(j = i-1;a[0]<a[j];j--)
                    a[j+1]=a[j];
                a[j+1] =a[0];
            }
            for (int k = 1; k <= n; k++) {
                System.out.print(a[k] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int t = in.nextInt();
        while(t-->0){
             n = in.nextInt();
             a = new int[n+1];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            int te = n;
//            mergesort(1,n+1,new int[n+1]);
//            quickSort(1,n);
//            create();
//            heapSort();
            insertSort(n);
            for (int i = 1; i <= te; i++) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值