user3287300
user3287300

Reputation: 151

Sorting arrays using a recursive binary search

So I am reading in a txt file with a list of unsorted numbers...

14 36 9 87 2 5

My recursive method for the binary search is...

public static int bSearch(int[] a, int lo, int hi, int key)
 {
    int mid = lo+(lo + hi)/2;

    if (lo <= hi) 
        return -(lo+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);
}

I want to sort the values by implementing the recursive binary search. Can someone point me in the direction on how I would go about doing this.

Upvotes: 0

Views: 570

Answers (3)

Alik Khilazhev
Alik Khilazhev

Reputation: 1117

Binary search algorithm uses for searching.I guess you mixed up binary search with quicksort algorithm

Upvotes: 0

vivekpansara
vivekpansara

Reputation: 899

It is possiable to sort using binary search. Take a look: Binary Searching and Sorting

Upvotes: 0

user3297129
user3297129

Reputation: 299

Why use binary search for sorting? If you are really looking for divide and conquer strategy, please have a look at merge sort.

Upvotes: 1

Related Questions