# QuickSort highest lowest Situation

• 05-06-2009, 02:26 AM
Tenn
[Solved]QuickSort highest lowest Situation

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);                                 }```
Ok, I've been putting alot of these up lately, but here's the situation.

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.

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;       }```
These are the two methods, if you see something please tell me. I'm ripping out hair here.
• 05-06-2009, 02:41 AM
emceenugget
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
OrangeDog
Looks fine to me. What are the results?
• 05-06-2009, 02:46 AM
Tenn
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
Tenn
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
OrangeDog
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.
• 05-06-2009, 03:05 AM
Tenn
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
Tenn
Results from the code posted up top.

Quote:

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

Thats not the entire array. its so big it locks up the paste method. Lowest didn't produce anything, highest would. now they bothw ork
• 05-06-2009, 04:09 AM
OrangeDog
So use a smaller array. Printing the highest and lowest values on a separate line would make it more readable.
• 05-06-2009, 04:15 AM
Tenn
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
OrangeDog
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.
• 05-06-2009, 04:21 AM
Tenn
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
OrangeDog
Quote:

Originally Posted by Tenn
They're set to print on a new line.

Not in the code you posted they're not. println() adds a new line after printing the string, not before.
• 05-06-2009, 04:28 AM
Tenn
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
OrangeDog
No it wasn't.
• 05-06-2009, 04:32 AM
Tenn
ok. I'm lieing I didn;t use

println insted of print.
• 05-06-2009, 04:36 AM
OrangeDog
I assume that's sarcasm. Try this example and you'll see.

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.");     } }```
• 05-06-2009, 04:37 AM
Tenn
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.