以下算法找到最大QAZ。元素的QAZ是在该元素的索引之后具有更高值的元素的数量。流行的答案是使用除法和解决使用O(nLog(n))..
数组未排序。在最坏的情况下,“Divide& Conquer”将触及所有n个元素。另外'for loop'会触及n / 2个元素更糟糕。它是如何n * log(n)..不应该是O(n ^ 2)..谢谢
SELECT
siteid,
linenum,
worktype,
COUNT(CASE WHEN status = 'APPR' THEN 1 ELSE NULL END) AS [Approved],
COUNT(CASE WHEN status = 'review' THEN 1 ELSE NULL END) AS [Review],
COUNT(CASE WHEN status = 'wmatl' THEN 1 ELSE NULL END) AS [WaitMatl],
COUNT(CASE WHEN status = 'comp' THEN 1 ELSE NULL END) AS [Complete],
COUNT(CASE WHEN status = 'incomp' THEN 1 ELSE NULL END) AS [InComplete],
COUNT(CASE WHEN status = 'closed' THEN 1 ELSE NULL END) AS [Closed],
COUNT(CASE WHEN status not in ('appr','wmatl') THEN 1 ELSE NULL END) AS [All_Completed],
Count (*) as allrecords,
/* The below divide by does not work */
COUNT(CASE WHEN status not in ('appr','wmatl') THEN 1 ELSE null END) / Count (*) as Completion_Ratio
FROM workorder
WHERE (siteid in ('p202','p203','p201')) AND (worktype in ('mpm','ppm','tspm')) AND (istask ='0')
AND (historyflag ='0') AND (woclass = 'workorder') --AND (status not in ('comp','closed','review','incomp'))
AND (assetnum is not null) AND (maintby not in ('ms','ed'))
AND targcompdate < DATEADD(mm,DATEDIFF(mm,0,GETDATE())-0,0)
GROUP BY siteid,linenum,worktype
Order by siteid, linenum,worktype
参考 - documentation
答案 0 :(得分:2)
是的,在最坏的情况下,for循环触及n/2
个元素。这里的重要事实是输入的划分尽可能均匀。算法在n
元素上完成的工作量为O(T(n))
,其中重复T
由
T(1) = O(1)
T(n) = 2 T(n/2) + O(n).
Master Theorem的案例2适用,因此T(n) = O(n log n)
。这与合并排序非常相似。