我想弄清楚如何给出最坏的情况时间复杂度.我不确定我的分析.我已经读过for大O的嵌套循环了n^2; 这对于一个内部for循环的while循环是否正确?
// A is an array of real numbers.
// The size of A is n. i,j are of type int, key is
// of type real.
Procedure IS(A)
for j = 2 to length[A]
{
key = A[ j ]
i = j-1
while i>0 and A[i]>key
{
A[i+1] = A[i]
i=i-1
}
A[i+1] = key
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我有:
j=2 (+1 op)
i>0 (+n ops)
A[i] > key (+n ops)
so …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是,例如当用户输入7时,显示屏显示:
0 11 2 3 5 8 13 21 child ends.
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚如何修复11,为什么它在序列中显示了很多数字!有人可以帮忙吗?
序列号将在命令行中提供.例如,如果提供5,则Fibonacci序列中的前五个数字将由子进程输出.由于父进程和子进程具有自己的数据副本,因此子进程必须输出序列.让父进程调用wait()调用以等待子进程在退出程序之前完成.执行必要的错误检查以确保在命令行上传递非负数.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int a=0, b=1, n=a+b,i,ii;
pid_t pid;
printf("Enter the number of a Fibonacci Sequence:\n");
scanf("%d", &ii);
if (ii < 0)
printf("Please enter a non-negative integer!\n");
else
{
pid = fork();
if (pid == 0)
{
printf("Child is producing the Fibonacci Sequence...\n");
printf("%d %d",a,b);
for (i=0;i<ii;i++)
{
n=a+b;
printf("%d ", n);
a=b;
b=n;
}
printf("Child ends\n");
}
else
{
printf("Parent …Run Code Online (Sandbox Code Playgroud)