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.

Thread: Sorting arrays using a recursive binary search

1. 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. 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. 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.