[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
Help with a heapsort  XML
Forum Index » New to Java
Author Message
shani1986rox

Newbie

Joined: 01/03/2010 00:38:19
Messages: 1
Offline

Hi, thanks for havin me on the forum... I'm kinda newbie to java and I ahve this assignment to be submitted on tuesday... I wrote the code for it, but I'm stuck and dunno where I can find the error.. If someone can pls pls pls help me coz I'm devestated!!! Below is the code i wrote...

/*
* 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));
try{
System.out.println("Please enter the list to be sorted with spaces and press enter:");
value=reader.readLine();
int b=value.length();

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

if (value.charAt(i)==' ')
maxSize +=1;
else
;
}
}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)
asciiCount+=(int)c;

asciiArrayForHeap[count]=asciiCount;
count++;

}

printUnsortedArray(words);
heapsort(asciiArrayForHeap);

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]+" ");

System.out.println();

}

public static void build_heap(int[]a){

for (int i=a.length/2;i>0;i--){
max_heapify(a,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])
biggest=l;
if (r<heapSize(a) && a[r] > a[biggest])
biggest = r;
if (biggest!=i){
int temp=a[i];
a[i]=a[biggest];
a[biggest]=temp;
max_heapify(a, biggest);
}
}

public static void heapsort(int a[]){

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

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

a[i]=A[0];
A[0]=A[i];
A=setHeap(A,A.length-1);
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++){
newArrray[i]=a[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

readToArray();
}


}

I wanted was to get an inout String array into a char array and sort it accordng to heapsort... There's something wrong, If you can ammend it or do wotever.. and reply so i can understand where i went wrong... Thank you sooooooooooooo much...

stdunbar

Newbie
[Avatar]

Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline

What errors are you getting? Where do you think the error is? We'll be glad to help but we need some more information.

Thanks for using the forums at hotjoe.com
[WWW] [Yahoo!] [ICQ]
 
Forum Index » New to Java
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle