hey guys, i programmed a bubble sort function in C and it doesnt seem to be sorting properly. Ive searched the forums and have seen similar posts and tried them but they dont work. :(
Hoping someone could look over it to see if the concept/sytax is right.

typedef struct
{
        char artist[40];
        char title[60];
        int rating;
} song;

void song_by_title(song oneSong[99], int size)
{
        song temp1;
        int i,j = 0;
        int s = 0;
        for(i = 0; i < size; i++)
        {
                for(j = 0; j <= size - 1; j++)
                {

                s = strcmp(oneSong[j].title, oneSong[j + 1].title);

                if(s < 0)
                {
                        temp1 = oneSong[j + 1];
                        oneSong[j + 1] = oneSong[j];
                        oneSong[j] = temp1;
                }
                }
        }
}

thank you in advanced
spencer

I believe j should be assigned the value of i, seeing that after the completion of the inner loop, one value would be sorted, hence not needing to start the search from subscript 0 again.

the loop for bubble sort is

void bubleSort(int data[], int size)
{
   for(i=0; i<size-1; i++)
   {
      for(j=0; j<size-i-1; j++)
      {
          if(data[j] < data[j+1]) 
                swap(&data[j], &data[j+1]); /*assuming swap function is there*/
      }
   }
}

thanks, i fixed the problem although another arised. When the program outputs the information it comes out like this

How many songs will be entered?2
Enter the information below for songs:
Artist:spencer
Title:skeen
Rating(0-5):3
Enter the information below for songs:
Artist:james
Title:james
Rating(0-5):2
Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:
t
Songs by title:
james
--james
--2skeen
--spencer
--3Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:
Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:

when it should print out like this

How many songs will be entered?2
Enter the information below for songs:
Artist:peter
Title:peter
Rating(0-5):3
Enter the information below for songs:
Artist:james
Title:james
Rating(0-5):2
Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:
t
Songs by title:
james--james--2
skeen--spencer--3
Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)ilter,(q)uit:

heres my int main

int main(void)
{
        int i, gar;
        int j = 0;
        int rating_filter;
        char oper;
        song oneSong[99];
        int songCount = 0;

        printf("How many songs will be entered?");
        scanf("%d", &songCount);
        gar = getchar();

        for(i = 0; i <= songCount - 1; i++)
        {
                printf("Enter the information below for songs:\n");

                printf("Artist:");

                fgets(oneSong[i].artist, 40, stdin);

                printf("Title:");

                fgets(oneSong[i].title, 60, stdin);


                printf("Rating(0-5):");

                scanf("%d", &oneSong[i].rating);
                gar = getchar();
        }
while(1)
{
        printf("Select the operation to perform --(t)itle,(a)rtist,(r)ating,(f)$
        scanf("%c", &oper);

        if(oper == 't')
        {
                printf("Songs by title:\n");
                song_by_title(oneSong, songCount);
                for(j = 0; j < songCount; j++)
                {
                printf("%s--%s--%d\n",oneSong[j].title, oneSong[j].artist, oneS$
                }
        }
        else if(oper == 'a')
        {
                printf("Song by artists:\n");
                song_by_artist(oneSong, songCount);
                for(j = 0; j < songCount; j++)
                {
                printf("%s--%s--%d\n",oneSong[j].title,oneSong[j].artist,oneSon$
                }
        }
        else if(oper == 'r')
        {
                printf("Songs by ratings:\n");
                song_by_rating(oneSong, songCount);
                for(j = 0; j < songCount; j++)
                {
                printf("%s--%s--%d\n",oneSong[j].title, oneSong[j].artist,oneSo$
                }
        }
        else if(oper == 'f')
        {
                printf("Enter the rating to show:\n");
                scanf("%d", &rating_filter);
                for(j = 0; j < songCount; j++)
                {
                if(oneSong[j].rating == rating_filter)
                {
                printf("%s--%s--%d\n",oneSong[j].title, oneSong[j].artist,oneSo$
                }
                }
        }
        else if(oper == 'q')
        {
                printf("Thank you for using!\n");
                break;
        }
        j = 0;
}
return 0;
}

been looking over the code and cant find what would produce this. :/
help would be greatly appreciated!

This article has been dead for over six months. Start a new discussion instead.