-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.c
51 lines (50 loc) · 806 Bytes
/
selection.c
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
43
44
45
46
47
48
49
50
51
#include <stdio.h>
void input(int n ,int a[]);
int selection(int n ,int a[]);
void display(int n, int a[]);
void swap(int *a,int *b);
void main()
{
int n, a[10];
printf("how many the elements\n");
scanf("%d",&n);
printf("enter the elements\n");
input(n,a);
selection(n,a);
display(n,a);
}
void input(int n ,int a[])
{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
int selection(int n ,int a[])
{
int i,small,j,index;
for(i=0;i<n-1;i++)
{
small= a[i];
index= i;
for(j=i+1;j<n;j++)
if(small>a[j])
{
small=a[j];
index=j;
}
swap(&a[i],&a[index]);
}
}
void display(int n, int a[])
{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}