Skip to content
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

Create odd-even_Sort.dart #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
37 changes: 37 additions & 0 deletions sort/odd-even_Sort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
void oddEvenSort(List<int> arr) {
bool sorted = false;
int n = arr.length;

while (!sorted) {
sorted = true;

// Perform the odd phase (compare and swap odd-indexed elements)
for (int i = 1; i < n - 1; i += 2) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
sorted = false;
}
}

// Perform the even phase (compare and swap even-indexed elements)
for (int i = 0; i < n - 1; i += 2) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
sorted = false;
}
}
}
}

void main() {
List<int> arr = [64, 34, 25, 12, 22, 11, 90];
print("Original List: $arr");

oddEvenSort(arr);

print("Sorted List: $arr");
}