-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.c
64 lines (57 loc) · 1.17 KB
/
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
#include<stdio.h>
int main()
{
int arr[]={5,4,3,2,1};
int n=5,index;
int a[]={5,2,1,4,7};
int m=5,point;
//Sorting by placing MAX elements
for(int i=0;i<n;i++)
{
int max=0;
for(int j=0;j<(n-i);j++)
{
if(max<arr[j])
{
max=arr[j];
index=j;
}
}
if(index!=(n-i-1))
{
int temp=arr[index];
arr[index]=arr[n-i-1];
arr[n-i-1]=temp;
}
}
//Sorting by placing MIN elements
for(int i=0;i<m;i++)
{
int min=a[m-1];
for(int j=i;j<m;j++)
{
if(min>a[j])
{
min=a[j];
point=j;
}
}
if(point!=i)
{
int temp=a[point];
a[point]=a[i];
a[i]=temp;
}
}
printf("First sorted set(using max elements) \n");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n Second sorted set(using min elements) \n");
for(int i=0;i<m;i++)
{
printf("%d ",a[i]);
}
return 0;
}