I used the same logic but was getting TLE for the worst case test (when the input is in descending order, where quick select takes n^2 complexity). Had to put an ugly fix to swap the middle element with the last element in the beginning to get the test passed. Can anyone tell why it's taking so long to run?

```
class Solution {
public void wiggleSort(int[] nums) {
int n = nums.length;
if(n<2) return;
//Had to add this for the worst
//case to pass, I know it's ugly
swap(nums,n/2, n-1);
sortAroundMedian(nums);
int median = nums[n/2];
int i=0,j=0,k=n-1,vj=0;
while(j<=k) {
vj = virtualIndex(j,n);
if(nums[vj]>median) {
swap(nums,vj,virtualIndex(i,n));
i++;j++;
} else if(nums[vj]<median) {
swap(nums,vj,virtualIndex(k,n));
k--;
} else {
j++;
}
}
}
public int virtualIndex(int i, int n) {
return (2*i+1)%(n|1);
}
public void sortAroundMedian(int[] nums) {
int m = nums.length/2;
int b = 0, e = nums.length-1;
int pi = b-1;
while(pi!=m) {
if(pi>m) {
pi = quickSelect(nums,b,pi-1);
} else {
pi = quickSelect(nums,pi+1,e);
}
}
}
public int quickSelect(int[] nums, int b, int e) {
int i = b, j = b;
int pivot = nums[e];
for(;j<e;j++) {
if(nums[j] <= pivot) {
swap(nums,i,j);
i++;
}
}
swap(nums,i,j);
return i;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```