简体   繁体   中英

How to print result of bubble sort function?

Bascially a beginner problem. I don't know how to output the result my function and to check whether the logic is correct. Any help would be great thanks.

#include<stdio.h>
#include<conio.h>
#define SIZE 3
int sort(int numbers[]);//function prototype
int main (void){
    int response[SIZE];
    int count;
    for (count=0;count<SIZE;count++)
    {
        printf ("Enter your number %d\n",count);
        scanf ("%d",&response[count]);
    }
    printf ("The array when sorted is\n");  
    getch();
return 0;   
}
//function to sort array elements
int sort(int numbers[])
{
    int store;
    int x;
    for (x=0; x<=SIZE;x++)
    {
        if (numbers[x+1]<numbers[x])
        {
            store=numbers[x+1];
            numbers[x+1]=numbers[x];
            numbers[x]=store;   

        }


    }

}

正如评论中指出的那样,您的冒泡排序实现是错误的,请查看此链接,也许它可以帮助冒泡排序C-实现

You could write a moderately general purpose sort order check function (for arrays of integers):

void check_ascending_order(int n, int data[n])
{
    for (int i = 0; i < n - 1; i++)
    {
        if (data[i] > data[i+1])
            fprintf(stderr, "!! FAIL !! data[%d] = %d > data[%d] = %d\n",
                    i, data[i], i+1, data[i+1]);
    }
}

You'd invoke it with:

check_ascending_order(SIZE, numbers);

It will produce error messages if the data is not sorted in non-decreasing order (it allows adjacent entries to be equal).

If you don't have at least C99, you will need to modify the code slightly:

void check_ascending_order(int n, int data[])
{
    int i;
    for (i = 0; i < n - 1; i++)
    {
        if (data[i] > data[i+1])
            fprintf(stderr, "!! FAIL !! data[%d] = %d > data[%d] = %d\n",
                    i, data[i], i+1, data[i+1]);
    }
}

Note that this not producing a failure message only shows that one set of data was sorted, not that your sort function is correct for all possible inputs.

Another test you could/should do is conservation; make a copy of the original data set, and check that each entry in the original unsorted data also appears in the sorted output, just as often as in the original (so nothing went missing, and nothing magically appeared).

And printing the result can be done multiple ways. One simple way is:

void print_array(int n, int data[n])
{
    for (int i = 0; i < n; i++)
        printf("[%d] = %d\n", i, data[i]);
}

You can add tweaks like a tag string to identify the data, and specifying the file stream to write to:

void print_array(FILE *fp, const char *tag, int n, int data[n])
{
    fprintf(fp, "%s:\n", tag);
    for (int i = 0; i < n; i++)
        fprintf(fp, "[%d] = %d\n", i, data[i]);
    fflush(fp);
}

You can then use:

print_array(stdout, "Unsorted", SIZE, numbers);
...code to sort the data...
print_array(stdout, "Sorted", SIZE, numbers);
check_ascending_order(SIZE, numbers);

Note that printing comes before the checking; it allows you to see the entire sorted result set first. Also note that 3 entries to sort is tiny; you need to check on much larger sets, like 6, 7, 10, 100.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM