package com.cn.gao;
//插入排序反序排序
public class InsertionSort {
    public static final int SIZE=10;
    //插入排序反序排序算法
    public static void insertionSort(int a[], int n){
        int i,j,temp;
        for(i=1;i<n;i++){
            temp=a[i];
            for(j=i-1;j>=0;j--){
                if(a[j]<temp){
                    a[j+1]=a[j];
                }else{
                    break;
                }
            }
            a[j+1]=temp;
            System.out.print("第"+i+"次排序的结果为:");
            for(j=0;j<n;j++){
                System.out.print(" "+a[j]);
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
           int[] a = new int[SIZE];
            //为数组赋值
            for(int i=0;i<a.length;i++){
                a[i] = (int) (100 + Math.random()*100);
            }
            //输出排序前的数组
            System.out.println("排序前的数组为:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
            System.out.println();
            //对数组排序
            insertionSort(a,SIZE);
            //输出排序后的数组
            System.out.println("排序后的数组为:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
            System.out.println();


    }

}

 

相关文章:

  • 2022-01-02
  • 2021-07-10
  • 2021-09-13
  • 2021-07-18
  • 2022-12-23
  • 2021-10-02
  • 2021-10-14
  • 2021-12-10
猜你喜欢
  • 2021-04-09
  • 2021-07-08
  • 2021-06-16
  • 2021-06-25
  • 2021-05-30
  • 2021-06-20
  • 2021-10-24
相关资源
相似解决方案