0
Is there any difference between these codes?
that:
void bubble_sort(int array[], int n)
{
int count = -1;
while (count != 0)
{
count = 0;
int aux;
for(int i = 0; i < n - 1; i++)
{
if(array[i] > array[i + 1])
{
aux = array[j];
array[j] = array[j + 1];
array[j + 1] = aux;
count++;
}
}
n--;
}
}
and that:
void bubbleSort(int array[], int n)
{
int i, j, aux;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
aux = array[i];
array[i] = array[i + 1];
array[i + 1] = aux;
}
}
}
*mainly in a matter of time
The best case complexity in the first code is O(n), while in the second code it is O(n 2). This means that, in an already ordered vector, the first program will only run once through the list, while the second will run n times through sublists of size 1 to n.
– G. Bittencourt
Grateful, you have removed my doubts
– user202246