-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.c
More file actions
42 lines (41 loc) · 780 Bytes
/
selectionSort.c
File metadata and controls
42 lines (41 loc) · 780 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>
void selectionSort(int a[], int n)
{
int temp;
int position;
int i, j;
for (i = 0; i < n - 1; i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (a[position] > a[j])
{
position = j;
}
}
if (position != i)
{
temp = a[position];
a[position] = a[i];
a[i] = temp;
}
}
}
int main()
{
int i, a[100], n;
printf("Enter the size of array:\n");
scanf("%d", &n);
printf("Enter the element:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
selectionSort(a, n);
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}