Results 1 to 18 of 18
- 05-06-2009, 02:26 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
[Solved]QuickSort highest lowest Situation
The problem was bad syntax
Ok, I've been putting alot of these up lately, but here's the situation.Java Code:case('2'): { for (int index = 0; index <count; index++) list[index] = (int) (Math.random() * 100000); low=getLowest(list); high=getHighest(list); for(int index=0; index<count; index++) System.out.print(list[index]+ " "); low=getLowest(list); System.out.println(low); }
We have to quicksort with median and middle. So, this is the case where it will eventually use the median value as the pivot. This is the general problem, It will pick up the highest or the lowest value, currently its only picking up the highest, insted of both. Before that it was picking up the lowest only.
Though the methods are both seperate, and use local veriables thtat are set to what they need to be independent of eachother. So before I can figure my way through the quick sort itself, I was wondering fi someone could see something wronge with the code that I've missed.
These are the two methods, if you see something please tell me. I'm ripping out hair here.Java Code:public static int getLowest(int x[]) { int min; min = x[0]; for (int j = 0; j < x.length; j++) { if (min > x[j]) min = x[j]; } return min; } public static int getHighest(int x[]) { int i; int max; max = x[0]; for (i = 0; i < x.length; i++) { if (max < x[i]) max = x[i]; } return max; }Last edited by Tenn; 05-06-2009 at 04:42 AM.
- 05-06-2009, 02:41 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
first of all, i would recommend a single method that finds both highest and lowest within an array so that you don't have to iterate over the array twice, or is that not what you want to do? sorry, i'm a little confused on what you're really trying to do as it seems like you aren't doing any sorting or array manipulation at all... just getting the highest/lowest and printing out the lowest.
- 05-06-2009, 02:45 AM #3
Looks fine to me. What are the results?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 02:46 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Ah if you saw the rest of it you'd see the sorting itself.
I have the sort methods set up in the other parts of the class beyond what you see here, LIke case 1 is a basic quick sort and works just fine. what your looking at is trying to find the values so I can get hte quicksort to impliment correctly. Though Iteration at this point isn't something i'm so worried about, I have to have it working by midnight tomorrow so at this point even if its a little slow and works great, I'll clean it up and make it faster on my own time.
- 05-06-2009, 02:48 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
mmm currently Orange it prints the high value, I think the last time I did it it was some 9000 number.
It displays the numbers sense I'm working on it currently.
then it displays the highest value.
no lowest value.
- 05-06-2009, 02:50 AM #6
That code you posted prints the highest value?
Post some code that is supposed to print both the highest and the lowest and then post the output.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 03:05 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Actually that one posted printed nothing, because the lowest was the one not printing anything, It just printed the 10000 numbers in the array.
but now it gets the highest and the lowest, which is what it wasn't doing in the first place.
Now its just getting it to sort properly, I'm going to leave this thread open until I complete it and I'll put the stuff I know is what solved it up.
as of right now I'm pretty sure the person in my class and I who have been working togeather used basically the same codes to get the highest and lowest, I'm going to dig through what was handed to me and see if I can locate a difference.
- 05-06-2009, 03:11 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Results from the code posted up top.
Thats not the entire array. its so big it locks up the paste method. Lowest didn't produce anything, highest would. now they bothw orkplease select a sort method.
1. quickSort. uses Middle
2. quickSort1. uses median
3. quickSort2. uses Middle with insertion.
4. quickSort3. uses Median with insertion.
2
90794 82326 78490 79022 82252 58220 63682 72292 55196 83223 37794 60832 99335 53158 27195 51558 45307 67199 11714 30976 3236 86881 28443 88670 98796 33471 15928 83479 86232 80925 83146 52413 44066 9942 9228 78449 1113 37563 67122 40106 97955 26306 17201 63054 62142 7015 35274
- 05-06-2009, 04:09 AM #9
So use a smaller array. Printing the highest and lowest values on a separate line would make it more readable.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 04:15 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
the requirements is a 10000 unit array, I'm not real sure the point in that one, and probably after I get it to where it works I'll set it up to print on every line.
- 05-06-2009, 04:19 AM #11
Yes, but while you're testing set it to 10 or something, so you can actually see what's going on. I would guess that your methods actually do work, you just can't tell because the arrays are too long and the results are just tacked on the end.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 04:21 AM #12
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
They're set to print on a new line. And that may be the case. Thanks for the input, I've got to set it up to where when it gets below 20 now it uses insertion sort insted of quicksort.
- 05-06-2009, 04:27 AM #13
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 04:28 AM #14
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
I meant that the lowest value was set to print to a new line. Not the array itself. I know the array isn't. And it might have been that too.
- 05-06-2009, 04:29 AM #15
No it wasn't.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 04:32 AM #16
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
ok. I'm lieing I didn;t use
println insted of print.
- 05-06-2009, 04:36 AM #17
I assume that's sarcasm. Try this example and you'll see.
Java Code:public class NewLine { public static void main(String[] args) { System.out.print("This line "); System.out.println("runs on."); System.out.print("This one starts on a new line "); System.out.print("and continues until-"); System.out.print("\n"); System.out.println("you start a new one."); } }Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 04:37 AM #18
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
ok...well it seems the only problem I had was I put my code in wrong and didn't see the results where they where supposed to be.
Thank you for pointing that out. Sorry about hte sarcasum you where right thats what it was. And now that program works completely. So thank you, I'll remember that next time. and All it was was bad sentax.
Similar Threads
-
What should the program look like for this situation
By megironi in forum New To JavaReplies: 4Last Post: 02-22-2009, 11:16 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
StringBuffer situation
By orchid in forum New To JavaReplies: 6Last Post: 08-12-2008, 01:39 PM -
Quicksort
By little_polarbear in forum New To JavaReplies: 12Last Post: 07-12-2008, 09:20 PM -
The highest number
By I-Shine in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks