Skip to content

Added wave sort a new sorting technique #11538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions sorts/wave_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def wave_sort(arr):
if len(arr) <= 1:
return arr

# Step 1: Divide the array into smaller sub-arrays
mid = len(arr) // 2
left_half = wave_sort(arr[:mid])
right_half = wave_sort(arr[mid:])

# Step 2: Sort each sub-array individually
left_half.sort()
right_half.sort()

# Step 3: Shuffle the sorted sub-arrays in a wave-like pattern
shuffled_array = []
i, j = 0, 0
toggle = True

Check failure on line 18 in sorts/wave_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/wave_sort.py:18:1: W293 Blank line contains whitespace
while i < len(left_half) and j < len(right_half):
if toggle:
shuffled_array.append(left_half[i])
i += 1
else:
shuffled_array.append(right_half[j])
j += 1
toggle = not toggle

Check failure on line 27 in sorts/wave_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/wave_sort.py:27:1: W293 Blank line contains whitespace
# Append remaining elements if any
shuffled_array.extend(left_half[i:])
shuffled_array.extend(right_half[j:])

# Step 4: Merge the sub-arrays back together
final_array = []
n = len(shuffled_array)
for k in range(n):
if k % 2 == 0:
final_array.append(shuffled_array[k])
else:
final_array.append(shuffled_array[-(k // 2 + 1)])

Check failure on line 40 in sorts/wave_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/wave_sort.py:40:1: W293 Blank line contains whitespace
# Step 5: Final pass to ensure complete sorting
for l in range(1, len(final_array)):

Check failure on line 42 in sorts/wave_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

sorts/wave_sort.py:42:9: E741 Ambiguous variable name: `l`
key = final_array[l]
m = l - 1
while m >= 0 and final_array[m] > key:
final_array[m + 1] = final_array[m]
m -= 1
final_array[m + 1] = key

Check failure on line 49 in sorts/wave_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/wave_sort.py:49:1: W293 Blank line contains whitespace
return final_array

# Test the Wave Sort algorithm
arr = [34, 7, 23, 32, 5, 62, 32, 2, 43]
sorted_arr = wave_sort(arr)
print("Original array:", arr)
print("Sorted array using Wave Sort:", sorted_arr)
Loading