Created
October 1, 2021 16:23
-
-
Save suryansh011/a57466421ef6f9a1f40a3c04651b8924 to your computer and use it in GitHub Desktop.
Pure Selection Sort in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Selection Sort Algorithm */ | |
#include <stdio.h> | |
int main() { | |
int i, j; | |
int min, temp; | |
int c = 0, s = 0; | |
int n; | |
scanf("%d", &n); | |
int arr[n]; | |
for(i = 0; i < n; i++) | |
scanf("%d", &arr[i]); | |
for (i = 0; i < n; i++) { | |
min = i; | |
for (j = i + 1; j < n; j++) { | |
c++; | |
if(arr[j] < arr[min]) { | |
min = j; | |
} | |
} | |
s++; | |
/* Swapping */ | |
temp = arr[min]; | |
arr[min] = arr[i]; | |
arr[i] = temp; | |
} | |
for (i = 0; i < n; i++) | |
printf("%d ", arr[i]); | |
printf("\n%d\n%d", c, s-1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment