-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.c
81 lines (76 loc) · 990 Bytes
/
selection_sort.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdlib.h>
#include<stdio.h>
void menu();
void insert();
void display();
void selection_sort();
int n=0,a[20];
int main()
{
while(1)
menu();
}
void menu()
{
int ch;
printf("\n-----------menu-----------\n1.Insert\n2.Selection_sort\n3.Display\n4.Exit");
printf("\nchoice");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:selection_sort();
break;
case 3:display();
break;
case 4:exit(1);
break;
default:
printf("\ninvalid");
}
}
void insert()
{
int i;
printf("\nEnter the n");
scanf("%d",&n);
if(n>20)
{
printf("\n invalid");
}
else
{
for(i=0;i<n;i++)
{
printf("\nentre the element=%d",i+1);
scanf("%d",&a[i]);
}
}
}
void display()
{
int i;
for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
}
void selection_sort()
{
int i,j,min,m;
for(i=0 ; i<n-1 ; i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
m=a[i];
a[i]=a[min];
a[min]=m;
}
}