ydornberg

heapsort

Oct 29th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. public class heapsort {
  2.  
  3.     private int h[];
  4.     private int heapSize; // the number of items stored in h
  5.     public static final int CAPACITY = 100;  
  6.     public heapsort(int[] A, int n) {
  7.         //Initialize h of size CAPACITY
  8.         h = new int [CAPACITY];
  9.         //Copy A into h
  10.         for(int i=1; i<=n; i++)
  11.             h[i]=A[i-1];
  12.         System.out.println("input1.txt:");
  13.         print(h,n);
  14.         Heapsort(h,n);
  15.        
  16.         //BuildMaxHeap();
  17.     }
  18.     public int[] Heapsort(int[] h, int n){
  19.         int temph[]; //array to be outputed at each extract
  20.         int newh[];//max numbers inserted
  21.         temph = new int [CAPACITY];
  22.         newh = new int [CAPACITY];
  23.         int tempValue=0;
  24.         for(int i=1; i<n; i++){
  25.             tempValue = heapMax(h);
  26.             heapExtractMax(h,n);
  27.             newh[i] = tempValue;
  28.             for(int j=1; j<=n; j++)
  29.                 temph[j]=h[j];
  30.             for(int k=1; k<=i; k++){
  31.                 temph[n-k+1]= newh[k];
  32.             }
  33.             print(temph,n);
  34.         }
  35.  
  36.         return newh;
  37.     }
  38.     public void BuildMaxHeap(int[] h,int n){
  39.         for (int i = (n/2); i >= 1; i--)
  40.             MaxHeapify(h,i);
  41.     }
  42.     public void MaxHeapify(int[] h,int index){
  43.         int tempp = h[index];
  44.         int templ = h[2*index];
  45.         int tempr = h[2*index+1];
  46.         if(tempp<templ||tempp<tempr){
  47.             if(templ>tempr||tempr==0){
  48.                 if(templ!=0){
  49.                     h[2*index] = tempp;
  50.                     h[index] = templ;
  51.                     int tdl = 2*index;
  52.                     if(h[tdl]<h[2*tdl]||h[tdl]<h[2*tdl+1])//follow trickle all the way down
  53.                         MaxHeapify(h,tdl);
  54.                 }  
  55.             }
  56.             else{
  57.                 if(tempr!=0){
  58.                     h[2*index+1] = tempp;
  59.                     h[index] = tempr;
  60.                     int tdr = 2*index+1;
  61.                     if(h[tdr]<h[2*tdr]||h[tdr]<h[2*tdr+1])
  62.                         MaxHeapify(h,tdr);
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     public void heapExtractMax(int[] h,int n){//---------------------------------------------------------------------------------------void heapExtractMax
  68.         if(h[1]==0){
  69.             throw new IndexOutOfBoundsException("MaxHeap empty.");
  70.         }
  71.         else{
  72.             int temp = h[n];
  73.             h[1] = temp;
  74.             h[n+1]=0;
  75.             BuildMaxHeap(h,n);
  76.         }
  77.     }
  78.     public int heapMax(int[] h){//--------------------------------------------------------------------------------------------int heapMax
  79.         if(h[1]==0){
  80.             throw new IndexOutOfBoundsException("heapMax empty.");
  81.         }
  82.         return h[1];
  83.     }
  84.  
  85.     public void print(int[] h,int n){//----------------------------------------------------------------------------------void print
  86.  
  87.         int i = n;
  88.         while (i!=0){
  89.             if(h[n-i+1]!=0){
  90.                 System.out.print(h[n-i+1]+" ");
  91.             }
  92.             i--;
  93.         }
  94.         System.out.println("");
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment