Hi, thank you for havin me in the forum... I'm kinda newbie to java... This is the code that I wrote for heapsort, but it seems like there is problem, can you pls pls help.. coz I'v been workin on this for a whole day, and i'v to submit it on next Tuesday..:( Thank you sooo much...

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package heapSort;

 * @author Shani
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.security.action.GetIntegerAction;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.lang.*;
import java.lang.String;

public class heapSort {

     * @param args the command line arguments

    static String value;
    static int maxSize;
    static int[]asciiArrayForHeap;
    static int [] newArrray;

    public static void readToArray(){

        BufferedReader reader= new BufferedReader(new InputStreamReader (System.in));
            System.out.println("Please enter the list to be sorted with spaces and press enter:");
                int b=value.length();

                for (int i=0;i<=b;i++){

                    if (value.charAt(i)==' ')
                            maxSize +=1;
            }catch(Exception e){}

            String[] words=value.split(" ");//splits the input taken from the spacss inbetween

            int count=0;
            asciiArrayForHeap=new int[words.length];

            for (String s:words){

                char[] chars=s.toCharArray();

                int asciiCount=0;

                for (char c:chars)



	public static void printUnsortedArray(String showArray[]){

         System.out.println("       Heap sort\n\n");
         System.out.println(" Values before the sort:\n");

         for (int i=0; i<showArray.length;i++)
             System.out.print(showArray[i]+" ");


	  public static void build_heap(int[]a){
        for (int i=a.length/2;i>0;i--){
   public static void max_heapify(int [] a,int i){

        int p=parent(i);        
                int l=left_child(p);
                int r=right_child(p);
                int biggest=i;

                if((l<heapSize(a))&& a[l]>a[p])
                if (r<heapSize(a) && a[r] > a[biggest])
                   biggest = r;
                if (biggest!=i){
                    int temp=a[i];
                    max_heapify(a, biggest);
	 public static void heapsort(int a[]){

         int []A=setHeap(a, a.length);
         for (int i=a.length-1; i>1;i--){

            // System.out.println("a[" +i+ "] is " + a[i]);

             max_heapify(A, 1);
   public static int[] setHeap(int []a,int size){

        newArrray=new int[a.length];
        for (int i=0;i<a.length;i++){
        return newArrray;
	public static int heapSize(int[]a){

        return a.length;

    public static int left_child(int i){

        return (2*i);


    public static int right_child(int i){

        return ((2*i)+1);

    public static int parent(int i){

        return ((i-1)/2);

    public static void main(String[] args) {
        // TODO code application logic here


What I wanted to do was take an input string to a char array and sort it in heapsort method.. Y doesn't it work? Can you please ammend it so I can understand where I went wrong? Thank you again.... I hope you'll write as soon as possible...:( I'm devestated!!

int b=value.length();
                 for (int i=0;i<=b;i++)
{                     if (value.charAt(i)==' ')
                            maxSize +=1;

I didnt have time to look at all your code but noticed something at the top.

your going to <=length in your loop here.

String c= "cat"; has stringlength 3. but charAt() will only work on spots 0,1,2, not 0-3 or 1-3. charAt(3) on "cat" would throw an exception. typically we do < stringname.length() not <=.

also is maxSize every initialized? i think it may start at 0 but still... :)


This article has been dead for over six months. Start a new discussion instead.