-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection_sort.c
54 lines (54 loc) · 966 Bytes
/
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void selectionSort(int A[],int n)
{
int i,least,POS,j,temp;
for(i=0;i<n;i++)
{
least=A[i];
POS=i;
for(j=0;j<n;j++)
{
if(A[j]<least)
{
least=A[j];
POS=j;
}
}
}
if(i!=POS)
{
temp=A[i];
A[i]=A[POS];
A[POS]=temp;
}
}
void display(int A[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
int main()
{
int A[100000],n,i;
clock_t start,end,diff;
printf("Enter n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
A[i]=rand();
}
display(A,n);
start =clock();
selectionSort(A,n);
end=clock();
display(A,n);
diff=end-start;
printf("Time Taken is %f second\n",(float)diff/CLOCKS_PER_SEC);
return 0;
}