-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionsort.c
71 lines (59 loc) · 1.4 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Selection sort
#include<stdio.h>
void selectionsort(int a[], int n)
{
int pass, min,pos,i;
//in pass 1 entire array is unsorted indices are 0 to n-1
//in pass 2 unsorted array indices are 1 to n-1
//in pass n-1 ie last pass unsorted array indices are n-2 to n-1
for(pass = 1; pass <= n-1; pass++)
{
min = a[pass-1];//first element of unsorted array assumed to be min
pos = pass-1; //note down position of unsorted array.
i = pass;
while( i < n) //compare current min with array ele
{
if( a[i] < min) //update min and position of min
{
min = a[i];
pos = i;
}
i++; //next ele of array
}
a[pos] = a[pass-1]; //exchange min ele with first element of unsorted array
a[pass-1] = min;
}
}
int main()
{
int a[20], n , i;
printf("Enter the number of elements in an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %dth element:",i+1);
scanf("%d",&a[i]);
}
printf("\nBefore sort array is: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
selectionsort(a,n); //call to mergesort
printf("\nAfter final sort array is: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
/* OUTPUT
Enter the number of elements in an array:5
Enter 1th element:4
Enter 2th element:8
Enter 3th element:2
Enter 4th element:0
Enter 5th element:-1
Before sort array is: 4 8 2 0 -1
After final sort array is: -1 0 2 4 8
*/