Consider the code given below, which runs insertion sort: [code] void insertionSort(int arr[], int array_size) { int i, j, value; for

Nguyễn Hào

New member
Consider the code given below, which runs insertion sort:
Code:
void insertionSort(int arr[], int array_size)
{
  int i, j, value;
  for (i = 1; i < array_size; i++)
  {
          value = arr[i];
          j = i;
          while (________ )
          {
                   arr[j] = arr[j − 1];
                   j = j − 1;
          }
          arr[j] = value;
  }
}
Which condition will correctly implement the while loop?
A. (j > 0) || (arr[j − 1] > value)
B. (j > 0) && (arr[j − 1] > value)
C. (j > 0) && (arr[j + 1] > value)
D. (j > 0) && (arr[j + 1] < value)
 
Answer: b
Explanation: In insertion sort, the element is A[j] is inserted into the correct position in the sorted sequence A[1… j - 1]. So, condition given in (j > 0) && (arr[j − 1] > value) will implement while loop correctly.
 

Tìm kiếm

Back
Top