|
Post by gabbie on Dec 12, 2001 18:39:37 GMT -5
If any one is actually free can they please explain the coding for sorting for me please... ~!!!! it is really confusing to me.. selection sort... public static void selectSort(double[ ]list){ for(int top= list.length-1; top>0; top--){ double largest = list[0]; int largeLoc = 0; for (int i =1; i<=top; i++){ if(list >largest){ largest = list; largeLoc = i; } } list[largeLoc] = list[top]; list[top] = largest; }
|
|
|
Post by Evotamer on Dec 12, 2001 20:45:12 GMT -5
Heh.. there was a bracket missing at the end but anyways suppose the code now looks like this:
public static void selectSort(double[ ]list){ for(int top= list.length-1; top>0; top--){ double largest = list[0]; int largeLoc = 0; for (int i =1; i<=top; i++){ if(list>largest){ largest = list; largeLoc = i; } } list[largeLoc] = list[top]; list[top] = largest; } }
You basically have an array of doubles named list Let's say you have an array of size 10. So you have list[0], list[1], ... list [9]
The first loop is just changing the length of the array that you are sorting. First you sort 10 things, then 9 then 8 etc...
Now you have double largest = list[0]; It's just taking the first double in the array and letting largest equal to that. Similarily for int largeLoc = 0; we're just telling ourself where the largest double was found.
The following for loop goes from i = 0 to top. Now remember top is from the other loop so the first iteration you're going through list[0], to list [9]. The next iteration is going from list[0], to list [8] etc...
Now here comes the IF statement... If the spot in the array is larger than the largest number then you just let largest equal that value, and largestLoc be the location in the array that you found the largest. So this keeps searching for the largest number and where it's stored... YAY ... okay now that 2nd for loop finishes
Now you have: list[largeLoc] = list[top]; list[top] = largest; Now you're letting the location of where you found the largest number to be equal to the value at the top... and you're letting the top be the value of the largest. So what you're really doing is swaping the top location with the largest number.
This help at all?
|
|
|
Post by gabbie on Dec 12, 2001 20:48:46 GMT -5
THANKS ~!! ALL CLEAR NOW I FINK .. this same method applies to vectors hehe...btw.. do you knoe wat we should study 4 ? 4 da exam ?? ;D
|
|
|
Post by Evotamer on Dec 12, 2001 20:53:16 GMT -5
Ummm No I don't know... I'm supposed to be writing up my aid sheet for my 238 / 270 test but aparently I'm procrastinating... Vectors are different than Arrays... So umm... This won't apply as well with Vectors...
|
|
|
Post by gabbie on Dec 12, 2001 21:03:46 GMT -5
|
|
|
Post by Evotamer on Dec 12, 2001 21:13:27 GMT -5
Vectors are made with Linked Lists basically... So it's a bit more efficient in the way that if you declare an array of size 100 and you only use 50, you don't waste memory. Vectors allow you to add things one by one. But they use slightly different calls (enumerations etc..). I believe you need to cast stuff when using vectors.
All objects have a default toString() method.. but well that information might be just jargon. So if you're making your own object (That's not a string, double, float, int etc...) and you use system.out.println(yourObject); You might ahve some thing like 3284BC34982 instead of something that you wanted like maybe "This is an object of type blah".
So yes you would have to have something in your class saying: public String toString () { return (what_you_want_it_to_print); }
|
|
|
Post by gabbie on Dec 12, 2001 21:16:08 GMT -5
argh..... do me a favor... read pg.576 in the textbook.. they have a toString method there.. but .. sigh.. i thought i understood it.. but .... HELP ~!! EXAM IN 10+HRS
|
|
|
Post by Evotamer on Dec 12, 2001 21:16:53 GMT -5
Uhh I'm not taking 108... so I don't have the textbook sorry
|
|
|
Post by gabbie on Dec 12, 2001 21:21:17 GMT -5
it says this
public String toString () { return elements.toString(); }
elements referring to the vector's name
|
|
|
Post by Evotamer on Dec 12, 2001 21:24:44 GMT -5
You're just doing an extra call to the elements.toString(). So in elements you have a toString() method that returns a 'useful' String. So there's a toString method within elements that's being called.
|
|
|
Post by gabbie on Dec 12, 2001 21:27:03 GMT -5
btw.. wat are you taking ? y are you so interested in 108 ?? you are rilly helpful ~!!!! =)
now... how can i code an insertion sort ??
.... wow... people are actually looking at these things.. the # of ppl viewed is going up ~!! woohoo.. i feel proud..hehe
|
|
|
Post by Evotamer on Dec 12, 2001 21:31:08 GMT -5
Oh I was taking Sta107 which I umm bombed the final.. so I was just glacing at the 108 stuff... I'm in 2nd year taking some useful/less junk...
Umm Insertion sort.. well I don't know it off hand...But don't spend too too much time on Sorting as it's just a small part of what they might test you for.
|
|
|
Post by Evotamer on Dec 12, 2001 21:43:25 GMT -5
IF you need more help just ICQ me at 1898653... I'm on that account ATM.
|
|