-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelection_sort.c
76 lines (69 loc) · 1.18 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Program to sort an array according to selection sort algorithm
*/
#include <stdio.h>
void lstp(int n, int *l)
{
int i=0;
for (i=0;i<n;i++)
{
printf("%d ",l[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d",&n);
int ar[n];
int i=0,j=0;
for (i=0;i<n;i++)
{
printf("Element %d: ",i+1);
scanf("%d",&ar[i]);
}
printf("Entered array is: ");
lstp(n,ar);
for (i=0;i<n;i++)
{
int s = i;
for (j=i+1;j<n;j++)
{
if (ar[j]<ar[s])
{
s=j;
}
}
if (s != i)
{
int t=ar[s];
ar[s]=ar[i];
ar[i]=t;
}
// lstp(n,ar); // Uncomment to get the sorting mechanism after every n iterations
}
printf("Sorted list is: ");
lstp(n,ar);
}
/*
Input:
Enter the size of array: 5
Element 1: 63
Element 2: -9
Element 3: 23
Element 4: 5
Element 5: 10
Output:
Entered array is: 63 -9 23 5 10
Sorted list is: -9 5 10 23 63
*/
/*
Mechanism:
63 -9 23 5 10
-9 63 23 5 10
-9 5 23 63 10
-9 5 10 63 23
-9 5 10 23 63
-9 5 10 23 63
*/