Skip to content

Commit 96c36f8

Browse files
Ishani08poyea
authored andcommitted
added wiggle_sort.py (TheAlgorithms#734)
* Wiggle_sort * Rename Wiggle_Sort to wiggle_sort.py
1 parent 8e67ac3 commit 96c36f8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sorts/wiggle_sort.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
3+
For example:
4+
if input numbers = [3, 5, 2, 1, 6, 4]
5+
one possible Wiggle Sorted answer is [3, 5, 1, 6, 2, 4].
6+
"""
7+
def wiggle_sort(nums):
8+
for i in range(len(nums)):
9+
if (i % 2 == 1) == (nums[i-1] > nums[i]):
10+
nums[i-1], nums[i] = nums[i], nums[i-1]
11+
12+
13+
print("Enter the array elements:\n")
14+
array=list(map(int,input().split()))
15+
print("The unsorted array is:\n")
16+
print(array)
17+
wiggle_sort(array)
18+
print("Array after Wiggle sort:\n")
19+
print(array)
20+
21+

0 commit comments

Comments
 (0)