Open In App

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

Like Article
Like
Save
Share
Report

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




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]);
    }
}


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


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads