S
spidey12345
Here is what i wrote for heap sort
//heapSort method
public static void heapsort(float[] array)
{
int n = array.length-1;
BuildMaxHeap(array);
while(n>=2){
swap(array, 1, n);
n--;
}
maxHeapfy(array, 1);
}
private static void maxHeapfy(float[] array, int i)
{
int n = array.length-1;
int largest = i;
int l = 2*i;
int r = 1+2*i;
if ( l <= n && array[l] >=array[largest]) {
largest = l;
}
if ( r <= n && array[r] >=array[largest] ) {
largest = r;
}
if ( largest != i ) {
swap(array, largest, i);
maxHeapfy(array, largest);
}
}
private static void BuildMaxHeap(float[] array)
{
int n = array.length-1;
for(int i = n/2; i>=1; i--)
maxHeapfy(array, i);
}
private static void swap(float array[], int i, int j)
{
float t=array;
array=array[j];
array[j]=t;
}
when i run a float array with = { 0.12, 0.67, 0.57, 0.64, 0.93}
it gives me wrong output of 0.64, 0.57, 0.67, 0.12, 0.93} Anybody know
where my aglorithm is wrong at
by the way, the array is 0-20
so i ingored the first 0 index, and go from 1-20...
and i copy the 0-19 original array to the 1-20 copied array, and sort
the copied array... so i don't think there is anything wrong with the
starting indexes, PLZ HELP, if you can tell me where the error is at...
//heapSort method
public static void heapsort(float[] array)
{
int n = array.length-1;
BuildMaxHeap(array);
while(n>=2){
swap(array, 1, n);
n--;
}
maxHeapfy(array, 1);
}
private static void maxHeapfy(float[] array, int i)
{
int n = array.length-1;
int largest = i;
int l = 2*i;
int r = 1+2*i;
if ( l <= n && array[l] >=array[largest]) {
largest = l;
}
if ( r <= n && array[r] >=array[largest] ) {
largest = r;
}
if ( largest != i ) {
swap(array, largest, i);
maxHeapfy(array, largest);
}
}
private static void BuildMaxHeap(float[] array)
{
int n = array.length-1;
for(int i = n/2; i>=1; i--)
maxHeapfy(array, i);
}
private static void swap(float array[], int i, int j)
{
float t=array;
array=array[j];
array[j]=t;
}
when i run a float array with = { 0.12, 0.67, 0.57, 0.64, 0.93}
it gives me wrong output of 0.64, 0.57, 0.67, 0.12, 0.93} Anybody know
where my aglorithm is wrong at
by the way, the array is 0-20
so i ingored the first 0 index, and go from 1-20...
and i copy the 0-19 original array to the 1-20 copied array, and sort
the copied array... so i don't think there is anything wrong with the
starting indexes, PLZ HELP, if you can tell me where the error is at...