I know question sounds dumb, I can't really figure out what is wrong in this code?
void sort(int *arr, int size)
{
int min = 0;
for (int i = 0; i < size - 1; i++)
{
for (int j = i; i < size; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
}
if (min != i)
{
Swap(&arr[i], &arr[min]);
}
}
}
The following code should sort the arr
but it is giving segmentation fault. I ran this code via debugger and it says the value of j
at line
for (int j = i; i < size; j++)
something like 3234
(not initialized) and program ends. But j
should be 0
.
In your second for
loop, it should be j < size
, not i < size
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments