Thanh Hoan
New member
Choose the correct function for binary insertion sort?
A.
B.
C.
D.
[/i][/i][/i][/i]
A.
Code:
void BinaryinsertionSort(int a[], int n)
{
int key;
for (int i = 1; i <=n-1; ++i)
{
int j = i - 1;
key = a[i];
//function binarySearch() returns the index
int index = binarySearch(a, key, 0, j);
//where the key should be inserted
while (j >= index)
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
Code:
void BinaryinsertionSort(int a[], int n)
{
int key;
for (int i = 1; i < =n-1; ++i)
{
int j = i - 1;
key = a[i];
//function binarySearch() returns the index
int index = binarySearch(a, key, j,0);
//where the key should be inserted
while (j >= index)
{
a[j+1] = a[j];
J++;
}
a[j+1] = key;
}
}
Code:
void BinaryinsertionSort(int a[], int n)
{
int key;
for (int i = 1; i <=n-1; ++i)
{
int j = i - 1;
key = a[i];
//function binarySearch() returns the index
int index = binarySearch(a, key, 0, j);
//where the key should be inserted
while (j <= index)
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
Code:
void BinaryinsertionSort(int a[], int n)
{
int key;
for (int i = 1; i <=n-1; ++i)
{
int j = i - 1;
key = a[i];
//function binarySearch() returns the index
int index = binarySearch(a, key, 0, j);
//where the key should be inserted
while (j <= index)
{
a[j+1] = a[j];
J++;
}
a[j+1] = key;
}
}