Results 1 to 18 of 18
  1. #1
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default [Solved]QuickSort highest lowest Situation

    The problem was bad syntax

    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);
                      
                   }
    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.

    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;
          }
    These are the two methods, if you see something please tell me. I'm ripping out hair here.
    Last edited by Tenn; 05-06-2009 at 05:42 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  4. #4
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  5. #5
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  7. #7
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  8. #8
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Results from the code posted up top.

    please 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
    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

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  10. #10
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  12. #12
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  13. #13
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by Tenn View Post
    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.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  14. #14
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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.

  15. #15
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  16. #16
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    ok. I'm lieing I didn;t use

    println insted of print.

  17. #17
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  18. #18
    Tenn is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    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

  1. What should the program look like for this situation
    By megironi in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 12:16 PM
  2. Replies: 21
    Last Post: 11-05-2008, 03:31 PM
  3. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 02:39 PM
  4. Quicksort
    By little_polarbear in forum New To Java
    Replies: 12
    Last Post: 07-12-2008, 10:20 PM
  5. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •