/* 来 自 时 代 J a v a 公 众 号 - nowjava.com*/
import java.security.SecureRandom;
import java.util.Arrays;
public class Main
{
// sort array using insertion sort
public static void insertionSort(int[] data)
{
// loop over data.length - 1 elements
for (int next = 1; next < data.length; next++)
{
int insert = data[next]; // value to insert
int moveItem = next; // location to place element
/**
nowjava.com
**/
// search for place to put current element
while (moveItem > 0 && data[moveItem - 1] > insert)
{
// shift element right one slot
data[moveItem] = data[moveItem - 1];
moveItem--;
}
data[moveItem] = insert; // place inserted element
printPass(data, next, moveItem); // output pass of algorithm
}
}
// print a pass of the algorithm
public static void printPass(int[] data, int pass, int index)
{
System.out.printf("after pass %2d: ", pass);
// output elements till swapped item
for (int i = 0; i < index; i++)
System.out.printf("%d ", data[i]);
System.out.printf("%d* ", data[index]); // indicate swap
// finish outputting array
for (int i = index + 1; i < data.length; i++)
System.out.printf("%d ", data[i]);
System.out.printf("%n "); // for alignment
// indicate amount of array that?s sorted
for(int i = 0; i <= pass; i++)
System.out.print("-- ");
System.out.println();
}
public static void main(String[] args)
{
SecureRandom generator = new SecureRandom();
int[] data = new int[10]; // create array
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。