com.prl.sort.InsertSort.java Source code

Java tutorial

Introduction

Here is the source code for com.prl.sort.InsertSort.java

Source

/*
 Copyright (c) 2013 QIDAPP.com. All rights reserved.
 QIDAPP.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.prl.sort;

import java.util.Random;

import org.apache.commons.lang.StringUtils;

/**
 * ??
 * 
 * @author Pengrl
 * @version $Id$
 * @since 1.1, 2014-6-21
 */
public class InsertSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Integer[] arr = new Integer[10];
        Random random = new Random();
        int index = 0;
        while (true) {
            int num = random.nextInt(100) + 1;
            arr[index++] = num;
            if (index > 9) {
                break;
            }
        }

        System.out.println(StringUtils.join(arr, ","));

        int temp = 0;
        for (int i = 1; i < arr.length; i++) {
            int j = i - 1;
            temp = arr[i]; // ?
            // ???????
            for (; j >= 0 && arr[j] > temp; j--) {
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }

        System.out.println(StringUtils.join(arr, ","));
    }

}