-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathselection_sort.c
More file actions
42 lines (36 loc) · 800 Bytes
/
selection_sort.c
File metadata and controls
42 lines (36 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
int main()
{
int a[20], n, i, j, temp, k, s;
// Taking size of array as input
printf("ENTER THE SIZE OF ARRAY: ");
scanf("%d", &n);
// Taking array elements as input
for (i = 0; i < n; i++)
{
printf("ENTER THE ELEMENTS: ");
scanf("%d", &a[i]);
}
// selection sort
for (int i = 0; i < n - 1; i++)
{
int smallest = i;
for (int j = i + 1; j < n; j++)
{
if (a[smallest] > a[j])
{
smallest = j;
}
}
// swapping
int temp = a[smallest];
a[smallest] = a[i];
a[i] = temp;
}
// Printing sorted array
printf("SORTED ARRAY: ");
for (j = 0; j < n; j++)
{
printf("%d\t", a[j]);
}
}