Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Passing Objects through Heap Sort

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having issues trying to pass an object class to be sorted via Heap Sort. Basically I have a class which holds employee data such as names, address, phone numbers and employee ID. We are to use Heap Sort to pass this class as a object and sort it by employee ID. My main issue is converting my heap sort structures to where they can take objects. This is for a beginning data structures course so we're not allowed to use advanced techniques. My road block is I'm stumped as to how to pass my objects into the heap sort methods which currently only take primitive data types. Example instead of: buildHeap(int [] currArray) it needs to take it as a object like buildHeap(OfficeStaff [] currArray). Its when I get down into the code below I start getting "cant convert from "int to object etc..." errors.

Office Class


Heap Sort Methods:
 
Marshal
Posts: 69747
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I don't know the algorithm for heap sort, but I know all types of sort take object1 and object2 and compare them to see whether they are “larger” or “smaller”. So you can sort an int[] because it is obvious that ints are “larger” or “smaller” than each other. So you have to make your Employee objects “larger” or “smaller” than each other. And there are several ways to do that. I suggest you start with the Java┬« Tutorials because that section on “ordering” is very helpful. You can create Comparators which will tell you whether your Employees earn more or less, or have been working for you the longest, or to sort them in order of name. But you will find more about that in the link I gave you.

Also please check the formatting of your code. Your indentation is inconsistent, and you have too few blank lines in the first class (each method should be preceded by an empty line, for example.) You had too many empty lines in the second bit of code, but I got rid of most of them. Don't start identifiers with underscores.
And you have a bit of poor design, with public fields in the Employee class.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your Employee class first implement, comparable interface. Then in your heap sort methods you will pass an array of Comparable.
Also it would be easier if you start putting elements from index 1.

Like parent of the any number will be at (k / 2) position and childrens are 2k and 2k+1 position where k is index if you use index = 1.
 
    Bookmark Topic Watch Topic
  • New Topic