3 ms·
It is simply an inefficient bubble sort. A bubble sort makes an improvement by not comparing values that are already sorted. Eons ago this example was a common
by strangattractor 5y ago
It is simply an inefficient bubble sort. A bubble sort makes an improvement by not comparing values that are already sorted. Eons ago this example was a common way to introduce making algorithms more efficient. Naive Implementation -> Better Implementation.
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
- ColinWright 5y agoIf you read elsewhere in this discussion you will see many people giving reasons why this is not simply an inefficient Bubble Sort. You may disagree with them, as others do, but the situation seems rather more complicated than just saying "Oh, it's just a Bubble Sort."
- dahart 5y agoNo. Look again. The compare is opposite of bubble sort’s.