-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.复杂度分析.c
77 lines (65 loc) · 1.48 KB
/
2.复杂度分析.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
77
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define N 100
int bubble(int a[], int length)
{
int i = 0, j, temp;
int change;
int count = 0;
do
{
change = 0;
for (j = 1; j < length - i; j++)
{
count++;
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
change = 1;
}
}
i = i + 1;
} while (i < length || change == 1);
return count;
}
char *TimeComplexity(int (*func)())
{
int a1[N];
int a2[N * N];
int count1, count2;
char *res = (char *)malloc(sizeof(char) * 20);
srand(1);
for (int i = 0; i < N; i++)
a1[i] = rand() % N;
for (int i = 0; i < N; i++)
a2[i] = rand() % N * N;
count1 = func(a1, N);
count2 = func(a2, N * N);
if (count1 == count2)
strcpy(res, "O(1)");
else if (count1 <= N * N * N * 10 && count1 > N * N * N / 10)
strcpy(res, "O(n³)");
else if (count1 > N * N / 10)
strcpy(res, "O(n²)");
else
strcpy(res, "O(n)");
return res;
}
char *SpaceComplexity(int (*func)())
{
char *res = (char *)malloc(sizeof(char) * 20);
strcpy(res, "O(1)");
return res;
}
int main()
{
char *res1 = TimeComplexity(bubble);
printf("时间复杂度: %s\n", res1);
char *res2 = SpaceComplexity(bubble);
printf("空间复杂度: %s", res2);
return 0;
}