-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Selection_sort.c
More file actions
63 lines (55 loc) · 865 Bytes
/
02_Selection_sort.c
File metadata and controls
63 lines (55 loc) · 865 Bytes
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
/*
02
Selection Sort
Name: Sayooj K
Roll no: 45
*/
#include <stdio.h>
int selectionSort(int array[], int size) {
int i, j, pos, temp;
for (i=0; i<size; i++) {
pos = i;
for (j=i; j<size; j++) {
if (array[j] < array[pos]) {
pos =j;
}
}
if (i != pos) {
temp = array[i];
array[i] = array[pos];
array[pos] = temp;
}
}
}
int main() {
int i, j, pos, array[100], temp, size;
printf("Enter number of elements in the array : ");
scanf("%d", &size);
printf("Enter the array\n");
for (i=0; i<size; i++) {
scanf("%d", &array[i]);
}
selectionSort(array, size);
printf("Sorted array \n");
for (i=0; i<size; i++) {
printf("%d\n", array[i]);
}
}
/*
OUTPUT:
Enter number of elements in the array :6
Enter the array
5
2
3
8
0
7
Sorted array
0
2
3
5
7
8
*/