GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
131 views

An array of $n$ distinct elements is said to be un-sorted if for every index $i$ such that $ 2 \leq i \leq n-1$, either $A[i] > max \{A [i-1], A[i+1]\}$, or $A[i] < min \{A[i-1], A[i+1]\}$. What is the time-complexity of the fastest algorithm that takes as input a sorted array $A$ with $n$ distinct elements, and un-sorts $A$?

  1. $O(n \: \log \: n)$ but not $O(n)$
  2. $O(n)$ but not $O(\sqrt{n})$
  3. $O(\sqrt{n})$ but not $O(\log n)$
  4. $O(\log n)$ but not $O(1)$
  5. $O(1)$
asked in Algorithms by Veteran (77.8k points)   | 131 views

1 Answer

+2 votes
A pairwise swap will make the sorted array unsorted. Hence, the option (b) is correct.

For eg - if an array is 1 2 3 4 5 6 7 8

The array will become after a pair wise swap to 2 1 4 3 6 5 8 7. For all i between 2 and n-1, a[i] is either lower, or either greater than their adjacent elements.

Since, each element is being swapped exactly once. The operation has O(n) time complexity.
answered by Active (1k points)  
edited by
So you mean only one pass of bubble sort is enough??
Hi,

I have just edited the answer to make my point more clear. By pairwise swap, i mean here the pairwise disjoint swapping of elements. Hope, I would have made my point more clear.
I think you have got the best case

Here is my exmple-- 15 10 20 13 17...I think here we need more swap..Please check once.Let me know If i m doing any mistake
Hi

Question asks that Input is sorted. You may be missing that point..
ohh..yes..i read just the opposite..sorry

btw thanks for correction


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1502 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1416 Points

  5. Niraj Singh 2

    1391 Points

  6. Debashish Deka

    1246 Points

  7. Rupendra Choudhary

    1194 Points

  8. rahul sharma 5

    1158 Points

  9. Arjun

    956 Points

  10. srestha

    950 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1386 Points

  3. junaid ahmad

    502 Points

  4. Debashish Deka

    414 Points

  5. sudsho

    410 Points


23,373 questions
30,079 answers
67,405 comments
28,396 users