Your bubble sort in the code is:
for(i=0; i<(k-1); i++)
{
for(i=0; i < k-j; i++)
{
}
if (nums[i] > nums[i+1])
{
swap = nums[i];
nums[i] =nums[i+1];
nums[i+1] = swap;
}
}
Assuming k
is the number of elements to sort, it should be:
for(i=0; i<(k-1); i++)
{
for(j=0; j < k - 1 - i; j++)
{
if (nums[j] > nums[j+1])
{
swap = nums[j];
nums[j] =nums[j+1];
nums[j+1] = swap;
}
}
Your code had the second loop with an empty body and you need a diffrent loop variable for the inner loop than i
, since this is driving the outer loop.