<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Help with a heapsort"]]></title>
		<link>http://forums.hotjoe.com/posts/list/1.page</link>
		<description><![CDATA[Latest messages posted in the topic "Help with a heapsort"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Help with a heapsort</title>
				<description><![CDATA[ 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!!!<img src="http://forums.hotjoe.com//images/smilies/9d71f0541cff0a302a0309c5079e8dee.gif" /> Below is the code i wrote...<br /> <br /> /*<br />  * To change this template, choose Tools | Templates<br />  * and open the template in the editor.<br />  */<br /> <br /> package heapSort;<br /> <br /> /**<br />  *<br />  * @author Shani<br />  */<br /> import java.util.logging.Level;<br /> import java.util.logging.Logger;<br /> import sun.security.action.GetIntegerAction;<br /> import java.io.*;<br /> import java.util.ArrayList;<br /> import java.util.List;<br /> import java.util.StringTokenizer;<br /> import java.lang.*;<br /> import java.lang.String;<br /> <br /> <br /> public class heapSort {<br /> <br />     /**<br />      *<br />      * @param args the command line arguments<br />      */<br /> <br />     static String value;<br />     static int maxSize;<br />     static int[]asciiArrayForHeap;<br />     static int [] newArrray;<br /> <br />     public static void readToArray(){<br /> <br />         BufferedReader reader= new BufferedReader(new InputStreamReader (System.in));<br />         try{<br />             System.out.println("Please enter the list to be sorted with spaces and press enter:");<br />                 value=reader.readLine();<br />                 int b=value.length();<br /> <br />                 for (int i=0;i&lt;=b;i++){<br /> <br />                     if (value.charAt(i)==' ')<br />                             maxSize +=1;<br />                     else<br />                         ;<br />                 }<br />             }catch(Exception e){}<br /> <br /> <br />             String[] words=value.split(" ");//splits the input taken from the spacss inbetween<br /> <br />             int count=0;<br />             asciiArrayForHeap=new int[words.length];<br /> <br />             for (String s:words){<br /> <br />                 char[] chars=s.toCharArray();<br /> <br />                 int asciiCount=0;<br /> <br />                 for (char c:chars)<br />                     asciiCount+=(int)c;<br /> <br />                 asciiArrayForHeap[count]=asciiCount;<br />                 count++;<br /> <br />             }<br /> <br />             printUnsortedArray(words);<br /> 			 heapsort(asciiArrayForHeap);<br /> 			 <br /> 	public static void printUnsortedArray(String showArray[]){<br /> <br />          System.out.println("       Heap sort\n\n");<br />          System.out.println(" Values before the sort:\n");<br /> <br />          for (int i=0; i&lt;showArray.length;i++)<br />              System.out.print(showArray[i]+" ");<br /> <br />          System.out.println();<br /> <br />      }<br /> 		 <br /> 	  public static void build_heap(int[]a){<br />         <br />         for (int i=a.length/2;i&gt;0;i--){<br />             max_heapify(a,i);<br />         }<br />    }<br />    <br />    public static void max_heapify(int [] a,int i){<br /> <br />         int p=parent(i);        <br />             <br />                 int l=left_child(p);<br />                 int r=right_child(p);<br />                 int biggest=i;<br /> <br />                 if((l&lt;heapSize(a))&& a[l]&gt;a[p])<br />                     biggest=l;<br />                 if (r&lt;heapSize(a) && a[r] &gt; a[biggest])<br />                    biggest = r;<br />                 if (biggest!=i){<br />                     int temp=a[i];<br />                     a[i]=a[biggest];<br />                     a[biggest]=temp;<br />                     max_heapify(a, biggest);<br />                 }<br />     }<br /> 	<br /> 	 public static void heapsort(int a[]){<br /> <br />          int []A=setHeap(a, a.length);<br />          build_heap(A);<br />          for (int i=a.length-1; i&gt;1;i--){<br /> <br />             // System.out.println("a[" +i+ "] is " + a[i]);<br /> <br />              a[i]=A[0];<br />              A[0]=A[i];<br />              A=setHeap(A,A.length-1);<br />              max_heapify(A, 1);<br />                          <br />          }<br />          <br />    }<br />    <br />    public static int[] setHeap(int []a,int size){<br /> <br />         newArrray=new int[a.length];<br />         for (int i=0;i&lt;a.length;i++){<br />             newArrray[i]=a[i];<br />         }<br />     <br />         return newArrray;<br />     }<br /> 	<br /> 	public static int heapSize(int[]a){<br /> <br />         return a.length;<br />     }<br /> <br />     public static int left_child(int i){<br /> <br />         return (2*i);<br /> <br />     }<br /> <br />     public static int right_child(int i){<br /> <br />         return ((2*i)+1);<br />     }<br /> <br />     public static int parent(int i){<br /> <br />         return ((i-1)/2);<br />     }<br /> <br />     public static void main(String[] args) {<br />         // TODO code application logic here<br />    <br />         readToArray();<br />     }<br /> <br /> <br /> }<br /> <br /> 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...<img src="http://forums.hotjoe.com//images/smilies/9d71f0541cff0a302a0309c5079e8dee.gif" />		   <br /> <br />    ]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/663/2477.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/663/2477.page</link>
				<pubDate><![CDATA[Sun, 3 Jan 2010 00:48:27]]> GMT</pubDate>
				<author><![CDATA[ shani1986rox]]></author>
			</item>
			<item>
				<title>Re:Help with a heapsort</title>
				<description><![CDATA[ What errors are you getting?  Where do you think the error is?  We'll be glad to help but we need some more information.]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/663/2480.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/663/2480.page</link>
				<pubDate><![CDATA[Sun, 3 Jan 2010 10:59:23]]> GMT</pubDate>
				<author><![CDATA[ stdunbar]]></author>
			</item>
	</channel>
</rss>
