Results 1 to 13 of 13
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question 2 dimensional array with strings.

    Assignment, I have chosen to do a bubble sort as it apparently is the easiest of the sorts although no where near the best but only have 20 Strings

    I have deisgned the gui as required, controlled by radio buttons as there are 2 sorts and 3 searches on the same data.

    From the research I have done scrolling through pages and pages of information nearly all bubble sorts are done using int rather than string

    my question is

    is this possible, is it possible using a bubble sort to sort String ?

    Thanks in advance

  2. #2
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default 2d Arrays, Strings and Bubble sort

    I say nearly all as I know I haven't gone through all the information available

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    So far I have my 2d Array setup

    Java Code:
    String[][]mbrnum = new String [20] [2]; // 2 dimensional array
    
            int swap;
            int arraymax; // stops a fault, doesnt keep running array
    entered all my strings e.g.

    Java Code:
    mbrnum[0][0] = "DAN100215";
    all together 40 strings in 2 sets of 20

    Set it to print out in appropriate textfields

    Java Code:
    txtOneA.setText(mbrnum[0][0]);
    arraymax = 20; // sets maximum number of Strings in array
    Then I have the radio buttons set up in group , 1 of the sorts asks for ascending order so I am aware that that will go in the

    private void rbtnMbrParActionPerformed(java.awt.event.ActionEve nt evt) {

    .................
    }
    However at the moment I do not seem to be getting anywhere, I have not found any information that helps solve the problem, I am not asking anyone to write the code for me, but if someone can point me in the right direction I would be grateful.

    Thanks in advance

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I don't see why you couldn't use bubblesort on strings assuming you have a valid way to compare them. You're going to need to write your own compares method that can determine if "cat" is less than "dog"
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default String arrays bubble sort

    Sorry but although I understand what you are trying to say actually putting it into practice is another thing.

    I cannot find a site that gives me some clues with this issue as everyone seems to use int rather than String.

    Appreciate any assistance.

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question 2d array now split into 2 single arrays

    I have found that the 2d array didnt work for me as far as I couldnt code it to work, I know have them in single arrays however for a programmer would this be bad practice ?:confused:

  7. #7
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    The reason why majority of ALL sorting (including bubble sort) deals with numbers only because all have have to compare is to see which one is bigger. When you deal with strings, that puts you into a whole different sorting decisions. There are many possibilities such as sorting in asc alphabetically or alphanumberic or by their compareString or by uppercase or lowercase etc..... You can use bubble sort on strings, but the trick is know what rule to use when sorting it. Number's only rule is " bigger or smaller or equal to ' <=, ==, >=')

  8. #8
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    Ok so which Sort Algorithms are classed as the best using Strings, bearing in mind my data is only an array of 20 words.

    :D

  9. #9
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    you still need to decide how you want to sort your strings. After that you can use any method of sorting. The only difference is the time complexity. recommend bubble or insertion.

  10. #10
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    I need to sort in ascending order, so I have 2 arrays of strings one made up of letters and numbers and the other with numbers, I firstly need to sort them into full lists and them the other sections ask for a search.

    So in this case which is the best algorithm, is it quicksort, shaker sort etc etc?

    Thanks in advance

  11. #11
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    So you're sorting numbers with strings?
    What i would do is setting my numbers with a specific vaue.
    For example, this is what i would do:
    a = 0
    b=1
    c= 2
    d=3
    and so on.
    then you will decide whether A = 0 also or A = 27, B = 28 and so forth.

    have a function that returns an INT such as
    public int letterToNum(char letter){
    then you return the responding number value of your letter;
    }

    from there you would do bubble sort. Then you would iterate through each words/numbers in your 2d array and then check their char at string with charAt() and compare values.

    then just do something along the lines of:

    if ( 0 >= charToNum(charAt( char here )){
    then do somehing;
    }

    hope this helps

  12. #12
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you can sort by the ASCII value or by the length of the strength or some value you make up. That's what the custom comparable class is for. How do you want to sort the strings? That will help determine how to write the comparisons. And bubble sort is fine for what you're trying to do, just knows there are more efficient algorithms for sorting.

    [Edit]
    Didn't realize you stated how you wanted to sort it.

    You can do what seandingobat said which is essentially the same thing as my advice, however requires a little more work.
    Google the ASCII table. It will give you the range to use(I think it's like 32 - 1?? for printable characters). When you goto compare you can do something like if A < B do this, and I believe the compiler will auto compare using the ascii value and work as intended.
    Last edited by xcallmejudasx; 10-12-2009 at 06:20 PM.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  13. #13
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default Thanks

    Appreciate the help, will post the code when I have it all working!

Similar Threads

  1. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 05:21 AM
  2. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 06:51 AM
  3. Mulit dimensional Array
    By jonsamwell in forum New To Java
    Replies: 7
    Last Post: 02-03-2009, 12:35 PM
  4. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 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
  •