算法的时间复杂度 - JavaScript

问题描述

我需要计算这个程序的时间复杂度。该程序计算排序数组中出现的次数代码在 JS 中。
我需要三个陡坡才能完成这项任务:

  1. 显性(基本)操作
  2. 复杂度函数(最坏情况)
  3. 最坏情况下的复杂程度

有人可以帮我吗?

function count(tab,w,n)
  {
  //index of the first appearance
  idMin = first(tab,n-1,n);
  
  //if 'w' doesn't exists in tab
  if (idMin == -1) { return idMin };
  
  //index of the last appearance
  idMax = last(tab,idMin,n);
  how_many = idMax-idMin+1;
  //return number of occurrences
  return how_many;
  }
    
function first(tab,l,p,n) //index of the first appearance
  {
  if (l <= p)                  //l - beginning index of the searching list,p - last index od the searching list
    {
    mid = Math.floor((l+p)/2); //middle index
    
    if ((mid == 0 || w  > tab[mid-1])&&(tab[mid] == w)) //mid == 0 when tab has one element
      {
      return mid; 
      }
    else if(w > tab[mid])
      {
      return first(tab,(mid+1),n);
      }
    else
      {
      return first(tab,(mid-1),n);
      }
    }
  return -1;
  }
    
function last(tab,n)//index of the last appearance
  {
  if (l <= p)
    {
    mid2 = Math.floor((l+p)/2);
    
    if ((mid2 == n-1 || w < tab[mid2+1]) && (tab[mid2] == w))
      {
      return mid2; 
      }
    else if (w < tab[mid2])
      {
      return last(tab,(mid2-1),n);
      }
    else
      {
      return last(tab,(mid2+1),n);
      }
    } 
  //return -1; it is necessary?
  }

var tab = [1,2,3,4,5,6,7,8,9],w   = 7           // looking value,n   = tab.length // length of the tab
  ;
counts = count(tab,n); 
document.write("Number " + w + " occurred " +counts+ " times<br/>");

w - 我们正在寻找的频率的搜索值,
n - 数组的长度,
tab - 我们的数组,
l - 搜索列表的起始索引,
p - 搜索列表的最后一个索引

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...