Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sorting in Java:

  1. #1
    Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    328
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Question Sorting in Java:

    I have been looking around and I cannot seem to find which algorithm java uses for its sort method. Merge, sort etc.

    While I am on the topic BinaryTree is a balanced heap right?

    Are there any libraries for depth or breath first search?

    Is the ArrayList a double linked list?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,375
    My Mood
    Mellow
    Thanks
    254
    Thanked 787 Times in 772 Posts

    Default Re: Sorting in Java:

    These sound like test or homework questions. Answers are out there for you to find, and I don't think it would take much effort.

    If you want to find what sort algorithm Java uses, read the source code.

    Search libraries? What did you find when you searched for them?

    ArrayList a doubly linked list? What did you learn when you read the ArrayList API or the ArrayList source code?

  3. #3
    Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    328
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Sorting in Java:

    No it has nothing to do with homework. I am learning Java on my own. I most familiar with C++ in which you are able to easily call certain sorting function. When I looked at Java tutorials it just showed me examples of how to do merge or quick sort which I am not interested in. I already know how to implement them, it deciding which one to use with an easy access library or method was my question.

    I am sure people come here often for answers to the test so I understand your initial impression.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    916
    Thanks
    0
    Thanked 170 Times in 151 Posts

    Default Re: Sorting in Java:

    The answer is that you should not care what kind of sorting algorithm is used. Its possibly changing with every java update.
    What is important is that the time needed for the sorts is no slower then O(n * log n).

    By the way, if you read the documentation it will tell you that:
    The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.

  5. #5
    Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    328
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Sorting in Java:

    Not caring what it is doing is like not caring about how efficient your program is. Granted computers are very fast now and therefore I see where the habit of just looking at the Big O has become the norm. However it would be nice to understand what is going on under the hood and having the choice of which method of sorting I choose to use.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    916
    Thanks
    0
    Thanked 170 Times in 151 Posts

    Default Re: Sorting in Java:

    If you want to choose the method yourself dont use the standard library. It does not give you any choice.
    If your application needs that tiny little bit of performance that much that then you should write the sorting algorithm yourself anyways.

Similar Threads

  1. need help with sorting
    By shad0wblade890 in forum Object Oriented Programming
    Replies: 5
    Last Post: March 21st, 2014, 02:30 PM
  2. [SOLVED] Java: Sorting int array in asc and desc
    By Abyd Ars in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 7th, 2013, 09:37 AM
  3. Basic Java Sorting Algorithm (Selection/Insertion) help
    By Arte7 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 22nd, 2011, 02:38 PM
  4. 2 Java methods and a sorting algorithm
    By Tiberius in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 26th, 2011, 03:41 PM
  5. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM