I came across the following question:
Given an unsorted array of B[1 . . 2n+1]real numbers, give a linear one that produces a permutation A[1..2n+1]of of Bsuch that it Ais wiggly.
I basically did a merge sort and changed it:
MergeSort(a,n)
int i=2;
while (i ≤ n)
{
Swap(a[i−1], a[i]);
i=i+2;
}
But the time complexity O(nlogn) + O(n)(from sorting and from loop, respectively) that gives O(nlogn). But I want to do it in O(n)time.
Should I use sorting sorting / sorting sorting / sorting notation to get linear time and then change it to get a wiggly array?
source
share