1

i've tried to make a bubble sort, but it does not work well. When i try to sort large numbers, the programm issues wrong numbers. With small numbers all is okey. Sorry for my bad english. My code is below:here is my problem

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


int main(void)
{
printf("Numbers in massive\n");
int number = GetInt();
int a[number];
for(int g = 0; g<number; g++)
{
printf("Number #%i\n", g+1);
a[g] = GetInt();
}
// part of data entry   
int end = 0;    
do
{
end = 0;
for(int i=0; number>i; i++)
    {
    if(a[i]>a[i+1])
        {
        int swp = a[i];
        a[i] = a[i+1];
        a[i+1] = swp;

        end++;
        }
    }
}
while (end > 0);
// part of sort
for(int k=0; number>k; k++)
    {
    printf("%i ", a[k]);
    }
printf("\n");    
}
// part of the output
1

you have this problem because you are storing this large number in an integer variable. You should use some other data type for such large values like long int or long long int, which can store the values you want to sort.

I hope this helps, please mind my english as it is not my primary language. Feel free to ask in case of any doubt in my answer. Please upvote if you found this answer helpful as it motivates me to help more folks like you. Happy coding :)

1
  • oh god, i've forgotten about it, thanks
    – VladKanap
    Aug 17 '17 at 19:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .