package none012堆;
//堆排序运行的时间复杂度为O(N*logN),尽管他比快速排序略慢,但他比快速排序优越的一点是
//它对初始数据的分布不敏感。在关键字值按某种排列顺序的情况下,快速排序运行的时间复杂度可以降到O(N^2)级,然而堆排序对任意排列的数据,其排序的时间复杂度都是O(N*logN);
//可以使用同一个数组来存放初始无序的数据,堆以及最后有序的数据,因此,堆排序不需要额外的存储空间。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HeapSortApp {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int size,j;
System.out.print("Enter number of items: ");
size=getInt();
HeapX theHeap=new HeapX(size);
for(j=0;j<size;j++)
{
int random=(int)(java.lang.Math.random()*100);
NodeX newNode=new NodeX(random);
theHeap.insertAt(j, newNode);
theHeap.incrementSize();
}
System.out.print("Random: ");
theHeap.displayArray();
for(j=size/2-1;j>=0;j--)
theHeap.trickleDown(j);
System.out.print("Heap: ");
theHeap.displayArray();
theHeap.displayHeap();
for(j=size-1;j>=0;j--)
{
NodeX biggestNode=theHeap.remove();
theHeap.insertAt(j, biggestNode);
}
System.out.print("Sorted: ");
theHeap.displayArray();
}
public static String getString() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
public static char getChar() throws IOException{
String s=getString();
return s.charAt(0);
}
public static int getInt() throws IOException
{
String s=getString();
return Integer.parseInt(s);
}
}
class NodeX
{
private int iData;
public NodeX(int key)
{
iData=key;
}
public int getKey(){
return iData;
}
}
class HeapX
{
private NodeX[] heapArray;
private int maxSize;
private int currentSize;
public HeapX(int mx)
{
maxSize=mx;
currentSize=0;
heapArray=new NodeX[maxSize];
}
public boolean isEmpty()
{
return currentSize==0;
}
public NodeX remove()
{
NodeX root=heapArray[0];
heapArray[0]=heapArray[--currentSize];
trickleDown(0);
return root;
}
public void trickleDown(int index){
int largerChild;
NodeX top=heapArray[index];
while(index<currentSize/2)
{
int leftChild=2*index+1;
int rightChild=leftChild+1;
if(rightChild<currentSize&&heapArray[leftChild].getKey()<heapArray[rightChild].getKey())
largerChild=rightChild;
else
largerChild=leftChild;
if(top.getKey()>=heapArray[largerChild].getKey())
break;
heapArray[index]=heapArray[largerChild];
index=largerChild;
}
heapArray[index]=top;
}
public void displayHeap(){
int nBlanks=32;
int itemsPerRow=1;
int column=0;
int j=0;
String dots="..................................";
System.out.println(dots+dots);
while(currentSize>0)
{
if(column==0)
for(int k=0;k<nBlanks;k++)
System.out.print(' ');
System.out.print(heapArray[j].getKey());
if(++j==currentSize)
break;
if(++column==itemsPerRow)
{
nBlanks/=2;
itemsPerRow*=2;
column=0;
System.out.println();
}
else
for(int k=0;k<nBlanks*2-2;k++)
System.out.print(' ');
}
System.out.println("\n"+dots+dots);
}
public void displayArray()
{
for(int j=0;j<maxSize;j++)
System.out.print(heapArray[j].getKey()+" ");
System.out.println("");
}
public void insertAt(int index,NodeX newNode){
heapArray[index]=newNode;
}
public void incrementSize(){
currentSize++;
}
}
堆排序
最新推荐文章于 2023-09-21 13:21:58 发布