-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlin_sear.c
48 lines (42 loc) · 870 Bytes
/
lin_sear.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
#include <stdio.h>
typedef struct
{
int smallest;
int index;
}
sorting;
int main(void)
{
// sorting done[1];
// Array of integers.
int nums[] = {5, 6, 3, 1, 0, 4, 7, 2};
printf("Before sorting - ");
for (int i = 0; i < 8; i++)
{
printf("%i ", nums[i]);
}
printf("\n");
// Sorting the array through selection sort.
for (int i = 0; i < 8; i++)
{
int smallest = 8;
int index = 0;
for (int j = i; j < 8; j++)
{
if (nums[j] < smallest)
{
smallest = nums[j];
index = j;
}
}
// Swaping the numbers.
nums[index] = nums[i];
nums[i] = smallest;
}
printf("After sorting - ");
for (int i = 0; i < 8; i++)
{
printf("%i ", nums[i]);
}
printf("\n");
}