-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.cpp
More file actions
54 lines (48 loc) · 817 Bytes
/
Copy pathselectionsort.cpp
File metadata and controls
54 lines (48 loc) · 817 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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 20
int small(int arr[],int k,int n);
void selection(int arr[],int n);
int small(int arr[],int k,int n)
{
int pos=k,small=arr[k],i;
for(i=k+1;i<n;i++)
{
if(arr[i]<small)
{
small=arr[i];
pos=i;
}
}
return pos;
}
void selection(int arr[],int n)
{
int k,pos,temp;
for(k=0;k<n;k++)
{
pos=small(arr,k,n);
temp=arr[k];
arr[k]=arr[pos];
arr[pos]=temp;
}
}
int main()
{
int i,j,n,temp,arr[10];
printf("enter the number of elements in an array:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
selection(arr,n);
printf("\n the array sorted is: \n");
for(i=0;i<n;i++)
{
printf("%d \n",arr[i]);
}
return 0;
}