Skip to content

Commit c94ca7b

Browse files
Create Wave_Array_Transformation.py
Added new algorithm
1 parent e9e7c96 commit c94ca7b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Description: Sort an array in a wave-like form. In this form, arr[0] >= arr[1] <= arr[2] >= arr[3]...
2+
3+
#Input: [10, 5, 6, 3, 2, 20, 100, 80]
4+
#Output: [20, 10, 6, 3, 5, 2, 80, 100]
5+
#Key Idea: Sort the array and swap every adjacent pair of elements.
6+
7+
def wave_array(arr):
8+
arr.sort()
9+
for i in range(0, len(arr)-1, 2):
10+
arr[i], arr[i+1] = arr[i+1], arr[i]
11+
return arr

0 commit comments

Comments
 (0)