简体   繁体   English

气泡排序打印垃圾值

[英]bubble sort printing garbage values

I was curious on bubble sorting so I have made a function that takes user input and than stores the values in the posititons of an array but it keeps printing out some garbage value. 我对气泡排序感到很好奇,所以我做了一个函数,它接受用户输入,然后将值存储在数组的正数中,但它会不断打印出一些垃圾值。

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void sort(int*z);
void swap(int* element1Ptr,int* element2Ptr);

int main(void)
{  
    int number[10];  
    int input;  
    int* sorting;  

    sorting = number;
    printf("Please enter a number less than 10 digits long");
    scanf_s("%d", &input);
    for (int i=0; i<10;i++)
    {
        number[9-i]=input%10;
        input/=10;
    }
    printf("\n");
    sort(sorting);
    printf("%d\n",number[0]);
}

do I have the bubble soriting code wrong or am I passing the wrong variable? 气泡请求代码错误还是传递错误的变量?

void sort(int* z)
{
    int pass; /* pass counter */  
    int j; /* comparison counter */  

    /* loop to control passes */  
    for ( pass = 0; pass < 11; pass++ ) 
    {
        /* loop to control comparisons during each pass */
        for ( j = 0; j < 10; j++ )
        {
             /* swap adjacent elements if they are out of order */
             if ( z[ j ] > z[ j + 1 ] ) 
             {
                 swap( &z[ j ], &z[ j + 1 ] );
             } /* end if */
        } /* end inner for */
    } /* end outer for */
}/* end function bubbleSort */

void swap(int* element1Ptr,int* element2Ptr)
{
    int hold = *element1Ptr;
    *element1Ptr = *element2Ptr;
    *element2Ptr = hold;  
} /* end function swap */  

The error I was having I was trying to print the first value in the array and that value would always be 0 if you didn't have a 10 digit number: 我尝试打印数组中的第一个值时遇到的错误,如果您没有10位数字,则该值将始终为0

 printf("%d\n", number[0]);

should read 应该读

 printf("%d\n", number[9]);

and the loop I had bult to place the values in was placing them in the wrong spot so I fixed it like so 而我大胆地将值放入的循环将它们放在错误的位置,因此我将其固定为

for (int i=0; i<10; i++)
{
     number[i] = input % 10;
     input /= 10;
}

that is all I changed and it worked fine. 那就是我所做的全部更改,并且效果很好。

Your number input code is a bit funky. 您的数字输入代码有点时髦。 Instead of entering a single long number and then using %10 to get the value of each digit, why not enter a set of 10 numbers? 为何不输入单个长数字,然后使用%10获取每个数字的值,为什么不输入一组10个数字呢?

for (int i=0; i<10;i++)
{
    scanf_s("%d", &input);
    number[i]=input;
}

Bubble sort should be 气泡排序应为

for ( pass = array_length - 2; pass >= 0; pass-- ) 
{
    for ( j = 0; j <= pass; j++ )
    {
       compare_and_swap(& z[j], & z[j + 1]);
    }

    // at this point in the code, you are guaranteed that 
    // every element beyond the index of pass is in the final
    // correct location in the array

    // so if you input array was {9, 8, 7, 6, 5}
    // and pass = 2
    // then elements 3 and 4 are correct here:
    // {*, *, *, 8, 9}
}

void compare_and_swap(int* a, int* b)
{
    if (*a > *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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