首页 \ 问答 \ 用于查找最小数量的递归算法的大O()(Big O() of recursive algorithm for finding minimum number)

用于查找最小数量的递归算法的大O()(Big O() of recursive algorithm for finding minimum number)

我有以下代码告诉findmin是O(n ^ 2),但我看不到它。

#include <iostream>
#include <cstdlib>
#include <ctime>

int findmin(const int a[], int n);
int cnt = 0;

int main (void)
{
    int num = 100;
    std::srand(std::time(nullptr));

    int arr[num];
    for (int i = 0; i < num; ++i)
        arr[i] = std::rand() % num;

    findmin(arr, num);
    std::cout << cnt;
    return 0;
}

int findmin(const int a[], int n)
{
    cnt++;
    if(n == 0)
        return a[0];

    int min;
    return a[n] < (min = findmin(a, n - 1)) ? a[n] : min;
}

在我看来,这个算法是最后一个元素,抓取它并从递归回来,将它与元素进行比较并继续,因此在我看来,这是O(2 * n)。

换句话说,如果我们有数组3,5,7,1,2,6,递归地我们走下去grub 6.在我们的路上,我们发现2小于6,所以我们grub 2.比起我们去比较它到1 grub 1并上去,比较它到7,5,3。所以这是O(n)。 我们正在经历n次阵列。 如果有人能向我解释这一点,我将非常感激。

这里是我的源https://stackoverflow.com/a/50034011/5550963


I have following code I was told findmin is O(n^2), but I cannot see it.

#include <iostream>
#include <cstdlib>
#include <ctime>

int findmin(const int a[], int n);
int cnt = 0;

int main (void)
{
    int num = 100;
    std::srand(std::time(nullptr));

    int arr[num];
    for (int i = 0; i < num; ++i)
        arr[i] = std::rand() % num;

    findmin(arr, num);
    std::cout << cnt;
    return 0;
}

int findmin(const int a[], int n)
{
    cnt++;
    if(n == 0)
        return a[0];

    int min;
    return a[n] < (min = findmin(a, n - 1)) ? a[n] : min;
}

In my mind this algorithm goes down the last element, grabs it and comes back from recursion, compares it to an element and goes on, thus in my mind this is O(2*n).

In another way if we have array 3, 5, 7, 1, 2, 6, recursively we go down and grub 6. On our way up we find that 2 is smaller than 6 so we grub 2. Than we go up and compare it to 1 grub 1 and go up, comparing it to 7, 5, 3. So that is O(n). We are going through array n times. I would really appreciate if somebody can explain this to me.

Here is my source https://stackoverflow.com/a/50034011/5550963


原文:https://stackoverflow.com/questions/50047701
更新时间:2022-09-28 14:09

最满意答案

JVM不强制本地方法的类型安全。 它只关心这个名字。

例如下面的代码可以很好地编译,并且库将被成功加载。 但是,当您调用nativeMethod ,JVM将崩溃,因为它将把返回值-1视为Object。

Test.java

public class Test {

    public static void main(String[] args) throws Exception {
        System.loadLibrary("test");
        new Test().nativeMethod(0, 1);
    }

    private native Object nativeMethod(long a, int b);
}

test.c的

#include <jni.h>

JNIEXPORT jint JNICALL Java_Test_nativeMethod(JNIEnv* env, jobject self) {
    return -1;
}

如果您想保留有关动态链接的参数类型的信息,则必须以某种方式将其编码到函数名称中。 请参见名称修改


JVM does not enforce type safety of native methods. It only cares about the name.

E.g. the following code will compile fine, and the library will be successfully loaded. However, when you call nativeMethod, JVM will crash, because it will treat return value -1 as an Object.

Test.java

public class Test {

    public static void main(String[] args) throws Exception {
        System.loadLibrary("test");
        new Test().nativeMethod(0, 1);
    }

    private native Object nativeMethod(long a, int b);
}

test.c

#include <jni.h>

JNIEXPORT jint JNICALL Java_Test_nativeMethod(JNIEnv* env, jobject self) {
    return -1;
}

If you want to retain information about argument types for dynamic linking, you'll have to encode it somehow in the function name. See Name mangling.

相关问答

更多
  • 这可能是由于指针指向已删除的对象。 检查您的本机代码。 I realized much later that jni throws signals that normally it would catch but in gdb, it causes gdb to report crash. My code had other very mundane bugs that I was not able to catch because I was relying on gdb to debug.
  • 如何在HotSpot JVM中使用JNI方法 本机方法可以与VM操作(包括GC)同时运行。 他们不会停在安全的地方 。 GC可能会移动Java对象,即使它们是从正在运行的本机方法引用的。 jobject句柄只是一个不可移动的对象引用数组的索引。 无论何时移动对象,相应的数组插槽都会更新,尽管索引保持不变。 就是说, jobject句柄仍然有效。 每当本地方法调用JNI函数时,它都会检查JVM是否处于安全点状态。 如果是(例如GC正在运行),JNI功能会阻止,直到完成安全点操作。 在执行像GetByteArr ...
  • JVM不强制本地方法的类型安全。 它只关心这个名字。 例如下面的代码可以很好地编译,并且库将被成功加载。 但是,当您调用nativeMethod ,JVM将崩溃,因为它将把返回值-1视为Object。 Test.java public class Test { public static void main(String[] args) throws Exception { System.loadLibrary("test"); new Test().nativeM ...
  • 究竟是什么触发了错误信息? 此错误并非我的应用程序所独有,因此该错误存在哪些文档? 错误消息是正确的。 我的自定义安装出了问题。 显然,文件在从可执行文件中提取时丢失了数据。 在目前安装的JRE中进行字面复制后,一切正常! 为什么复制的JRE(它是已安装的JRE的精确副本)不能按预期工作? 因为它不是“精确”的副本。 我如何纠正这种情况,以便我可以使用这个解压缩的JRE? 这不再是一个问题,因为这是我的错。 提取没有正确完成。 PS:享受JNI代码,希望它能帮助后来的人做到这一点! What exactly ...
  • 您只能在每个进程中创建一个VM,并获得一个JNIEnv 。 一些非常古老的JVM实现曾经支持创建多个VM,但不再是。 请参阅Oracle Java 7 JNI文档中的 JNI_CreateJavaVM : 从JDK / JRE 1.2开始,不支持在单个进程中创建多个VM。 和IBM的JDK 7 JNI文档 : IBM i上的Java支持在单个作业或进程中仅创建一个Java虚拟机(JVM)。 (据推测,同样的限制适用于IBM AIX JVM) 在这里更详细: 您无法在作业中多次成功调用JNI_CreateJa ...
  • 我知道这个问题很老但是...... 要查找数组的类,请使用: env->FindClass("[[Lmy/package/MyClass;") I know this question is old but... To find a class of your array, use: env->FindClass("[[Lmy/package/MyClass;")
  • 虽然我仍然没有解决在JNI_OnLoad中加载另一个JNI库的问题,但我找到了第一个被调用的本地函数,并将其替换,并在该函数中运行loadLibrary代码。 然后我再次从JNI调用本地方法,并运行该方法的新版本。 While I still don't have a solution for the problem of loading another JNI library in JNI_OnLoad, I found the first native function that is called, ...
  • 你得到的错误是在这一行: jint rc = JNI_CreateJavaVM(&jvm, (void**)&&env, &vm_args); 你要求指向env的指针的地址; 而如果你看一下参考示例来源 : #include /* where everything is defined */ ... JavaVM *jvm; /* denotes a Java VM */ JNIEnv *env; //