• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

QuickSort Algorithm and Generics

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to write a quicksort class, that will sort an array of other classes. it needs to sort them by an array of 6 integers. there are also a couple of other simpler sorts i'll need to do (like sort the numbers in those arrays, and sort a seperate array, that will then be used to classify the classes into 1 of 4 or 5 groups.)
so, i figured the best way to cut down code was to write a QuickSort class. now i just have to implement it generically (right?) so that it will take the classes i build, and the seperate arrays of integers.
i think i may be in over my head just a little bit.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this is your lucky day because java.util.Arrays.sort() already sorts arrays using QuickSort.



Now, as arrays in java cannot be generic, there is no generic version for the methods, but there is an implementation for every primitive data type and one for Object.
[ April 09, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that there is already a quick sort implementation in the API does little good when you have to write your own, either for an assignment or for general knowlege. Although you can look at the code and see what is going on.


A useful sorting class will not care what type of data is inside the array. As long as it implements the Comparable interface, the actual comparison is done in the class that is stored in the array, not in the sorting class.

My advice is to ignore the generics crap for now. Make sure you you got the algorthm working right, quick sort has quite a few places where it is easy to make a mistake(getting pivots, recursive quicksort calls, ect). If you are fighting generics at the same time it adds frusterations. Just make sure you accept the array as type Comparable.

If you could post what you have with specific questions, it would be easier to help out.
[ April 09, 2006: Message edited by: Rusty Shackleford ]
 
Richard Shelly
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
implements Comparable huh? awesome, will remember that.
now, as for the specific task.
i need to take an array of objects (each of which contain an array of integers), sort the integers (using whatever method i choose, going with quicksort, as its my personal favourite, it has quick in the name, how can i lose?), then after sorting the integers in the objects, sort a seperate array of integers. this seperate array is then used to compare all the objects, and thus put the objects into 4 groups.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Shelly:
...then after sorting the integers in the objects, sort a seperate array of integers. this seperate array is then used to compare all the objects, and thus put the objects into 4 groups.

You had me up until this portion right here, could you clarify what you're trying to do here.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The fact that there is already a quick sort implementation in the API does little good when you have to write your own, either for an assignment or for general knowlege. Although you can look at the code and see what is going on.



I am affraid Rusty is right.

So, I will have to ask two questions:

1. What is the purpose of this algoruthm you have to write. Is it a homework from the college? or something related to your work assigments?

2. What is actually your question? You actually did not ask anything in your post, you just told us what you want to do. Then, how can we help you?
[ April 09, 2006: Message edited by: Edwin Dalorzo ]
 
Richard Shelly
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was wondering if there was a way to do it, by writing 1 sorting class, instead of having to write code to sort integers and code to sort class objects.
reply
    Bookmark Topic Watch Topic
  • New Topic