forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added wiggle_sort.py (TheAlgorithms#734)
* Wiggle_sort * Rename Wiggle_Sort to wiggle_sort.py
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... | ||
For example: | ||
if input numbers = [3, 5, 2, 1, 6, 4] | ||
one possible Wiggle Sorted answer is [3, 5, 1, 6, 2, 4]. | ||
""" | ||
def wiggle_sort(nums): | ||
for i in range(len(nums)): | ||
if (i % 2 == 1) == (nums[i-1] > nums[i]): | ||
nums[i-1], nums[i] = nums[i], nums[i-1] | ||
|
||
|
||
print("Enter the array elements:\n") | ||
array=list(map(int,input().split())) | ||
print("The unsorted array is:\n") | ||
print(array) | ||
wiggle_sort(array) | ||
print("Array after Wiggle sort:\n") | ||
print(array) | ||
|
||
|