From 16286d646fb269899e0f6a4f16cef3f645249ad9 Mon Sep 17 00:00:00 2001 From: marizvi <202051121@iiitvadodara.ac.in> Date: Wed, 13 Oct 2021 12:56:59 +0530 Subject: [PATCH] selection sort dart added --- SORT[DART]/selection_sort_dart.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 SORT[DART]/selection_sort_dart.dart diff --git a/SORT[DART]/selection_sort_dart.dart b/SORT[DART]/selection_sort_dart.dart new file mode 100644 index 00000000..7c43b316 --- /dev/null +++ b/SORT[DART]/selection_sort_dart.dart @@ -0,0 +1,28 @@ +void selectionSort(List list) { + if (list == null || list.length == 0) return; + int n = list.length; + int j, i; + for (i = 0; i < n; i++) { + int min = list[i]; + int temp_pos = i; + for (j = i + 1; j < n; j++) { + if (min > list[j]) { + min = list[j]; + temp_pos = j; + } + } + swap(list, i, temp_pos); + } +} + +void swap(List list, int i, int j) { + int temp = list[i]; + list[i] = list[j]; + list[j] = temp; +} + +void main() { + List l = [14, 2, 17, 1, 9]; + selectionSort(l); + print(l); +}