算法-->反序(插入)

来源:互联网 发布:php unpack 二进制 编辑:程序博客网 时间:2024/06/05 02:58
package 插入排序;/*** * 反序 *  * @author 朱珍珍 * */public class Fanxu {    static final int SIZE = 10;    static void insertSort(int[] a, int len) {        int i, j, t, h;        for (i = 1; i < len; i++) {            t = a[i];            j = i - 1;            while (j >= 0 && t > a[j]) {                a[j + 1] = a[i];                j--;            }            a[j + 1] = t;            System.out.print("第" + i + "步排序结果");// 输出每步 的 排序结果            for (h = 0; h < len; h++) {                System.out.print(" " + a[h]);            }            System.out.print("\n");        }    }    public static void main(String[] args) {        Fanxu ff = new Fanxu();        int[] shuzu = new int[SIZE];        int i;        for (i = 0; i < SIZE; i++) {            shuzu[i] = (int) (100 + Math.random() * (100 + 1));// 初始话 数组        }        System.out.print("排序前的数组 为:\n");        for (i = 0; i < SIZE; i++) {            System.out.print(" " + shuzu[i]);        }        System.out.print("\n");        insertSort(shuzu, SIZE);        System.out.print("排序后的数组 为:\n");        for (i = 0; i < SIZE; i++) {            System.out.print(shuzu[i] + " ");        }        System.out.print("\n");    }}

这里写图片描述

原创粉丝点击