From 7217cc506a3562752ae35aee3f53ba5f2dd84845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=E3=83=85?= <85552617+codingmiracle@users.noreply.github.com> Date: Thu, 13 Oct 2022 15:54:40 +0200 Subject: [PATCH] add: selectionsort (#53) --- C/selectionsort.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/C/selectionsort.c b/C/selectionsort.c index 3a95d16..461df66 100644 --- a/C/selectionsort.c +++ b/C/selectionsort.c @@ -4,8 +4,30 @@ void selectionSort(int arr[], int n) { - // your code goes here - // print the sorted array + int index = 0; + int min; + for (int i = 0; i < n; i++) + { + min = arr[i]; + for (int j = i; j < n; j++) + { + if (arr[j] < min) + { + index = j; + min = arr[index]; + } + } + if (arr[i] != arr[index]) + { + arr[i] += arr[index]; + arr[index] = arr[i] - arr[index]; + arr[i] -= arr[index]; + } + } + for (int i = 0; i < n; i++) + { + printf("%d ", arr[i]); + } } int main()