Skip to content
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
28 changes: 28 additions & 0 deletions dart/selection_sort.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
void selectionSort(List<int> arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;

// Find the index of the minimum element in the remaining unsorted part of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// Swap the minimum element with the current element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

void main() {
List<int> numbers = [64, 25, 12, 22, 11];

print("Original array: $numbers");

selectionSort(numbers);

print("Sorted array: $numbers");
}