1 Replies - 112 Views - Last Post: 30 January 2018 - 04:45 PM Rate Topic: -----

#1 crazyamerican  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-January 17

need help with heapAdd(float[] a)

Posted 30 January 2018 - 04:16 PM

It is supposed to pass 4 tests; random floats, big numbers, small numbers, and random ints. It passes on the small test but none of the others. i do not know whats wrong with the code. Any help would be great.


public static float heapAdd(float[] a) {
		if (a.length == 0){
			return 0;
		}

	    float min = 0;
	    int min2 = 0;
	    float sum = 0;
	    int end = a.length;

	    if (a.length == 0) {
	        return 0;
	    }

	    for (int i = 0; i < a.length; i++) {
	        pullUp(i, a);
	    }

	    for (int j = 0; j < a.length; j++) {
	        if (end != 1) {
	            min = a[0];

	            if (end != 2) {
	                if (a[1] > a[2]) {
	                    min2 = 2;
	                } else {
	                    min2 = 1;
	                }
	            } else {
	                min2 = 1;
	            }

	            if (min2 > end - 1) {
	                sum = min + a[1];
	            } else {
	                sum = min + a[min2];
	            }

	            a[0] = sum;

	            if (a[min2] == a[1]) {
	                a[1] = a[end - 1];
	            } else {
	                a[2] = a[end - 1];
	            }

	            end--;

	            min = a[0];


	            pushDown(min2, a, end);
	        }
	    }

	    return a[0];// return the error-minimized sum of floats
	}

	private static float[] pushDown(int i, float[] a, int end) {

	    int Lchild = (2 * i) + 1;
	    int Rchild = (2 * i) + 2;

	    float tmp;
	    int min = 0;

	    if (Lchild > end - 1) {
	        return a;
	    } else if (Rchild > end - 1) {
	        min = Lchild;
	    }


	    if (min != Lchild) {
	        if (a[Lchild] < a[Rchild]) {
	            min = Lchild;
	        } else if (a[Rchild] < a[Lchild]) {
	            min = Rchild;
	        }
	    }

	    if (min != i) {
	        tmp = a[i];
	        a[i] = a[min];
	        a[min] = tmp;
	        return (pushDown(min, a, end));
	    }
	    return a;
	}

	private static float[] pullUp(int i, float[] a) {

	    int parent = (i - 1) / 2;

	    if (parent >= 0) {

	        float tmp;

	        if (a[i] < a[parent]) {
	            tmp = a[i];
	            a[i] = a[parent];
	            a[parent] = tmp;
	            return pullUp(parent, a);
	        }
	    }
	    return a;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: need help with heapAdd(float[] a)

#2 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,980
  • Joined: 25-December 13

Re: need help with heapAdd(float[] a)

Posted 30 January 2018 - 04:45 PM

Quote

It passes on the small test but none of the others.

Please post something like program output or error messages that shows what you are talking about.
Add some comments describing what the problem is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1