There are a few things that you can do. It depends on what you are sorting. If its numbers, then the best would be a Heap Sort (one of the fastest sorting algorithms). Here is a link to Wikipedia http://en.wikipedia.org/wiki/Heapsort and it has pseudo-code on it. Its easy to implement and understand.
If its your own custom class that you are sorting, what you SHOULD do is extend the Comparable class, and you have to "fill in" the .compareTo() method. The compareTo() method returns 1 if the first object is greater than the second, 0 if they are equal and -1 if the first is less than the second. In there you have to determine what conditions one object is "less than" or "greater than" or "equal" to another of your objects. Here is the Javadoc for Comparable object. It should help you out http://java.sun.com/j2se/1.4.2/docs/...omparable.html
Of course there are other sorting algorithms such as selection sort, bubble sort and a few others. Here is Wiki's list of sorting algorithms http://en.wikipedia.org/wiki/Sorting_algorithm I would suggest that you look at the Summaries Section on that page.
There are a few things that you can do. It depends on what you are sorting. If its numbers, then the best would be a Heap Sort (one of the fastest sorting algorithms). Here is a link to Wikipedia http://en.wikipedia.org/wiki/Heapsort and it has pseudo-code on it. Its easy to implement and understand.
For more effective output you can use "Divide and Conquer" algorithm.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
I don't want to start another discussion here. But you should be able to pick a sorting algorithm according to the length and size of the data you are sorting.
The implementation shouldn't be a problem. but anyway, I've attached a zip file tha contains a project full of sorting algorithms. you might want to look up each algo's performance on wikipedia or any source you like
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson My Blog
I don't want to start another discussion here. But you should be able to pick a sorting algorithm according to the length and size of the data you are sorting.
The implementation shouldn't be a problem. but anyway, I've attached a zip file tha contains a project full of sorting algorithms. you might want to look up each algo's performance on wikipedia or any source you like
I'll check it and put my thoughts later.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Use good old Bubble sort or insertion sort or merge sort. Bubble sort is easy to implement although not the most efficient.
Bubble sort is much similar to Divide and Conquer, I think. But there is one thing is difficult handle. If ther is odd number of elements, we can't divide them exactly in to two parts. So we have to use a dummy value. It can make errors.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Bubble sort is much similar to Divide and Conquer, I think. But there is one thing is difficult handle. If ther is odd number of elements, we can't divide them exactly in to two parts. So we have to use a dummy value. It can make errors.
Bubble sort isn't similar to divide and conquer ... Bubble sort just goes down the list comparing values, and switch them if need be... If it gets to the end it starts from the beginning until the list is sorted.
And with divide and conquer having odd or even elements isn't too big of a deal. I've never seen an implementation w/ a dummy value, if there is an odd amount, the programmer decides which side the middle item should go.
And with divide and conquer having odd or even elements isn't too big of a deal. I've never seen an implementation w/ a dummy value, if there is an odd amount, the programmer decides which side the middle item should go.
There is no huge implimentation. Just divide until there are less number of elements on a one side. I used a dummy value to find the solution, depending on the other element types and its value.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha