Results 1 to 5 of 5
Thread: Need help
- 01-18-2011, 08:41 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
Need help
I have written a code to sort an array :
I can use Arrays.binarySearch(arr, 9) only when my array is ascending order.Is there any api in java which allows me to use without sorting the array in ascending order.Java Code:int arr[] = {95, 20, 34, 1, 9, 78, 3}; int index = 0; Arrays.sort(arr, 0, arr.length); System.out.println(Arrays.toString(arr)); index = Arrays.binarySearch(arr, 9); System.out.println("The index of 9 is:" + index);
- 01-18-2011, 09:10 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I cant see anyway binary search can work on a non sorted array, if you jump to halfway through an array how do you know whether you should go left or right if both sides can have numbers greater than and less than the middle element.
- 01-18-2011, 09:25 PM #3
The only way to search an unordered list is a linear search. Just think about a phone book. How would you find someone's name if it was not sorted alphabetically?
- 01-18-2011, 10:06 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
- 01-18-2011, 10:52 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks