Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Open
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
38 changes: 38 additions & 0 deletions algorithms/sorting/Odd-EvenSort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function oddEvenSort(arr)
n = length(arr);
sorted = false;

while ~sorted
sorted = true;

% Odd phase (compare and swap odd-indexed elements)
for i = 1:2:(n-1)
if arr(i) > arr(i + 1)
temp = arr(i);
arr(i) = arr(i + 1);
arr(i + 1) = temp;
sorted = false;
end
end

% Even phase (compare and swap even-indexed elements)
for i = 2:2:(n-1)
if arr(i) > arr(i + 1)
temp = arr(i);
arr(i) = arr(i + 1);
arr(i + 1) = temp;
sorted = false;
end
end
end
end

% Example usage
arr = [64, 34, 25, 12, 22, 11, 90];
disp("Unsorted List:");
disp(arr);

oddEvenSort(arr);

disp("Sorted List:");
disp(arr);