Skip to content

Instantly share code, notes, and snippets.

@suryansh011
Created October 1, 2021 16:23
Show Gist options
  • Save suryansh011/a57466421ef6f9a1f40a3c04651b8924 to your computer and use it in GitHub Desktop.
Save suryansh011/a57466421ef6f9a1f40a3c04651b8924 to your computer and use it in GitHub Desktop.
Pure Selection Sort in C
/* 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