Help With Printing Integers
Author |
Message |
Programming_Kid
|
Posted: Fri Oct 30, 2009 12:01 pm Post subject: Help With Printing Integers |
|
|
So this is a very easy question, but I just can't seem to understand how to figure this out. I am trying to print out some integers, after they have been sorted, but I don't know what to do.
This is the code I have for my code so far.
The final System.out.println that has been made into a comment is what I believe I need, but I'm missing the code that will only use the sorted numbers.
Any help will be greatly appreciated.
Thanks
import java.util.Random;
public class BinarySearch {
public static void main(String[] args){
int y = 0; //Array variable to generate 30 numbers
int randomInt; //Variable to generate random numbers
Random randomGenerator = new Random();
int linArray [] = new int[30]; //Generated array alloted with 30 slots
boolean swap; //Variable used in the swap method
//Creates a set amount of number slots, along with random numbers to fill those slots
for (y = 0; y<30; y++){
linArray [y] = randomGenerator.nextInt(50);
}
//Counts and orders the randomly generated array numbers
do{
swap = false;
for(int i = 0; i<29; i++){
if (linArray[i] > linArray[i+1]){
int temp = linArray[i];
linArray[i] = linArray[i+1];
linArray[i+1] = temp;
swap = true;
}
//System.out.println(linArray[i]);
}
}
while(swap);
}
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jbking
|
Posted: Fri Oct 30, 2009 1:31 pm Post subject: Re: Help With Printing Integers |
|
|
A few thoughts:
1. Why are you trying to put a print in with the loops? Do the sort and then print the results in separate loops makes more sense.
2. If you consider the case where you'd have a lot of swaps, e.g. a list that is in reverse order, then that first value to print out isn't going to be in the first place until the end now is it? (Consider a short list of only a few values and trace through your program and see what happens.)
3. The commented out line contains a variable that isn't defined at that point. i is defined in the for loop and isn't to be used outside unless you declare it out one scope level.
4. Where is "randomInt" actually used?
5. Why is the class called "BinarySearch" if you are sorting an array?
HTH,
JB |
|
|
|
|
|
|
|