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()