heapsort - Passing Objects through Heap Sort -
having issues trying pass object class sorted via heap sort. have class holds employee data such names, address, phone numbers , employee id. use heap sort pass class object , sort employee id. main issue converting heap sort structures can take objects. beginning data structures course we're not allowed use advanced techniques. road block i'm stumped how pass objects heap sort methods take primitive data types.
office class:
public class office_staff { public string name , dept , phonenumber; public int id, years; office_staff() { id = (""); name = (""); dept = (""); phonenumber = (""); years = 0; } office_staff(int empid ,string empname, string empdept , string empphone, int service) { id = empid; name = empname; dept = empdept; phonenumber = empphone; years = service; } public void setid(int empid) { id = empid; } public void setname(string empname) { name = empname; } public void setdept(string empdept) { dept = empdept; } public void setphone(string empphone) { phonenumber = empphone; } public void setyears(int service) { years = service; } public string getid() { return id; } public string getname() { return name; } public string getdept() { return dept; } public string getphone() { return phonenumber; } public int getyears() { return years; } public string tostring() { string str = "office_staff name : " + name + "office_staff id : " + id + "office_staff deaprtment : " + dept + "office_staff phone number : " + phonenumber + "years active : " + years; return str; } }
heap sort:
import java.util.scanner; import java.util.arraylist; import java.io.*; class znode { private int idata; public znode(int key) { idata = key; } public int getkey() { return idata; } public void setkey(int k) { idata = k; } } class heapsort { private int [] currarray; private int maxsize; private int currentsize; private int currindex; heapsort(int mx) { maxsize = mx; currentsize = 0; currarray = new int[maxsize]; } //buildheap public boolean buildheap(int [] currarray) { int key = currindex; if(currentsize==maxsize) return false; int newnode = key; currarray[currentsize] = newnode; siftup(currarray , currentsize++); return true; } //siftup public void siftup(int [] currarray , int currindex) { int parent = (currindex-1) / 2; int bottom = currarray[currindex]; while( currindex > 0 && currarray[parent] < bottom ) { currarray[currindex] = currarray[parent]; currindex = parent; parent = (parent-1) / 2; } currarray[currindex] = bottom; } //siftdown public void siftdown(int [] currarray , int currindex) { int largerchild; int top = currarray[currindex]; while(currindex < currentsize/2) { int leftchild = 2*currindex+1; int rightchild = leftchild+1; if(rightchild < currentsize && currarray[leftchild] < currarray[rightchild] ) largerchild = rightchild; else largerchild = leftchild; if( top >= currarray[largerchild] ) break; currarray[currindex] = currarray[largerchild]; currindex = largerchild; } currarray[currindex] = top; } //remove max element public int removemaxelement(int [] currarray) { int root = currarray[0]; currarray[0] = currarray[--currentsize]; siftdown(currarray , 0); return root; } //heapsort private void _sortheaparray(int [] currarray) { while(currentsize != 0) { removemaxelement(currarray); } } public void sortheaparray() { _sortheaparray(currarray); } //hepify private int[] heapify(int[] currarray) { int start = (currentsize) / 2; while (start >= 0) { siftdown(currarray, start); start--; } return currarray; } //swap private int[] swap(int[] currarray, int index1, int index2) { int swap = currarray[index1]; currarray[index1] = currarray[index2]; currarray[index2] = swap; return currarray; } //heapsort public int[] _heapsort(int[] currarray) { heapify(currarray); int end = currentsize-1; while (end > 0) { currarray = swap(currarray,0, end); end--; siftdown(currarray, end); } return currarray; } public void heapsort() { _heapsort(currarray); }
Comments
Post a Comment