I am doing an bubble sort assignment, the goal is to enter up to a max of 10 numbers and sort them. The problem that I am running into is when I run the code, I have to hit ^D to have the next line of the array displayed and I am getting a negative number sometimes displayed which is not one of the numbers I entered. Basically what I am asking is there something wrong with my main method as I believe my sort method is correct. Thanks in advance.

#include <stdio.h>
#define MAXIMUM 10

void sort(int a[], int n)
{
        int j, jj;
        int t;

        for(j=0; j<(n-1); j++) /* outer loops and counts passes */
        {       for(jj=0; jj< (n-(1-j)); jj++) /* inner loop and compares adjenct items */
                {       if (a[jj] > a[jj+1]) /* out of order */
                                {
                                t= a[jj];
                                a[jj] = a[jj+1];
                                a[jj + 1] = t;
                                }
                }
        }
}
int main()
{
        int a[MAXIMUM];
        int j; /* loops */
        int x; /* get data */
        int cnt = 0; /* actually count */

        printf("enter number or -1 to quit");

        scanf(" %d" , &x); /* into x */
        while( x != -1 && cnt < MAXIMUM)
                {
                        a[cnt++] = x; /* put data into array */
        printf("enter number or -1 to quit");
        scanf("%d",&x);
                } 
        printf("number of items entered %d \n", cnt);

        for (j=0; j < cnt; j++)

        {
            printf("%d %d\n",j, a[j]);
        }

        if (cnt > 0)

        {
                sort(a, cnt);

                printf("sorted list \n");
        }

        for (j =0; j < cnt; j++)
                {
                printf("%d %d\n", j, a[j]);

        if (cnt = MAXIMUM)
                printf("full-overflow\n");

        else printf("enter a number or -1 to quit");
                scanf("%d", &x);
        }
}

Two things I see: for(jj=0; jj< (n-(1-j)); jj++) /* inner loop and compares adjenct items */ I don't believe jj< (n-(1-j)); is doing what you want. Try calculating values of jj using various values of j.


Are you assigning or comparing cnt in this IF statement?

if (cnt = MAXIMUM)
                printf("full-overflow\n");

Also, please use better formatting. Your indentation is very inconsistent making the program a little hard to follow.

I am comparing cnt in that if statement.
I tried flipping the j and n

for(jj=0; jj< (j-(1-n)); jj++)

and here is the output I get.

enter number or -1 to quit56
enter number or -1 to quit7
enter number or -1 to quit5
enter number or -1 to quit6
enter number or -1 to quit9
enter number or -1 to quit56
enter number or -1 to quit44
enter number or -1 to quit23
enter number or -1 to quit85
enter number or -1 to quit65
enter number or -1 to quit45
number of items entered 10
0 56
1 7
2 5
3 6
4 9
5 56
6 44
7 23
8 85
9 65
sorted list
0 5
full-overflow
1 6
full-overflow
2 7
full-overflow
3 9
full-overflow
4 1
full-overflow
5 23
full-overflow
6 -4195980
full-overflow
7 44
full-overflow
8 -4195972
full-overflow
9 56
full-overflow

One = char is for assignment in C. Two == char's are used for comparisons.

if(variable == someValue);

This is the pseudo code for a nested loop version of bubble sort:

procedure bubbleSort( A : list of sortable items )
  n = length(A)
  for (i = 0; i < n; i++)
     /* back through the area bringing smallest remaining element to position i */
     for (j = n-1; j > i; j--)
        if A[j-1] > A[j] then
           swap(A[j-1], A[j])
        end if
     end for
  end for
end procedure

Straight from Wikipedia, which has a whole page on it, and links to many more references, versions, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.