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.


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

Results 1 to 2 of 2

Thread: Arrays.sort or iterative search?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Thanked 0 Times in 0 Posts

    Default Arrays.sort or iterative search?

    I have a large integer array of mixed values I want to search for the highest and lowest values. I have a method to iterate through each element in the array. I was thinking Arrays.sort() might be faster, however I need to retain the order of the elements in the array.

    Would it be more efficient to:
    a) Stick with the iterative method
    b) Make a copy of the array, sort and then take the first and last elements and lowest and highest respectively

    Any suggestions much appreciated

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Arrays.sort or iterative search?

    Have you heard of the QuickSelect algorithm? It's quite good at finding the min/max (I think in practice it's about O(log(n)) average, O(n) worst case).

    If you only need a few min/max value, I'd say stick with the quickselect algorithm. Otherwise, it may just be better to use the full quicksort routine (which is probably the algorithm used by Array.sort()).


    I was mistaken. The best way to find the #1 min/max is to iterate. The best way to find the #nth min/max is to use quickselect, or quicksort if you need to find a lot of min/max values.
    Last edited by helloworld922; September 16th, 2009 at 02:20 AM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    igniteflow (September 16th, 2009)

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM
  3. How to highlight search keyword in text?
    By Mohd in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: February 1st, 2009, 06:35 AM
  4. Implementation of search function for Patricia Trie
    By javaguy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2008, 01:54 PM