I'm learning C and I've got to a point learning about different sorting algorithms. Before seeing how it was done, I wanted to try doing it myself based on what I read on how it's working.
int temp;
for(int i = 0; i < SIZE - 1; i++)
{
for(int j = 0; j < SIZE - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
This is what the code from where I learn looks like:
for(int i = 0; i < SIZE; i++) { if((i < SIZE - 1) && (arr[i] > arr[i+1])) { int tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; i = -1; } }
Now, obviously they're different, but is one more efficient in someway than the other? Did I do something that is less efficient than their code?