Results 1 to 12 of 12

Thread: Array Element Sorting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Array Element Sorting

    what i want to do is sort elements in an array
    without using the .sort method

    i've googled around but no luck.

    Could any one point me in the right direction, or show me a sample.

    I'm extremely new to java, only just got to grips with vb(a) lol

    thanks

  2. #2
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: Array Element Sorting

    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.

  3. #3
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Array Element Sorting

    Quote Originally Posted by fartman_900
    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

  4. #4
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Array Element Sorting

    What's wrong w/ the .sort method?

  5. #5
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Array Element Sorting

    Nothing to worry about that.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array Element Sorting

    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
    Attached Files Attached Files
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Array Element Sorting

    Use good old Bubble sort or insertion sort or merge sort. Bubble sort is easy to implement although not the most efficient.

  8. #8
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Array Element Sorting

    Quote Originally Posted by ComputerJy
    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

  9. #9
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Array Element Sorting

    Quote Originally Posted by xxarmoxx
    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

  10. #10
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Array Element Sorting

    Quote Originally Posted by eranga262154
    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.

  11. #11
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Array Element Sorting

    Quote Originally Posted by pgag45
    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

  12. #12
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Array Element Sorting

    Actually, bubble sort uses a complex system of levers and pulleys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width