# Wiggle Sort in Java

An array arr[] is given to us that contains n integers. Our task is to sort the array in such a way that a wiggle sequence is formed. If multiple wiggle sequence is there, then print anyone of them. A wiggle sequence for an array satisfies the following condition:

arr <= arr >= arr <= arr >= arr <= arr …

Example: 1

Input:

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Output: {1, 6, 2, 7, 3, 8, 4, 9, 5}

Explanation: The mentioned sequence forms a wiggle sequence. Other valid sequences are: {5, 9, 1, 6, 3, 8, 2, 7, 4}, {2, 8, 3, 5, 4, 6, 1, 9, 7}, {1, 9, 2, 8, 3, 7, 4, 5}, and { 1, 3, 2, 5, 4, 7, 6, 9, 8}

Example: 2

Input:

int arr[] = {8, 12, 10, 11, 9}

Output: {8, 12, 9, 11, 10}

Explanation: The mentioned sequence forms a wiggle sequence. Other valid sequences are: {9, 11, 10, 12, 8}, {10, 12, 9, 11, 8}, and {8, 12, 10, 11, 9}

Example: 3

Input:

int arr[] = {-1, 0, 1, -2, -5}

Output: {-1, 0, -2, 1, -5}

Explanation: The mentioned sequence forms a wiggle sequence. Other valid sequences are: {0, 1, -5, -1, -2}, {-5, 0, -2, 1, -1}, {-5, 1, -2, 0, -1}, and { -1, 1, -2, 0, -5}

## Approach: Using Sorting

The sequence arr <= arr >= arr <= ar >= arr <= arr ….. can be achieved if we place larger elements at the odd position of the array, i.e., first position, third position, fifth position, and so on, and the smaller elements are placed at the even position, i.e., 0th position, 2nd position, 4th position, and so on. Now the task is to find the smaller and larger elements, which can be accomplished if we sort the elements. After sorting, the elements on the left sides are the smaller elements, and the elements on the right sides are the larger elements. Now, we will use two pointers, one will start from the leftmost side of the sorted array, and the other pointer will start from the rightmost side of the sorted array. Using a loop, we will increment and decrement the pointers. In each iteration, we will increment the pointer that is pointing to the smaller elements by one and will decrement the pointer that is pointing to the larger elements by one.

FileName: WiggleSort.java

Output:

```For the input Array:
1 2 3 4 5 6 7 8 9
The wiggle sequence is:
1 9 2 8 3 7 4 6 5

For the input Array:
8 12 10 11 9
The wiggle sequence is:
8 12 9 11 10

For the input Array:
-1 0 1 -2 -5
The wiggle sequence is:
-5 1 -2 0 -1
```

Complexity Analysis: Because of sorting, the time complexity of the program is O(n x log(n)). Also, the program uses an array list for storing the result, making the space complexity of the program O(n), where n is the total number of elements present in the input array.

If we observe, there is no need to sort the array. We can swap adjacent pairs of elements if they do not follow the wiggle sequence. In this way, not only we avoid the sorting of the input array, but we also escape from using the extra space that we are using in the previous program to store the results in an array list. The following program shows the same.

FileName: WiggleSort1.java

Output:

```For the input Array:
1 2 3 4 5 6 7 8 9
The wiggle sequence is:
1 3 2 5 4 7 6 9 8

For the input Array:
8 12 10 11 9
The wiggle sequence is:
8 12 10 11 9

For the input Array:
-1 0 1 -2 -5
The wiggle sequence is:
-1 1 -2 0 -5
```

Complexity Analysis: Since the program is using a for-loop, the time complexity of the program is O(n), where n is the total number of elements present in the input array. The space complexity of the program is constant, i.e., O(1).

Next TopicJava DOM

### Feedback   