Descending Order Problem

  • Thread starter Robert Scheaffer via JavaKB.com
  • Start date
R

Robert Scheaffer via JavaKB.com

Hello everybody!

I am completely lost with this. I am a newbie with Java and am stuck on how to change this file to descending order?


import java.util.Random;

public class BinarySearch
{
public static void main(String[] args)
{
// The first command line argument specifies the length of the data list.
int n = Integer.parseInt(args[0]), searchValue, searchLoops = 0, foundLocation = -1, first = 0, last = n - 1, mid;

// Create a data list of integers with n entries, with values in ascending order between and including 0 and n-1.
int[] dataList = new int[n];
for (int i = 0; i < n; i++) dataList = i;

// Randomly choose a search value as an integer between and including 0 and n-1.
Random generator = new Random();
searchValue = generator.nextInt(n);

// Check the first entry in the list to see if it equals the search value.
while (first <= last)
{
searchLoops++;
mid = (first + last)/2;
if (searchValue == dataList[mid])
{
foundLocation = mid; // searchValue is found
System.out.println("Found search value = " + searchValue + " in " + searchLoops + " while loops at index = " + mid);
return;
}
else if (searchValue < dataList[mid])
last = mid - 1; // searchValue is in the first half
else
first= mid + 1; // searchValue is in the second half
}

System.out.println("The search value of " + searchValue + " isn't in the data list. Performed " + searchLoops + " while loops.");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top