The following function represents which sorting?
void Sorting(int a[], int n)
{
bool swap = true;
int first = 0;
int last = n - 1;
while (swap)
{
swap = false;
for (int i = first; i < last;i++)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
if (!swap)
break;
swap = false;
--last;
for (int i = last - 1; i >= first; i--)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
++first;
}
}
void Sorting(int a[], int n)
{
bool swap = true;
int first = 0;
int last = n - 1;
while (swap)
{
swap = false;
for (int i = first; i < last;i++)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
if (!swap)
break;
swap = false;
--last;
for (int i = last - 1; i >= first; i--)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
++first;
}
}A. Bubble sort
B. Selection sort
C. Bidirectional bubble sort
D. Odd-even sort
Answer: Option C

Join The Discussion