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: Sorting arrays using a recursive binary search

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting arrays using a recursive binary search

    How would I use a recursive binary search to sort a list of unsorted numbers e.g.

     5 29 17 3 6 43

    My code for the recursive binary search is....

        public static int bSearch(int[] a, int lo, int hi, int key)
    	    {
    		    int mid = (lo+hi)/2;
    		    if(lo>hi)
    		        return -1;
    		    else if (a[mid]==key)
    		        return mid;
    		    else if (a[mid]<key)
    		        return bSearch(a, mid+1, hi, key);
    		    else
    		        return bSearch(a, lo, mid-1, key);
    	    }

    My insert in order method would be....
        static void insertInOrder( int[] arr, int cnt, int newVal )  
         {
          int index = -( bSearch( arr, 0, arr.length-1, newVal)) - 1;
           //Stuck here ********************
         }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sorting arrays using a recursive binary search

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Sorting arrays using a recursive binary search

    recursive binary search
    does it mean that you are not allowed to use a loop? maybe it is an assignment, i guess.

    My insert in order method would be....
    do you really need another method for interchanging the values?

    do you know how to sort an array using loop with nested loop?
    i think it is called a bubble sort.
    I think it would be easier to explain how to do that if you at least understand how to create a bubble sort program.

Similar Threads

  1. Sorting Array & Binary Search Help
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2013, 09:53 PM
  2. Depth-Limited Search using Recursive Backtracking...please help!
    By Zaxx81 in forum Algorithms & Recursion
    Replies: 26
    Last Post: June 20th, 2012, 04:21 PM
  3. Binary Tree Longest Increasing Path (Recursive)
    By varsis in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 13th, 2012, 10:19 PM
  4. Decimal To binary Recursive conversion java?
    By sirdonclemo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:33 AM
  5. recursive search of all local disks
    By ttsdinesh in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 08:23 AM