searching and sorting
I have a set of names to put into a vector in alphabetic order. the names are not presorted and they are added into the vector one at a time. I need a searching algorithm to search the position for each name when it is put into the vector.
I am currently using binary search. the speed can not satisfy my task. Could anyone suggest me a faster searching algorithm?
[377 byte] By [
RiekeyLeea] at [2007-11-26 13:55:25]

> I have a set of names to put into a vector in
> alphabetic order. the names are not presorted and
> they are added into the vector one at a time. I need
> a searching algorithm to search the position for each
> name when it is put into the vector.
>
> I am currently using binary search. the speed can not
> satisfy my task. Could anyone suggest me a faster
> searching algorithm?
Instead of implementing your own search algorithm, and applying it each time you're about to insert an item into the collection, why don't you instead just add all the items to the collection (unsorted), and when done doing that, invoke a flavor of Collections.sort(yourCollection) to sort it via the standard algorithm you already get for free?
I might not make may question clear.
the program is made for a j2me midlet. there is no container with sorting method in j2me. I have more than a hundred of names in memory, which need to be put into a container for display, like ChoiceGroup. concerns to memory limitation on mobiles phones, I would like to sort the names while inserting into the ChoiceGroup, rather than put them somewhere else and make copies while sorting.
Any idea?
> I might not make may question clear.
>
> the program is made for a j2me midlet. there is no
> container with sorting method in j2me. I have more
> than a hundred of names in memory, which need to be
> put into a container for display, like ChoiceGroup.
> concerns to memory limitation on mobiles phones, I
> would like to sort the names while inserting into the
> ChoiceGroup, rather than put them somewhere else and
> make copies while sorting.
By writing your own sort method which takes that Vector as a parameter, you are not creating extra copies of the elements in that Vector.