-
Notifications
You must be signed in to change notification settings - Fork 40
/
selectionSort.c
49 lines (39 loc) · 918 Bytes
/
selectionSort.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
//The following Program asks for Number of elements you want to give as input
//Then it takes all the elements as input values and arranges them in ascending order using selection sort.
//It also prints the array you have given as input
#include <stdio.h>
#include <stdlib.h>
void selection_sort(int A[],int n);
int main()
{
int a[50],i,j,n,t;
printf("how many element you want to input: ");
scanf("%d",&n);
printf("Now enter %d element one by one\n:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Your elements are:");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t = a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\nThe sorted elements in asscending order are:\n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
}