What purpose does the following code serves int main() { int array[] = {5, 3,…

2025

What purpose does the following code serves

int main()

{

int array[] = {5, 3, 1, 9, 8, 2, 4, 7};

int size = sizeof(array)/sizeof(array[0]);

int i, j, min_idx,temp;

for (i = 0; i < size-1; i++)

{

min_idx = i;

for (j = i+1; j < size; j++)

{

if (array[j] < array[min_idx])

min_idx = j;

}

temp = array[min_idx];

array[min_idx] = array[i];

array[i] = temp;}

  1. A.

    Finds some special element in the array

  2. B.

    Sorts the elements of array

  3. C.

    Finds the smallest element in the array

  4. D.

    None of the above

Attempted by 156 students.

Show answer & explanation

Correct answer: B

The code implements the Selection Sort algorithm. It iterates through the array to find the minimum element in the unsorted portion and swaps it with the current position, sorting the elements in ascending order.

Explore the full course: Coding For Placement