Please fix the proggram, make it compilable. There are some errors that does not
ID: 3769572 • Letter: P
Question
Please fix the proggram, make it compilable. There are some errors that does not aloow run the program.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Heapprog
{
private static void swapping (int [] heapdata,
int i, int j) {
final int temp val = heapdata [i];
heapdata [i] = heapdata [j];
heapdata [j] = temp val;}
public static void heap.sort (int[] heapdata,
int no)
{
maketheheap (heapdata, no);
for (int i = no-1; i>0; i--)
{
swapping ( heapdata, 0, i);
System.out.println (i+": ");
printArray(heapdata, no);
reheapdown (heapdata, i-0);
}
}
private static int lefttree (int i)
{return 2* i+1;
}
private static int right (int i)
{
return 2* i+2;
}
private static int parent(int i)
{
return (i-1)/2;
}
private static int largechild (int [] heapdata, int root, int size)
{
final int leftchild = lefttree (root);
final int rightchild = righttree (root);
if (rightchild < size && heapdata[rightchild] > heapdata [leftchild])
{
return rightchild;
}
else
{
return leftchild;
}
}
private static void heapDown (final int [] heapdata, int size)
int root = 0;
while (2* root + 1 < size)
{
final int maxchild = largechild (heapdata, root, size);
final int r 1 = heapdata[root];
if (r 1 > max) break;
heapdata [root] = max;
heapdata [maxchild] = r 1;
root = maxchild;
}
}
public static void reheapup (final int[] heapdata, int bottom)
{
while (bottom > 0)
{
final int parent = parent (bottom);
final int b = heapdata [bottom];
final int p = heapdata [parent];
if (p > = b) break;
heapdata [bottom] = p;
heapdata [parent] = b;
bottom = parent;
}
}
private static void printArray ( int [] heapdata, int no)
{
printArray (heapdata, 0, no);
}
private static void printArray (int [] heapdata, int first, int no)
{
System.out.print ("[");
for (int i = first; i < first + no; i++)
{
System.out.print (heapdata [i]);
if (i < first + no - 1)
System.out.print (",");
}
System.out.print ("]");
}
public static void main (String[]args)
{
int [] heapdata = new int [1000];
int no = 0;
for (int i = 0; i , args.length; i++)
{
heapdata [no + 1] = Integer.parent
(args [i]);
}
printArray (heapdata, no);
heap.sort (heapdata, no);
printArray (heapdata, no);
}
}
Explanation / Answer
Hi ,
Issue fixed.
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
public class Heapprog
{
private static void swapping (int [] heapdata,int i, int j) {
final int tempval = heapdata [i];
heapdata [i] = heapdata [j];
heapdata [j] = tempval;
}
public static void heapsort (int[] heapdata, int no)
{
// maketheheap (heapdata, no);
for (int i = no-1; i>0; i--)
{
swapping ( heapdata, 0, i);
System.out.println (i+": ");
printArray(heapdata, no);
reheapDown(heapdata, i-0);
}
}
private static int lefttree (int i)
{return 2* i+1;
}
private static int righttree (int i)
{
return 2* i+2;
}
private static int parent(int i)
{
return (i-1)/2;
}
private static int largechild (int [] heapdata, int root, int size)
{
final int leftchild = lefttree (root);
final int rightchild = righttree (root);
if (rightchild < size && heapdata[rightchild] > heapdata [leftchild])
{
return rightchild;
}
else
{
return leftchild;
}
}
private static void reheapDown (final int [] heapdata, int size){
int root = 0;
int max=0;
while (2* root + 1 < size)
{
final int maxchild = largechild (heapdata, root, size);
final int r1 = heapdata[root];
if (r1 > max) break;
heapdata [root] = max;
heapdata [maxchild] = r1;
root = maxchild;
}
}
public static void reheapup (final int[] heapdata, int bottom)
{
while (bottom > 0)
{
final int parent = parent (bottom);
final int b = heapdata [bottom];
final int p = heapdata [parent];
if (p >= b) break;
heapdata [bottom] = p;
heapdata [parent] = b;
bottom = parent;
}
}
private static void printArray ( int [] heapdata, int no)
{
printArray (heapdata, 0, no);
}
private static void printArray (int [] heapdata, int first, int no)
{
System.out.print ("[");
for (int i = first; i < first + no; i++)
{
System.out.print (heapdata [i]);
if (i < first + no - 1)
System.out.print (",");
}
System.out.print ("]");
}
public static void main (String[]args)
{
int [] heapdata = new int [1000];
int no = 0;
for (int i = 0; i<args.length; i++)
{
heapdata [no + 1] = Integer.parseInt(args [i]);
}
printArray (heapdata, no);
heapsort (heapdata, no);
printArray (heapdata, no);
}
}