-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortTest.c
57 lines (50 loc) · 1.04 KB
/
sortTest.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
// sortTest.c: This sort method that I'm not sure about.
#include <stdio.h>
void int_swap(int *a, int *b);
int main(void)
{
/**
* nums[][0]: value.
* nums[][1]: sequence.
*
*/
int n_nums;
scanf("%d", &n_nums);
int nums[n_nums][2];
// Input nums.
for (int i = 0; i < n_nums; ++i)
{
scanf("%d", &nums[i][0]);
nums[i][1] = i + 1;
}
// Sort.
for (int i = 0; i < n_nums; ++i)
{
for (int j = i + 1; j < n_nums; ++j)
{
// Sort in acsending order.
// Commence swap if a>b.
if (nums[i][0] > nums[j][0])
{
int_swap(&nums[i][0], &nums[j][0]);
int_swap(&nums[i][1], &nums[j][1]);
}
}
}
// Output nums.
for (int i = 0; i < n_nums; ++i)
{
printf("%d(%d) ", nums[i][0], nums[i][1]);
}
printf("\n");
return 0;
}
void int_swap(int *a, int *b)
{
if (*a != *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
}