java - Get heapsort to print in ascending order -


this program takes array of n length, , uses heapsort pull smallest k elements out. have gotten k number smallest elements out of array, have been trying several hours them print in ascending order. complete binary tree building correctly, based upon father > son. if me figure out need appreciate it.

also, homework assignment , i'm not asking code fix it. legitimately stumped, , set on correct path working correctly. in advance input.

edit - kind of have output in ascending order, basic test getting: 5,6,31,34,29,

import java.lang.*; import java.util.*;  public class heap {  /*  * definitions of parameters  *    1) tree: array sweeping window implemented  *    2) newele: new element insert  *    3) pos: insert new element initially.  *            note not mean newele going  *            stay @ pos after function  *    4) increment  *      a) true: insert newele,  *      b) false: insert newele , remove root  */ static void insertheaptreeat(int[] tree, int newele,         int pos, boolean increment) {     int temp;       //place first element in, no need start tree yet     if (tree[0] == 0) {         tree[0] = newele;     }      //increment true, sliding window isn't full yet     if (increment == true) {         tree[pos] = newele;         //create tree         createtree(tree);      } else {         //increment false, if larger root nothing         //place in pos (being k+1), swap root.         //then createtree slides father > son          if (newele < tree[0]) {             tree[pos] = newele;             temp = tree[0];             tree[0] = tree[pos];             tree[pos] = 0;             createtree(tree);         }     }      //then need compare root down tree check father>=son }  static void createtree(int[] tree) {     int i, father, son;      (i = 1; < tree.length; i++) {         int leaf = tree[i];         son = i;         father = (son - 1) / 2;          while (son > 0 && tree[father] < tree[son]) {             tree[son] = tree[father];             tree[father] = leaf;             son = father;             father = (son - 1) / 2;         }     }     // sort(tree);  }  static void sort(int[] tree) {     int father, larger, temp;     //father = 0;      (int = tree.length - 1; > 0; i--) {          //no need bubble down bc 1 node         if (i - 1 == 0) {             break;         }          //two nodes, root , son         if (i - 1 == 1) {             //then larger son has left (index = 1)             larger = 1;         } else {             //compare left/right sons larger             larger = (tree[1] > tree[2]) ? 1 : 2;         }          //bubble down root         father = 0;         while (tree[father] < tree[larger]) {             temp = tree[father];             tree[father] = tree[larger];             tree[larger] = temp;              father = larger;              if (2 * father + 1 > - 1) {                 break; //no son, rofl             } else if (2 * father + 1 > - 1) {                 larger = 2 * father + 1; //only left son             } else {                 larger = (tree[2 * father + 2] > tree[2 * father + 1]) ? 2 * father + 2                         : 2 * father + 1;              }          }     } }   static void sortascending(int[] x){     int father, son, temp;      (int = 0; < x.length-1; i++) {         son = i;         father = (son - 1) / 2;          //while (son > 0 && x[father] > x[son]) {         while(x[father] > x[son]){             temp = x[son];             x[son] = x[father];             x[father] = temp;              son = father;             father = (son - 1) / 2;         }     }    }  /*  * smallest k elements in array x in o(nlogn) time,  * n size of array x.  *  * supposed return array, containing smallest k elements  * in increasing order.  */ static int[] getsmallestk(int x[], int k) {      if (k > x.length) {         return null;     }     int[] result = new int[k + 1];      // use first k elements in x construct     // complete binary tree, father>=sons     result[0] = x[0];     (int = 1; < k; i++) {         insertheaptreeat(result, x[i], i, true);     }      // each element in rest of array x,     // insert in complete binary tree, ,     // remove root in tree.     (int = k; < x.length; i++) {         insertheaptreeat(result, x[i], k, false);     }      // first k elements in result smallest k elements in x      // sort first k elements in result in o(klogk) time     sortascending(result);     //sort(result);        return result; }  public static void main(string[] args) {       // basic testing       int[] data = {31, 44, 64, 5, 61,         43, 6, 88, 59, 90,         39, 97, 77, 62, 99,         34, 57, 53, 60, 29};      int[] smallestk = getsmallestk(data, 5);     (int = 0; < 5; i++) {         system.out.print(smallestk[i] + ",");     }     system.out.println();       // more complete testing      random random = new random();     list<integer> numslist = new arraylist<integer>();     int[] numsarray = new int[1000];     (int = 0; < 1000; i++) {         int rand = 0;          {             rand = random.nextint(1000);         } while (numslist.contains(rand));          numslist.add(rand);         numsarray[i] = rand;     }      collections.sort(numslist);     int[] smallest100 = getsmallestk(numsarray, 100);      (int = 0; < 100; i++) {         system.out.print(smallest100[i] + ",");     }     system.out.println();      (int = 0; < 100; i++) {         system.out.print(numslist.get(i) + ",");     }      (int = 0; < 100; i++) {         if (numslist.get(i) != smallest100[i]) {             throw new runtimeexception("error");         }     }  } 

}

pass comparator functions implement heap. change sort order, pass different comparator.

static void sortascending(int[] x, comparator comp){ 

then

    while(comp.compare(x[father], x[son]) > 0){ 

etc.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -