-
terminate a bubble sort
My bubble sort finishes before the loop finishes, so I get some unless printout. Is there a way I terminate my bubble sort early if no more swaps is being made with code without using bool?
Code:
void sortlist(int nums[],int size)
{
for(int index = 0 ; index < size-1; index++)
{
if (nums[index] > nums[index+1])
{
int temp = nums[index+1];
nums[index+1] = nums[index];
nums[index] = temp;
}
}
printlist(nums,size);
}
-
Don't call printlist() inside sortlist() perhaps?
Besides, I thought a bubble sort had two nested loops, and you only have one.