forked from Nihal-Priyadarshi/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sort.c
More file actions
55 lines (41 loc) · 846 Bytes
/
selection-sort.c
File metadata and controls
55 lines (41 loc) · 846 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
43
44
45
46
47
48
49
50
51
52
53
54
//using selection sort..sorting an array in ascending order.....
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,*p,i,j,min,temp,k;
printf("enter the size of an array:\n");
scanf("%d",&n);
p=(int*)calloc(n,4);
if(p==NULL)
{
printf("error!!");
exit(1);
}
printf("enter the elements of the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
printf(" after sorting the array in ascending order using selection sort...\n");
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(*(p+j)<*(p+min))
{
min=j;
}
}
temp=*(p+i);
*(p+i)=*(p+min);
*(p+min)=temp;
printf("modified array:");
for(k=0;k<n;k++)
{
printf(" %d",*(p+k));
}
printf("\n");
}
}