首页 > 编程语言 > 详细

插入排序反序排序

时间:2015-05-07 08:40:16      阅读:215      评论:0      收藏:0      [点我收藏+]
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();


    }

}

 

插入排序反序排序

原文:http://www.cnblogs.com/gaopeng527/p/4483799.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!