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 3 of 3

Thread: QuickSort method

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default QuickSort method

     
    void quicksort (int[] a, int lo, int hi)
    {
    //  lo is the lower index, hi is the upper index
    //  of the region of array a that is to be sorted
        int i=lo, j=hi, h;
     
        // comparison element x
        int x=a[(lo+hi)/2];
     
        //  partition
        do
        {    
            while (a[i]<x) i++; 
            while (a[j]>x) j--;
            if (i<=j)
            {
                h=a[i]; a[i]=a[j]; a[j]=h;
                i++; j--;
            }
        } while (i<=j);
     
        //  recursion
        if (lo<j) quicksort(a, lo, j);
        if (i<hi) quicksort(a, i, hi);
    }

    Do you guys see anything wrong with this quickSort method? This will not sort.

    Example: If I type:
    2
    4
    1
    3

    I expect the results to be:
    1
    2
    3
    4

    when I hit the "sort" button. But I don't.

    Your help is appreciated!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: QuickSort method

    How are you calling the quicksort method? Please provide an SSCCE or demonstration of what the input is, how the method is called, and what the output is.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: QuickSort method

    What results do you get for the input you show?

    Try debugging your code by adding printlns to show the logic flow and how variable values change.
    Be sure to add id Strings to the prints to be able to identify them.

Similar Threads

  1. StackOverflowError when using quicksort
    By Mathew.Williams in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 22nd, 2011, 05:24 PM
  2. Quicksort algorithm displaying strange behaviour
    By Mathew.Williams in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2011, 12:56 PM
  3. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  4. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM