GATE | GATE-CS-2015 (Mock Test) | Question 11



Let swap() be a function that swaps two elements using their addresses. Consider the following C function.

filter_none

edit
close

play_arrow

link
brightness_4
code

void fun(int arr[], int n)
{
    for (int i = 0; i < n; i+=2)
    {
        if (i>0 && arr[i-1] > arr[i] )
            swap(&arr[i], &arr[i-1]); 
        if (i<n-1 && arr[i] < arr[i+1] )
            swap(&arr[i], &arr[i + 1]);
    }
}

chevron_right


If an array {10, 20, 30, 40, 50, 60, 70, 80} is passed to the function, the array is changed to
(A) {20, 10, 40, 30, 60, 50, 80, 70}

(B) {10, 30, 20, 40, 60, 50, 80, 70}
(C) {10, 20, 30, 40, 50, 60, 70, 80}
(D) {80, 70, 60, 50, 40, 30, 20, 10}


Answer: (A)

Explanation: The function sorts the array in waveform. See Sort an array in wave form for more details.

Quiz of this Question



My Personal Notes arrow_drop_up