import java.util.ArrayList;
import java.util.Random;
/**
* Copyright 2015年5月3日 by
*
* All Rights Reserved
*
* @Package
* @Title: InsertSort.java
* @author
* @date 2015年5月3日 上午8:59:56
*/
public class InsertSort
{
public static void main(String[] args)
{
int k = 0;
while (k < 1000)
{
InsertSort is = new InsertSort();
Integer[] toBeSortedData = ArrayUtil.createIntergerArrays(
new Random(), 10);
System.out.println("\n 排序前的数组:");
ArrayUtil.printArray(toBeSortedData);
Integer[] haveSoredArray = is.sort(toBeSortedData);
System.out.println("\n 排序后的数组:");
ArrayUtil.printArray(haveSoredArray);
if (ArrayUtil.isSortedArray(haveSoredArray))
{
System.out.println("\n 排序算法正确");
}
else
{
System.err.println("\n 排序算法错误");
}
k++;
}
}
private Integer[] sort(Integer[] toBeSortedData)
{
ArrayList<Integer> sorted = new ArrayList<Integer>();
for (int index = 0; index < toBeSortedData.length; index++)
{
int temp = toBeSortedData[index];
insertSuitPosition(temp, sorted, index, index + 1);
}
return sorted.toArray(toBeSortedData);
}
private void insertSuitPosition(int data,
ArrayList<Integer> toBeInsertedArray, int beforeInsertedLength,
int afterInsertedlength)
{
if (beforeInsertedLength == 0 && afterInsertedlength == 1)
{
toBeInsertedArray.add(beforeInsertedLength, data);
}
else
{
int start = 0;
int end = beforeInsertedLength - 1;
int position = binarySearch(toBeInsertedArray, data, start, end);
toBeInsertedArray.add(position, data);
}
}
private int binarySearch(ArrayList<Integer> datas, int searchData,
int start, int end)
{
int startData = datas.get(start);
int endData = datas.get(end);
if (searchData >= endData)
{
return end + 1;
}
if (searchData <= startData)
{
return start;
}
int middle = (start + end) / 2;
if (middle == start)
{
return start + 1;
}
else
{
int middleData = datas.get(middle);
if (middleData == searchData)
{
return middleData;
}
if (middleData < searchData)
{
if (middle == end - 1)
{
return middle + 1;
}
else
{
return binarySearch(datas, searchData, middle + 1, end - 1);
}
}
if (middleData > searchData)
{
if (middle == start + 1)
{
return start + 1;
}
else
{
return binarySearch(datas, searchData, start + 1,
middle - 1);
}
}
}
return end;
}
}
import java.util.Random;
/**
* Copyright 2015年5月3日
*
* All Rights Reserved
*
* @Package
* @Title: ArrayUtil.java
* @author
* @date 2015年5月3日 上午9:01:23
*/
public class ArrayUtil
{
public static boolean isSortedArray(int[] test)
{
int i = 0;
int j = 1;
while (j < test.length)
{
if (test[i] > test[j])
{
return false;
}
i++;
j++;
}
return true;
}
public static int[] createIntArrays(Random random, int size)
{
int[] intArray = new int[size];
for (int i = 0; i < intArray.length; i++)
{
intArray[i] = random.nextInt();
}
return intArray;
}
public static void printArray(int[] t)
{
System.out.println("array size is: " + t.length + " values are:");
for (int i : t)
{
System.out.print(i + " ");
}
}
public static boolean isSortedArray(Integer[] test)
{
int i = 0;
int j = 1;
while (j < test.length)
{
if (test[i] > test[j])
{
System.out.println("\n 前一个值:" + test[i] + "大于" + "后一个值:"
+ test[j]);
return false;
}
i++;
j++;
}
return true;
}
public static Integer[] createIntergerArrays(Random random, int size)
{
Integer[] intArray = new Integer[size];
for (int i = 0; i < intArray.length; i++)
{
intArray[i] = random.nextInt();
}
return intArray;
}
public static void printArray(Integer[] t)
{
System.out.println("array size is: " + t.length + " values are:");
for (int i : t)
{
System.out.print(i + " ");
}
}
public static void swap(Integer[] beSorted, int big, int little)
{
int temp = beSorted[big];
beSorted[big] = beSorted[little];
beSorted[little] = temp;
}
}