aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/tests/sysy_lvX/select_sort.sy
blob: 33a6f0a5d1f83c0d1c246646b6eece02158b0c11 (plain)
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
int arr[100];

void selectSort(int n)
{
	int i = 0;
	int j = 0;
	int minValue;
	int tmp;
	while (i < n - 1)
	{
		minValue = i;
		j = i + 1;
		while (j < n)
		{
			if (arr[minValue] > arr[j])
			{
				minValue = j;
			}
			j = j + 1;
		}
		if (minValue != i)
		{
			tmp = arr[i];
			arr[i] = arr[minValue];
			arr[minValue] = tmp;
		}
		i = i + 1;
	}
}

void printArray(int n)
{
	int i = 0;
	while (i < n)
	{
        putint(arr[i]);
		putch(32);
		// printf("%d ", arr[i]);
		i = i + 1;
	}
}

int main()
{
	int n;
	int i = 0;
    n = getint();
    while (i < n)
	{
        arr[i] = getint();
        i = i + 1;
	}
	selectSort(n);
	printArray(n);
	return 0;
}