forked from nitinsultania/CPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.c
More file actions
47 lines (40 loc) · 750 Bytes
/
selectionsort.c
File metadata and controls
47 lines (40 loc) · 750 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
//Selection Sort
#include<stdio.h>
void selsort(int arr[],int n)
{
int k,temp;
for (int i = 0; i < n-1; i++)
{
int k=i;
for (int j = i+1; j < n; j++)
{
if(arr[k]>arr[j])
{
k=j;
}
}
temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
}
}
int main()
{
int n;
printf("\nEnter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("\nEnter the elements: ");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
selsort(arr,n);
printf("\nAfter Sorting Elements are: ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}