Create Integer Zigzag
Rearrange integers into alternating peak/valley zigzag pattern (2 algorithms) or generate bouncing sequences. Free, client-side.
Permute a list of integers into an alternating peak/valley zigzag (sort-and-swap or in-place greedy), or generate a bouncing sequence from start → end → start →. Validity check included.
How to Use Create Integer Zigzag
- Pick mode. Permute takes a list of integers and rearranges them into an alternating peak/valley pattern. Generate builds a bouncing sequence between start and end.
- For permute: paste integers (any separator works). Live count shows valid + dropped.
- For permute: pick algorithm. Sort-and-swap always produces a guaranteed-valid zigzag. Greedy is O(n) and preserves more of the original ordering, but may fail on heavy duplicates.
- For generate: set start, end, step, and bounces. 1 bounce means "go once, come back once". 2 means an extra round trip.
- Pick output format. Process (or Ctrl+Enter). Stats line shows peak/valley counts and zigzag-validity verdict.
Frequently Asked Questions
What’s a “zigzag” pattern exactly?
An array a[0], a[1], a[2], … is a zigzag (or “wiggle sort”) if a[0] ≤ a[1] ≥ a[2] ≤ a[3] ≥ a[4] ≤ …. Odd-indexed elements are peaks (≥ both neighbors); even-indexed elements are valleys (≤ both neighbors). The validity check verifies this exact condition.
How does sort-and-swap work?
Sort the array ascending. Then swap pairs at positions (1,2), (3,4), (5,6), …. Example: [1,5,3,8,2,7] sorted = [1,2,3,5,7,8], swap (1,2)→(2,1) → [1,3,2,5,7,8], swap (3,4)→(5,7)→(7,5) → [1,3,2,7,5,8]. Wait – that’s wrong. The actual swap is index (i, i+1) for odd i. So [1,2,3,5,7,8] swap (1,2): [1,3,2,5,7,8], swap (3,4): [1,3,2,7,5,8]. That’s a valid zigzag (1≤3≥2≤7≥5≤8). Sort is O(n log n).
How does in-place greedy work?
Walk left-to-right. At each position i, check the invariant: even-i needs a[i] ≤ a[i+1]; odd-i needs a[i] ≥ a[i+1]. If violated, swap a[i] and a[i+1]. O(n) time. The greedy variant can leave duplicates in problematic positions where strict zigzag is impossible – the validity check will flag this.
When do duplicates cause “NOT valid zigzag”?
If your input has many duplicates of the same value, no rearrangement can satisfy strict ≤/≥ at every position. Example: [5, 5, 5, 5] can only be [5, 5, 5, 5] regardless of algorithm – and that’s not a strict zigzag. The tool emits an info toast and the stats line marks the failure index.
What’s the difference between strict and non-strict zigzag?
Strict (this tool): < and > required. Non-strict: ≤ and ≥. We use the non-strict version (≤, ≥) in the validity check because strict is impossible whenever any adjacent values are equal – which would mark even valid-looking outputs invalid. The validity check follows the standard “wiggle sort” definition used in LeetCode 280.
How does generate mode work?
Builds an ascending sequence from start to end (in step increments), then descends back to start, then ascends, alternating. “Bounces” controls how many round-trip segments to append after the first ascent. Bounces = 0 → just [1, 2, …, 10]. Bounces = 1 → [1, …, 10, 9, …, 1]. Bounces = 2 → [1, …, 10, 9, …, 1, 2, …, 10].
Can I have negative integers?
Yes. The zigzag pattern compares relative values, so signs don’t matter. [-5, 3, -2, 8] works the same as [1, 2, 3, 4] would, conceptually.
What separators does the parser accept?
Newlines, commas, spaces, semicolons, or any combination. Non-integer tokens are silently dropped; the live counter shows the drop count.
Is anything uploaded?
No. All processing happens locally in your browser – nothing is sent to a server, logged, or stored, and the tool keeps working offline once the page has loaded.