Results 1 to 13 of 13
  1. #1
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default Help with a long sorting program.

    The assignment asks for a program that sorts a 1D int array, 2D int array, 1D double array, 2D double array, 1D char array, 2D char array, 1D string array and 2D string array using selection sort, bubble sort and another sort of your choice.
    The program also asks for a nice menu to keep things organized and clean. I already know how to make the menu for this: 2 layers of menu, first have them pick what kind of sorting they want, then choose which kind of array to sort, after the second selection has been made, the array is made based on the selection. However, the things I am baffled by are:
    1. Code efficient way of telling the sorting function what kind of sort and array the user has chosen.
    2. How to sort chars and strings.
    3. Another method of sorting other than bubble and selection.

    I don't know exactly how to do bubble and selection sorting but I think i can look that up myself. Any help would be greatly appreciated.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    don't really know this subject too well, but i can give you a few suggestions for your third question.
    quick sort, merge sort, heap sort, insertion-sort (recommended).
    you can find out more about them in wikipedia.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    Thanks for the ideas angryboy. I'm mainly trying to get past number 1 because without it, I'm reluctant to move on.

  4. #4
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    Umm... I tried this code for the menu of the program. However, I can't seem to pass the array variable.

    Java Code:
    static Console c;
        public static void main (String[] args)
        {
            c=new Console();
            int choice, choice2;
            do
            {
                c.print("MENU");
                c.print("1.  1D Int Array");
                c.print("2.  2D Int Array");
                c.print("3.  1D Double Array");
                c.print("4.  2D Double Array");
                c.print("5.  1D Char Array");
                c.print("6.  2D Char Array");
                c.print("7.  1D String Array");
                c.print("8.  2D String Array");
                do
                {
                    c.println("Make a selection: ");
                    choice=c.readInt();
                    if ( (choice < 1) || (choice > 8) )
                    c.println("Invalid choice please enter 1 - 8.");
                }
                while ( (choice2 < 1) || (choice2 > 8) );
                if (choice==1){int[] array = new int[5];break;}
                else if (choice==2){int[][] array = new int[5][2];break;}
                else if (choice==3){double[] array = new double[5];break;}
                else if (choice==4){double[][] array = new double[5][2];break;}
                else if (choice==5){char[] array = new char[5];break;}
                else if (choice==6){char[][] array = new char[5][2];break;}
                else if (choice==7){String[] array = new String[5];break;}
                else if (choice==8){String[][] array = new String[5][2];break;}
                do
                {
                    c.clear();
                    c.println("MENU");
                    c.println("1.  Selection Sort");
                    c.println("2.  Bubble Sort");
                    c.println("3.  Exit");
                    c.println("4.  Exit");
                    do
                    {
                        choice2=c.readInt();
                        if ( (choice2 < 1) || (choice2 > 4) )
                        c.println("Invalid choice please enter 1 - 4.");
                    }
                    while ( (choice2 < 1) || (choice2 > 3) );
                    if (choice2==1){c.clear();bubble([b]array[/b]);}
                    else if (choice2==2){c.clear();selection([b]array[/b]);}
                    else if (choice2==3){c.clear();bubble([b]array[/b]);}
                }
                while (choice2!=4);
            }
            while (choice !=4);
            c.println("Program Closed.");
        }
    The bolded part is where its giving me a "no field name found" error. I think that the variable array is not recognized when it is created in a if statement.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    that because you declared the array inside the if statements. i suggest using intArray, charArray, etc...
    also look into switch-case statements.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    I've decided to do the sorting and method selection within separate classes and call the appropriate classes within the main function's menu. Problem is I'm not familiar with the code to call new classes.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    just make a field and use the class as is. e.g. public MySortClass c = new MySortClass();
    then just use: c.selection(array);
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    I'm having trouble naming my classes I know I would have a base class that does the sorting and the method selection. Then i would have subclasses off of that base class that is used for the different types of arrays. But what would the code look like?

    Java Code:
    public class sortbase
    {
           public int selection
           {
                 //Code for inputting method choice.
           }
           //Code for sorting
    }
    public class intsort: sortbase
    {
          //Code for sorting integers.
    }
    public class dintsort: sortbase
    {
          //Code for sorting 2D integers.
    }
    public class charsort: sortbase
    {
          //Code for sorting chars.
    }
    
    etc...
    But how would I connect the subclasses with the base class? What would be the most code efficient way of doing this?
    Last edited by leiferouis; 02-02-2009 at 05:44 AM.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    do some research on relationships (has-a, is-a, etc...). in other words, you can extend the class, or use it as in the above post #7.
    i suggest making a sort class, and a main class that ask for user inputs, then pass params to the sort class methods.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    Okay. Thanks for the help. I've pretty much figured out exactly what this would look like. Now I just have to code. One question about the code itself.
    I have something like this:
    Java Code:
    //In main function
    static sortclass s = new sortclass();
    //random code
    String[][] array=new string[5][2];
    //random code
    array=s.sort(array, 5,2);
    //output code
    
    //In sortclass class
    public class SortClass2
    {
        static public String[][] sort (String array[][],int x,int y)
        //sorting code
        return array;
    }
    The error is that its saying sortclass has to return a value thats compatible with String[][], which doesn't make sense because I am only returning array and array is a String[][].
    Last edited by leiferouis; 02-03-2009 at 01:01 AM.

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    typo's in bold:
    Java Code:
    //In main function
    static [B]sortclass[/B] s = new [B]sortclass[/B]();
    //random code
    String[][] array=new [B][I]string[/I][/B][5][2];
    //random code
    array=s.sort(array, 5,2);
    //output code
    
    //In sortclass class
    public class [B]SortClass2[/B]
    {
        static public String[][] sort (String array[][],int x,int y)
        //sorting code
        return array;
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #12
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    nevermind I figured out myself a while ago.

  13. #13
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    Argh. I can't figure out the code for two dimensional sectional and bubble sorting. I still have much trouble with this program because I still need to learn the third sorting method.

Similar Threads

  1. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 02:49 AM
  2. Calculate sum of long integer!
    By Julingo in forum New To Java
    Replies: 2
    Last Post: 09-10-2008, 12:50 AM
  3. It takes very long time.....
    By iresha in forum Advanced Java
    Replies: 6
    Last Post: 05-11-2008, 03:31 AM
  4. Help with Sorting Program
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 11:08 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
  •