复习这几种排序的写法以及过程
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();
}
}
}