0

I am getting unexpected results from my bubble sort program in C. The program below is a program which takes 5 inputs from the user, performs selection sort on them, then performs exchange sort on them.

If i use these inputs:

50
150
75
175
23

It should sort them to the following:

23
50
75
150
175

However, it doesnt sort correctly and sorts like the following (opposite way around for exchange as it does it in Descending order):

150
175
23
50
75

Its quite strange because if you enter certain values it will sort them correctly such as:

73
84
03
26
83

Not quite sure whats going on with it. I cant start making changes to it when it technically works for certain values.

I must be missing something somewhere.

Any help would be appreciated.

CODE IN FULL:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

int main(int argc, char *argv[])
{
    char    arc5Strings[5][256];
    int nCount, nCount2, nCount3, nCount4, nCount5, nCount6, nCount7, letter, sorted;


    int fMinVal[1][2] = {1,1};
    int nMinValPosition;
    int nMoves;
    int nRow;
    int i, j, k, indexOfCurrentSmallest, q, temp;
    char    arcTemp[256];
    int nOutOrder;
    int nNumValues;
    int clean1, clean2;

    //input the values

    for(nCount=0; nCount < 5; nCount++)
    {
        printf("Please input string %d/5: ", nCount + 1);
        fgets(arc5Strings[nCount], 256, stdin);

            if(strlen(arc5Strings[nCount]) > 11)
            {
                printf("Your string contains more than 10 characters. Please try again.");
                nCount = 5;
                exit(0);
            }

    }

    //------------------------------------------------------------------------------
    //Selection Sort

    printf("\n\n");

    for(i=0;i<5;i++)
    {
         indexOfCurrentSmallest = i;
         for(j=i;j<5;j++)
         {
              for(k=0;k<255;k++)
              {
                    if(arc5Strings[j][k] < arc5Strings[indexOfCurrentSmallest][k])
                    {
                      //we found a new possible smallest
                      indexOfCurrentSmallest = j;
                      break;
                    }
                    else if(arc5Strings[j][k] > arc5Strings[indexOfCurrentSmallest][k])
                    {
                        //no point in searching further, the one we are looking at is already larger than the one we found.
                        break;
                    }
              }
        }

         //let's do a swap
         for(q=0;q<255;q++)
         {
          temp = arc5Strings[i][q];
          arc5Strings[i][q] = arc5Strings[indexOfCurrentSmallest][q];
          arc5Strings[indexOfCurrentSmallest][q] = temp;
         }
    }   

    //---------------------------------------------------------------

    //print entire array

    printf("This is your selection sorted array based on ASCII values\n\n");
    for(nCount3 = 0; nCount3 < 5; nCount3++)
    {
        for(nCount4 = 0; arc5Strings[nCount3][nCount4] != '\0'; nCount4++)
        {
            printf("%c", arc5Strings[nCount3][nCount4]);
        }

    }

    //---------------------------------------------------------------------
    //Exchange Sort

    nNumValues = 5;
    nOutOrder = TRUE;
    nMoves = 0;

    while(nOutOrder && nNumValues > 0) 
    {
        nOutOrder = FALSE;
        for(i=0;i<5;i++)
        {
            for(nCount=0; nCount < nNumValues -1; nCount++)
                 {
                        for(nCount2=0, sorted=0; sorted==0; nCount2++)
                        {
                              if(arc5Strings[nCount][nCount2] < arc5Strings[nCount+1][nCount2])
                              {
                                     for(letter=0; letter<256; letter++)
                                     {
                                        arcTemp[letter] = arc5Strings[nCount][letter];
                                     }

                                     for(letter=0; letter<256; letter++)
                                     {
                                        arc5Strings[nCount][letter]= arc5Strings[nCount+1][letter];
                                     }

                                     for(letter=0; letter<256; letter++)
                                     {
                                        arc5Strings[nCount+1][letter] = arcTemp[letter];
                                     }

                                     sorted = 1;
                                     nMoves++;
                              }
                              else if (arc5Strings[nCount][nCount2] < arc5Strings[nCount+1][nCount2])
                                   sorted = 1;
                        }
                 }

            nNumValues--;
        }
    }

    printf("\n\n\nThe sorted list in Descending order is: \n\n\n");
    for(nCount5 = 0; nCount5 < 5; nCount5++)
    {
            printf("%s", arc5Strings[nCount5]);
    }

//-----------------------------------------------

    printf("\n %d moves were required to sort this list\n\n", nMoves);

    return 0;
}   
Dr.Pepper
  • 559
  • 4
  • 11
  • 27

2 Answers2

3

It's because you're sorting strings rather than numbers, so it's using character-based sorting.

In other words, "175" is less than "23" because "1" is less than "2".

If you want to sort them as numbers, convert them to numbers first.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

You are using strings while you should be using integers. Either convert your string to integer or use int straight away

See this link an example if you want to compare string

compare two alphanumeric string

Again use Array of integers if you dont need text as an input to compare

Community
  • 1
  • 1
Hamza
  • 1,593
  • 2
  • 19
  • 31
  • Hi Hamza, i tried adding the "atoi" in as you mentioned above but i get errors such as "makes pointer from integer without a cast" - is this because its in a 2d array? – Dr.Pepper Feb 04 '14 at 23:21
  • O yeah this wont do that my bad.. or convert each char to int using atoi and then concatenate it but again why go through all this mess when you can do this with integer – Hamza Feb 04 '14 at 23:26
  • The user should be able to either enter text or numbers - i dont understand what you mean when you say " do this with integer" – Dr.Pepper Feb 04 '14 at 23:28
  • why should it be text? and by do it with integer means that instead of using character array, use array of integers – Hamza Feb 04 '14 at 23:28
  • Because the task we have been set is to "Declare a 2D char array that can be used to hold 5 strings " – Dr.Pepper Feb 04 '14 at 23:55