Skip to content

Commit cf5c406

Browse files
authored
Create selectionsort.c
1 parent cb6c0fd commit cf5c406

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

selectionsort.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
void swap(int *p, int *q)
5+
{
6+
int temp;
7+
temp = *p;
8+
*p = *q;
9+
*q = temp;
10+
}
11+
12+
void SelectionSort(int A[],int n)
13+
{
14+
int i,j,k;
15+
for(i=0;i<n;i++){
16+
for(j=k=i;j<n;j++)
17+
{
18+
if(A[k]>A[j])
19+
k=j;
20+
}
21+
swap(&A[i],&A[k]);
22+
}
23+
}
24+
25+
26+
int main()
27+
{
28+
int A[10] = {1,5,2,1,6,3,6,2,5,10};
29+
int i;
30+
SelectionSort(A,10);
31+
for(i=0;i<10;i++)
32+
{
33+
printf("%d ",A[i]);
34+
}
35+
return 0;
36+
}

0 commit comments

Comments
 (0)