Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit96c36f8

Browse files
Ishani08poyea
authored andcommitted
added wiggle_sort.py (TheAlgorithms#734)
* Wiggle_sort* Rename Wiggle_Sort to wiggle_sort.py
1 parent8e67ac3 commit96c36f8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎sorts/wiggle_sort.py

Lines changed: 21 additions & 0 deletions
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+
defwiggle_sort(nums):
8+
foriinrange(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)

[8]ページ先頭

©2009-2025 Movatter.jp