import java.util.Random;
import yongbo.tan.algorithm.ArrayUtil;
import yongbo.tan.algorithm.sort.Sort;
/**
* Copyright 2015年5月3日
*
* All Rights Reserved
*
* @Package
* @Title: HeapSort.java
* @author
* @date 2015年5月3日 下午6:54:37
*/
public class HeapSort implements Sort
{
public static void main(String[] args)
{
int k = 0;
while (k < 1000)
{
HeapSort ss = new HeapSort();
Integer[] toBeSortedData = ArrayUtil.createIntergerArrays(
new Random(), 100);
System.out.println("\n 排序前的数组:");
ArrayUtil.printArray(toBeSortedData);
Integer[] haveSoredArray = ss.sort(toBeSortedData);
System.out.println("\n 排序后的数组:");
ArrayUtil.printArray(haveSoredArray);
if (ArrayUtil.isSortedArray(haveSoredArray))
{
System.out.println("\n 排序算法正确");
}
else
{
System.err.println("\n 排序算法错误");
}
k++;
}
}
@Override
public Integer[] sort(Integer[] beSorted)
{
bulidHeap(beSorted);
for (int i = 0,j=beSorted.length-1; j >=1 ; j--)
{
swap(beSorted,i,j);
heapAjust(beSorted, i, j);
}
return beSorted;
}
private void bulidHeap(Integer[] beSorted)
{
for (int lastParentPosition = beSorted.length / 2 - 1; lastParentPosition >= 0; lastParentPosition--)
{
heapAjust(beSorted, lastParentPosition, beSorted.length);
}
}
private void heapAjust(Integer[] beSorted, int lastParentPosition,
int currentBeSortedArrayLength)
{
int leftPosition = getLeftChildPosition(lastParentPosition);
if (leftPosition >= currentBeSortedArrayLength)
{
return;
}
int leftValue = beSorted[leftPosition];
int rightPosition = getRightChildPosition(lastParentPosition);
int rightValue = Integer.MIN_VALUE;
if (rightPosition < currentBeSortedArrayLength)
{
rightValue = beSorted[rightPosition];
}
int maxValue = leftValue;
int maxIndex = leftPosition;
if (maxValue < rightValue)
{
maxValue = rightValue;
maxIndex = rightPosition;
}
int currentParentValue = beSorted[lastParentPosition];
if (currentParentValue < maxValue)
{
swap(beSorted, maxIndex, lastParentPosition);
heapAjust(beSorted, maxIndex, currentBeSortedArrayLength);
}
}
public Integer[] sortFrontToBehind(Integer[] beSorted)
{
for (int i = 0; i < beSorted.length / 2; i++)
{
int currentValue = beSorted[i];
int leftPosition = getLeftChildPosition(i);
int leftValue = beSorted[leftPosition];
int rightPosition = getRightChildPosition(i);
int rightValue = Integer.MIN_VALUE;
if (rightPosition < beSorted.length)
{
rightValue = beSorted[rightPosition];
}
if (rightValue > leftValue && currentValue < rightValue)
{
swap(beSorted, leftPosition, i);
int iCurrent = i;
int iParent = getParentPositionByChildPos(iCurrent);
while (iParent >= 0)
{
if (beSorted[iParent] > beSorted[iCurrent])
{
break;
}
else
{
swap(beSorted, iCurrent, iParent);
iCurrent = iParent;
iParent = getParentPositionByChildPos(iCurrent);
}
}
if (iCurrent != i)
{
}
}
if (rightValue < leftValue && currentValue < leftValue)
{
swap(beSorted, leftValue, currentValue);
}
}
return null;
}
private void swap(Integer[] beSorted, int big, int little)
{
int temp = beSorted[big];
beSorted[big] = beSorted[little];
beSorted[little] = temp;
}
private int getLeftChildPosition(int parentPosition)
{
return parentPosition * 2 + 1;
}
private int getRightChildPosition(int parentPosition)
{
return parentPosition * 2 + 2;
}
private int getParentPositionByLeftChildPos(int leftChildPos)
{
return (leftChildPos - 1) / 2;
}
private int getParentPositionByRightChildPos(int rightChildPos)
{
return (rightChildPos - 2) / 2;
}
private int getParentPositionByChildPos(int childPos)
{
if (childPos % 2 == 0)
{
return getParentPositionByRightChildPos(childPos);
}
else
{
return getParentPositionByLeftChildPos(childPos);
}
}
}
import java.util.Random;
/**
* Copyright 2015年5月3日 by
*
* 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;
}
}