Skip to content

Conversation

lhxon
Copy link
Contributor

@lhxon lhxon commented Jun 1, 2017

Could you give a more deep explanation?

public int[] WiggleSort(int[] array)
{
_array = array;
int median = findKThLargest(_array.Length / 2);
int left = 0, i = 0, right = _array.Length - 1;
while (i <= right)
{
if (_array[newIndex(i)] > median)
{
//put newIndex(i) at odd index(from 1, 3, to 5, ...)
swap(newIndex(left++), newIndex(i));
i++;
}
else if (_array[newIndex(i)] < median)
{
//put newIndex(i) at even index(max even index to little .... )
swap(newIndex(right--), newIndex(i)); //right--, so i relatively toward right 1 step
}
else
{
i++;
}
}
return _array;
}

@jackzhenguo jackzhenguo merged commit 49ac5cf into my-leetcode-csharp Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants