I want to sort a vector contains like [a,b,1,3,5,z] both ascending and descending on Java ME, i.e. without using function like Collections.sort()
4 Answers
Exchange sort in 3 sentences:
- Find the smallest item in the vector, and exchange it with the first element in the vector.
- Sort the rest of the vector, i.e. pretend your vector starts at the next element after the first one (or whichever one you just did).
- If there's no more "rest of the vector" because you've just allocated the last position, you're done.
Comments
If it's a vector you can have a look at this example:
http://www.java-examples.com/sort-java-vector-descending-order-using-comparator-example
1 Comment
Jesper
"With out using function like Collections.sort() ..."
Copy the implementation of Collections.sort(), paste and modify it so much that you will be able to claim that you have "only been inspired" by it.
It's not cheating, it's learning from the chosen implementation.
5 Comments
Ron Tuffin
not so that you can cheat. but it is always a good exercise to go to the source code to find out how things are actually done.
Joel Shemtov
If you are asked, for whatever reason, to re-invent the wheel, what would you do? Check how the wheel has been invented in the first place and do the same - good professionals do exactly that
Paul Wagland
a) This is obviously a homework answer, not a "real world" question, telling them to copy the code is not helping them to learn. b) In the real world you might use it as a source of inspiration, but do you really want to expose yourself and your company to the legal nightmares of copyright infringement?
Joel Shemtov
I guess we'll never agree on that. Anyway nothing is more "real" for Michael than his homework. Chances are that he'll take my advise, and if so, he'll get a best practice how to solve a problem.
Gareth Davis
assuming it's not homework.. it's a perfectly valid thing to do. What else are you doing to do? waste an hour or two implementing and testing a sort algorithm from Wikipedia or something, just cut&paste Collections.sort()
Collections.sort(..)? Homework??