未分类ñ于阵列算法找出使用2嵌套循环重复 [英] Unsorted n array- algorithm to find duplicates using 2 nested loops

查看:127
本文介绍了未分类ñ于阵列算法找出使用2嵌套循环重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题是:

  

什么是属于你的算法的大O的复杂性?解释你的答案,并描述了发生在最坏的情况下操作的数量。

我还没有得到很到 - 您没有回答,如果你不想要,但再一次,我将大大AP preciate你们的帮助,并会帮助我学习,通过一些示范性的答案。谢谢:)

再次!这是不是就该功课,这样的..我试图解决一些答案(我通常在我的业余时间),我知道这里的​​人要好得多的教学比我的导师(谁很难讲英语)的问题。谢谢大家。

解决方案

 布尔IsHaveDup(INT [] myArray的,INT ARRAYSIZE)
{
    INT I,J;
    布尔isHaveDup = FALSE;

    对于(i = 0; I< ARRAYSIZE  -  1;我++)
    {
        为(J = I + 1; J< ARRAYSIZE; J ++)
        {
            如果(myarray的[I] == myArray的研究[J])
            {
                isHaveDup = TRUE;
                打破;
            }
        }

        如果(isHaveDup)
        {
            打破;
        }
    }

    返回isHaveDup;
}
 

的复杂度为O(N ^ 2)= N *((N + 1)/ 2)

一个更好的解决方案是将数组排序,然后检查复制 那么复杂度为O(N * logN)的分类和阵列的复制Ø在一个快速环路(N) 总计:O(N)+ O(N * logN)的= O(NlogN)

The following question was:

What is the Big O complexity of your algorithm in part? Explain your answer and describe the number of operations that take place in the worst case.

I haven't yet quite gotten to that -you don't have to answer it if you don't want but again, I will greatly appreciate your help and will help me to learn through some exemplar answers. Thank you :)

Again! this is not a question in relation to homework and such.. I'm trying to solve some answers (I usually do in my spare time) and I know people here are much better at teaching than my tutors (who hardly speaks English). Thank you everyone.

解决方案

bool IsHaveDup(int[] myArray, int arraySize)
{
    int i, j;
    bool isHaveDup = false;

    for (i = 0; i < arraySize - 1; i++)
    {
        for (j = i + 1; j < arraySize; j++)
        {
            if (myArray[i] == myArray[j])
            {
                isHaveDup = true;
                break;
            }
        }

        if (isHaveDup)
        {
            break;
        } 
    }

    return isHaveDup;
}

The complexity is O(N^2) = N * ((N + 1)/2)

A better solution is to sort the array and then check for duplication then complexity is O(N*logN) for sorting and one quick loop on the array for duplication O(N) In total: O(N)+O(n*logN) = O(NlogN)

这篇关于未分类ñ于阵列算法找出使用2嵌套循环重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆