Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default Question with bubble sort

    My question is how would I sort an array of country objects by country name with this code as an example:

    I'm confused on where I would use my array name(myCountries) of type Country in this. I can provide more info if need be just let me know. Thanks


    There are 43 countries that I need to sort.

    public void bubbleSort()
    {
    int out, in;

    for (out=42; out>1; out--)
    for (in=0; in<out; in++)
    if( a[in] > a[in+1] )
    swap (in, in+1);
    }//end bubbleSort()

    private void swap(int one, int two)
    {
    String temp = a[one];
    a[one] = a[two];
    a[two] = temp;
    }//end swap()

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Where did you get the code?
    2) It already uses an array, so it would be reasonable to assume/guess/know that your array would be used in place of the "a" array.
    3) Your example code uses a "magic" number, 42. You'll need to replace that with something that is closely related to the length of your array. Can you figure out what should go there? If not, care to take a guess?
    4) You'll not be able to compare your Strings with the > or < operators, but luckily String has a compareTo(String anotherString) method that you can use instead.

    I recommend that you give it a try to come up with some code that might work, test it, and if it fails, post it in a small compilable runnable program here and we can then better help you out.

    Much luck!

  3. #3
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    The problem I have when I use my array in place of the a is that it says cannot find variable myCountries, which is my array, so I guess to start how do I go about using my array(myCountries) in place of the a?

    Sorry for the trivial questions lol

    Also the 42 is my array size so I could have used myCountries.length to take care of that

    The code is out of the book that I am using so the way I would have to go about doing the sort is going to be a little bit different than what I posted. I'm just confused as to why I can't use my array name in the method
    Last edited by Metastar; 09-13-2010 at 03:44 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Metastar View Post
    The problem I have when I use my array in place of the a is that it says cannot find variable myCountries, which is my array, so I guess to start how do I go about using my array(myCountries) in place of the a?
    If you show us your code where you attempt to use your array, show the actual error message and somehow indicate the line that causes it, we'll be better able to help you figure out what isn't work.

    Sorry for the trivial questions lol
    There's nothing trivial about trying to learn a programming language, nothing at all, but with hard work and with smart work, you can lick this and move on.

    Much luck!

  5. #5
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    How do I post compilable code on here?
    I'm looking it up as well just figured I'd post the question in case you responded before I figured it out
    Last edited by Metastar; 09-13-2010 at 04:03 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Metastar View Post
    How do I post compilable code on here?
    Well, let me correct that, since you have a compilation problem, that will be impossible, but I can't imagine that your program is all that big, so if it's less than say 100 lines long, copy the whole thing and then post it between code tags by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Much luck!

  7. #7
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ah I got ya, yea that is true that it isn't compilable hah.

    Java Code:
    import java.io.*;
    
    public class Main
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws java.io.IOException
        {
            FileInputStream fis1 = new FileInputStream("AsiaCountries.Fall2010.txt");
            BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    
            String inline;
            int totalReg1Counter = 0, totalReg2Counter = 0, totalReg3Counter = 0;
            int totalReg4Counter = 0, index = 0;
            Country[] myCountries;
            myCountries = new Country[43];
    
            while ((inline = br1.readLine()) != null)
            {
                myCountries[index] = new Country(inline);
                index++;
    
            for(int count=0; count < index; count++)
            {
                System.out.println(myCountries[count]);
            }//end for
        }//end main()
    }//end Main

  8. #8
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Java Code:
    public class Country 
    {
        private String countryName, countryCapital, countryRegion, countryAbbrev;
        private int countryRegionNum;
        public static int countryObjCounter;
        
        public Country(String countryInput)
        {
            countryName = countryInput.substring(0,17).trim();
            countryCapital = countryInput.substring(21,40).trim();
            countryRegion = countryInput.substring(44,55).trim();
            countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim());
            countryAbbrev = countryInput.substring(66,68).trim();
            countryObjCounter++;
    
        }//end parseData Constructor()
    
        public String getCountryName()
        {
            return countryName;
        }//end getCountryName()
    
        public String getCountryCapital()
        {
            return countryCapital;
        }//end getCountryCapital()
    
        public String getCountryRegion()
        {
            return countryRegion;
        }//end getCountryRegion()
    
        public int getCountryRegionNum()
        {
            return countryRegionNum;
        }//end getCountryRegionNum()
    
        public String getCountryAbbrev()
        {
            return countryAbbrev;
        }//end getCountryAbbrev()
    
        public void bubbleSort()
        {
            int out, in;
    
            for (out=42; out>1; out--)
                for (in=0; in<out; in++)
                    if( myCountries[in].compareTo(String myCountries[in+1]) )
                        swap (in, in+1);
        }//end bubbleSort()
    
        private void swap(int one, int two)
        {
            String temp = myCountries[one];
            myCountries[one] = myCountries[two];
            myCountries[two] = temp;
        }//end swap()
        
        public String toString()
        {
            return String.format("%-23s %-30s %-24s  %-16d %-4s\n",
            countryName, countryCapital,countryRegion, countryRegionNum,
            countryAbbrev);
    
        }//end toString()
    }//end Country

  9. #9
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    any ideas why I can't have my array name in there?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't put your bubble sort code in your Country class as it uses Country objects but isn't part of a Country object's innate behavior. Instead you'll put your sort code in another class, possibly even the Main class.

    Next you're using a variable, myCountry, that you never declare, never initialize, so the compiler will of course complain about this. Next, in your compareTo method, you have the type "String" in the parameter, and methods simply aren't called this way. You have the type specifier when you define a method but not when you use a method....
    Last edited by Fubarable; 09-13-2010 at 04:45 AM.

  11. #11
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ah ok, my professor said we would have our Main class, Country class, and a collection class. I wasn't sure what a collection class was, maybe you can see how it would be used here?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There ya go -- the Collection type of class probably holds the array of Country, has the bubble sort code and does the sorting and whatnot.

  13. #13
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Ok so I would create my array in the collection class, put any sorts in there, and have my Main as my driver correct? Or do I have something backwards?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    That sounds like a viable plan to me. I'd give it a go if I were you and see how it turns out. But if you're not using an IDE, be sure to compile frequently so you catch the compile errors and fix them before adding any more code.

    Best of luck!

  15. #15
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Using NetBeans so it makes my life easy on catching the errors. Thanks so much for your help :) was stuck on how to do the sort for hours lol

  16. #16
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    actually real quick, i made another class and put my Country array statement to create the new array but its still giving errors that it cant find the Country class

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Metastar View Post
    actually real quick, i made another class and put my Country array statement to create the new array but its still giving errors that it cant find the Country class
    Let's see... my crystal ball tells me that your problem is due to.... chit, it's broken. You're going to have to post code. :rolleyes:

  18. #18
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    lol I got ya

    lol I had it figured out in my head but can't get the code in the right place.
    Java Code:
    import project1cquast.Country;
    
    public class Sort 
    {
        Country[] myCountries;
        myCountries = new Country[43];
        
        public void bubbleSort()
        {
            int out, in;
    
            for (out=myCountries.length - 1; out>1; out--)
                for (in=0; in<out; in++)
                    if( myCountries[in].compareTo(myCountries[in+1]) )
                        swap (in, in+1);
        }//end bubbleSort()
    
        private void swap(int one, int two)
        {
            String temp = myCountries[one];
            myCountries[one] = myCountries[two];
            myCountries[two] = temp;
        }//end swap()
    }//end Sort
    Last edited by Metastar; 09-13-2010 at 05:47 AM.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Country is not a String. To use compareTo with Country, you have one of several options including making Country implement Comparable<Country> and giving it it's own compareTo method that compares the name Strings. Otherwise you can call getName() on your Country objects in the bubbleSort method and compare String to String. Also the swap method needs to recognize that you're swapping Country items, not String items.

  20. #20
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    For the bubbleSort() I changed the compareTo but it says it's an int. What do I need to change?

    Java Code:
    public void bubbleSort()
        {
            int out, in;
    
            for (out=myCountries.length - 1; out>1; out--)
                for (in=0; in<out; in++)
                    if( myCountries[in].getCountryName().compareTo(myCountries[in+1].getCountryName()) )
                        swap (in, in+1);
        }//end bubbleSort()
    and yea I saw I had made temp a string instead of country lol
    Last edited by Metastar; 09-13-2010 at 05:59 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 01:45 AM
  2. Trouble w/ Bubble Sort
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:41 PM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:27 PM

Posting Permissions

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