(06-20-2011, 10:46 PM)Koopaul Wrote: Yeah I have a Java programming online course, I've been doing decent but now we're at Arrays and I'm starting to get confused.
Anyway I need help figuring out the value of the num array with this program. I think I have an idea but I'm not sure. Take a look.
I tried to provide the answer stepwise, such that you can try and figure out the rest yourself after a certain step, or go to the next one if you are stuck.
The 'thumb' and 'finger' metaphores I use below are what I've been taught with, and correspond to how you could do it on paper (which usually helps with these kind of problems).
Code:
// creation of an array with some initial values.
// The i'th value is accessed using num[i] starting at 0, so num[2] is currently 10
int [] num= {20,50,10,70,40,80,30,90,60};
int t=0;
// place the thumb on each position in the array, 'from left to right'
for (int i=0; i<num.length; i++) {
// go with the finger through the rest of the array
for (int j=i+1; j<num.length; j++) {
// whenever the value at the finger is less than the value at the thumb, do something
if (num[i]>num[j]) {
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
[spoiler=determine what 'do something' does]
I could just tell you what the 'do something' is, but it may be more helpful to try and to this with some paper;
- write a couple of numbers (or any other type of value of course) next to each other in boxes (a horizontal ladder, as it were); this is the array
- for convenience, you could write the index of each element in the array above/below it. Be sure to start with 0.
- draw an empty box somewhere else on the paper. This represents the value of '
t'. (you could fill it with a 0, as it's t's initial value)
- pick any value for i and j, for which i < j and both i and j are valid indices of the array (the array should have a number at both i and j).
- the perform the 'do something' on the paper;
[spoiler=analogue version of 'do something' sequence]-- erase the value at t, and replace it with the value of the i'th element in the array
-- erase the value at the i'th element in the array, and replace it with the value of the j'th element in the array
-- erase the value at the j'th element in the array, and replace it with the value of t[/spoiler]
- If you remember the original set of values of the array (or if you wrote them down / re-used the example array from the exercise), you should be able to see what has just happened. (you can ignore the value of t for that; it's the t of Temporary, and is only used to enable the operation that is being performed in the 'do something' part)
[spoiler=what 'do something' does]-- It swaps the values at location i and j in the array[/spoiler]
[/spoiler]
Once you know what 'do something' does, the next step is figuring out what it does to the array when used in the program above.
[spoiler=hint 0]- the value at the thumb is swapped with the value at the finger whenever the value at the finger is less than the value at the thumb[/spoiler]
[spoiler=hint 1]- Will the value at the thumb ever decrease?
[spoiler=answer]No, it is only swapped when the thumb is higher than the finger.[/spoiler][/spoiler]
[spoiler=hint 2]- Will there be a value 'to the right of' the thumb that is smaller than the thumb?
[spoiler=answer]No. The thumb is always the minimum value between the thumb and the finger (inclusive).[/spoiler][/spoiler]
[spoiler=hint 3]- Since the thumb is moved from left to right, this, means that for every element in the array, all values to the right will be ...[/spoiler]
[spoiler=hint 4]- ergo, the values are not decreasing when read from left to right.[/spoiler]
[spoiler='hint 5'/answer]- ergo, the array is sorted in ascending order[/spoiler]