Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sorting a 2-D Array with bubblesort?

Bad ProgramerBad Programer Posts: 8Member
Does anyone know how to sort a two-dimensional character/string array using bubble sort? I know to do it with a 1-D array perfectly, but how is done with a 2-D?
«1

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-4-26 11:42:38[/red][/b][hr]
    : Does anyone know how to sort a two-dimensional character/string array using bubble sort? I know to do it with a 1-D array perfectly, but how is done with a 2-D?
    :

    post an example of what you think a 2d array of string is. Something like this?
    [code]
    // This is really a 3d array, but it is a 2d array of 80-character strings.
    char array[5][4][80];
    [/code]

    or do you mean this
    [code]
    // this is a 1d array of 80-character strings
    char array[5][80];
    [/code]


    in either case, it will be nearly identical to the int arrays, except use strcmp() and strcpy() to move the elements around.


  • Bad ProgramerBad Programer Posts: 8Member
    Like this:

    char list[40][10]={"Bob","John","Mary","Sam"};
  • stoberstober Posts: 9,765Member ✭✭✭
    : Like this:
    :
    : char list[40][10]={"Bob","John","Mary","Sam"};
    :

    treat it as a 1d array of ints, but use strcmp() to compare the two and strcpy() to copy from one to another. Otherwise, the algorithm is identical.
  • Bad ProgramerBad Programer Posts: 8Member
    This is what I have so far:

    void bubbleSort(char list[40][10], int array_size)
    {
    int i, j;
    char temp[40][10]:
    for (i=0; i<=(array_size - 1); i++)
    {
    for (j=i+1; j>=array_size; j++)
    {
    if (strcmp(list[j-1],list[j]) < 0)
    {
    strcpy(temp,list[j]);
    strcpy(list[j], list[i];
    strcpy(list[i],temp);
    }
    }
    }
    }

  • stoberstober Posts: 9,765Member ✭✭✭
    : This is what I have so far:
    :
    : void bubbleSort(char list[40][10], int array_size)
    : {
    : int i, j;
    : char temp[10]; [red]//[10]: [/red]
    : for (i=0; i<=(array_size - 1); i++)
    : {
    : for (j=i+1; j>=array_size; j++)
    : {
    : if (strcmp(list[j-1],list[j]) < 0)
    : {
    : strcpy(temp,list[j]);
    : strcpy(list[j], list[i];
    : strcpy(list[i],temp);
    : }
    : }
    : }
    : }
    :
    :

  • Bad ProgramerBad Programer Posts: 8Member
    For some reason, something is still not right. When I pass the array to the bubbleSort, it doesn't sort it in alphabetical order. Everything looks right though.

    void bubbleSort(char list[40][10], int array_size)
    {
    int i, j;
    char temp[10];

    for (i=0; i<(array_size - 1); i++)

    {
    for (j=i+1; j>array_size; j++)
    {
    if (strcmp(list[j-1],list[j]) < 0)
    {
    strcpy(temp,list[j]);
    strcpy(list[j], list[i]);
    strcpy(list[i],temp);
    }
    }
    }
    cout<<list[0]<<endl;
    cout<<list[1]<<endl;
    cout<<list[2]<<endl;
    cout<<list[3]<<endl;
    cout<<list[4]<<endl;
    }
  • stephlstephl Posts: 422Member
    : For some reason, something is still not right. When I pass the array to the bubbleSort, it doesn't sort it in alphabetical order. Everything looks right though.
    :
    : void bubbleSort(char list[40][10], int array_size)
    : {
    : int i, j;
    : char temp[10];
    :
    : for (i=0; i<(array_size - 1); i++)
    :
    : {
    : for (j=i+1; j>array_size; j++)
    : {
    : if (strcmp(list[j-1],list[j]) < 0)
    : {
    : strcpy(temp,list[j]);
    : strcpy(list[j], list[i]);
    : strcpy(list[i],temp);
    : }
    : }
    : }
    : cout<<list[0]<<endl;
    : cout<<list[1]<<endl;
    : cout<<list[2]<<endl;
    : cout<<list[3]<<endl;
    : cout<<list[4]<<endl;
    : }
    :
    I don't know exactly what bubblesort is, but here are two points that seem odd to me:
    [code]for (j=i+1; j[red]>[/red]array_size; j++)[/code]
    Isn't it '<' instead?
    [code]if (strcmp(list[j-1],list[j]) < 0)
    {
    strcpy(temp,list[j]);
    strcpy(list[j], list[i]);
    strcpy(list[i],temp);
    }[/code]
    You're comparing list[j-1] and list[j], so why do you invert list[j] and list[i]?

    Take care, Steph.
  • stoberstober Posts: 9,765Member ✭✭✭
    see corrections in [red]RED[/red] below
    :
    [code]
    : void bubbleSort(char list[40][10], int array_size)
    : {
    : int i, j;
    : char temp[10];
    :
    : for (i=0; i<(array_size - 1); i++)
    :
    : {
    : for (j=i+1; j[red]< [/red]array_size; j++)
    : {
    : if (strcmp(list[[red]i[/red]],list[j]) < 0)
    : {
    : strcpy(temp,list[j]);
    : strcpy(list[j], list[i]);
    : strcpy(list[i],temp);
    : }
    : }
    : }
    [red]for(i = 0; i < array_size; i++)
    : cout<<list[i]<<endl;[/red]
    : }
    [/code]:

  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    : I don't know exactly what bubblesort is

    [blue]Its probably the easiest sort algorithm to code, but normally also the slowest to run.[/blue]

  • stephlstephl Posts: 422Member
    : : :
    : : I don't know exactly what bubblesort is
    :
    : [blue]Its probably the easiest sort algorithm to code, but normally also the slowest to run.[/blue]
    :
    :
    Thanks for the explanation.

    Take care, Steph.
«1
Sign In or Register to comment.