From 4b9c8248463ccdbac7557d9674cce1b26395da61 Mon Sep 17 00:00:00 2001 From: Soumyadeep Sinha <43180848+Soumyadeep21@users.noreply.github.com> Date: Tue, 10 Oct 2023 04:19:27 -0500 Subject: [PATCH] Create selection_sort.dart --- dart/selection_sort.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dart/selection_sort.dart diff --git a/dart/selection_sort.dart b/dart/selection_sort.dart new file mode 100644 index 0000000..e8baadc --- /dev/null +++ b/dart/selection_sort.dart @@ -0,0 +1,28 @@ +void selectionSort(List 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 numbers = [64, 25, 12, 22, 11]; + + print("Original array: $numbers"); + + selectionSort(numbers); + + print("Sorted array: $numbers"); +}