求一个比较高效的数组查找算法

llpoo 2009-08-06 04:01:16
有数组:a(9);
a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6
希望经过一定的计算得出
============================
值,出现的次数
1, 3
2,2
3,2
4,1
5,1
6,1
...全文
138 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzx99 2009-08-06
a是一定范围内的自然数:

<%
DIM a(9) , b(9,1)
a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6

For i=0 To 9
b(i,0)=i
for j=0 to 9
if b(i,0)=a(j) then b(i,1)= b(i,1)+1
next
next

For i=0 To 9
if b(i,0)>0 and b(i,1)>0 then Response.Write b(i,0) & "," & b(i,1) & "<BR>"
next

%>
回复
lzp4881 2009-08-06
<%
dim a : a = Array(1,1,2,3,1,2,3,4,5,6,1,1,4,5,6,2,22,22,222,2,3,4,5,6,6,5,1,2,3,4,5,6,1,2,3,4,5,6,3,2,1,22,222,22,22,222,3,4,5,6,2,3,1,2,3,4,5,6,6,5,4,3,2,1)
str=","&join(a,",")&","
while len(str)<>1
temp=left(str,instr(2,str,","))
l1=len(str)
while instr(str,temp)<>0
str=replace(str,temp,",")
wend
l2=len(str)
response.write replace(temp,",","") &":"& (l1-l2)/(len(temp)-1)&"<br>"
wend
%>
回复
lzp4881 2009-08-06
<%
dim a : a = Array(1,1,2,3,1,2,3,4,5,6,1,1,4,5,6,2,22,22,222,2,3,4,5,6,6,5,1,2,3,4,5,6,1,2,3,4,5,6,3,2,1,22,222,22,22,222,3,4,5,6,2,3,1,2,3,4,5,6,6,5,4,3,2,1)
str=","&join(b,",")&","
while len(str)<>1
temp=left(str,instr(2,str,","))
l1=len(str)
while instr(str,temp)<>0
str=replace(str,temp,",")
wend
l2=len(str)
response.write replace(temp,",","") &":"& (l1-l2)/(len(temp)-1)&"<br>"
wend
%>
回复
llpoo 2009-08-06
我意思就是通用类型的循环处理
回复
toury 2009-08-06
[Quote=引用 5 楼 llpoo 的回复:]
toury,谢谢你的关注。我要的只是得出数组各个值出现的次数。希望算法不要跟语言相关,即ASP里写了这个算法,那么用到php或JS或java或C只要改变几个关键的控制符就好了,不要特定的内置函数
[/Quote]
各语言基本通吃的代码?惭愧,本人才疏学浅哈:))玩笑~~
比较通用的就是做循环了,也不麻烦,但效率就谈不上了,呵呵
另外:
For Each x In c
response.write x & " = " & c.Item(x)'这里输出的就是“数组各个值出现的次数。”
Next
回复
llpoo 2009-08-06
toury,谢谢你的关注。我要的只是得出数组各个值出现的次数。希望算法不要跟语言相关,即ASP里写了这个算法,那么用到php或JS或java或C只要改变几个关键的控制符就好了,不要特定的内置函数
回复
toury 2009-08-06

<%
a = Array(1, 1, 2, 3, 1, 2, 3, 4, 5, 6)
Set c = CreateObject("Scripting.Dictionary")
a1 = 0: a2 = 0

For i = 0 To UBound(a)
If c.Exists(a(i)) Then
d = CInt(c.Item(a(i)))
d = d + 1
c.Item(a(i)) = CStr(d)
Else
c.Add a(i), 1
d = 1
End If

If d > a2 Then
a1 = a(i): a2 = d
End If
Next


For Each x In c
response.write x & " = " & c.Item(x)
Next
response.write "最多的是:"; a1 & ",出现了" & a2 & "次"

%>
回复
llpoo 2009-08-06
可以不用Recordset实现吗?
回复
shenzhenNBA 2009-08-06
老大,回答比较快啊,有创新,呵呵...支持
回复
hookee 2009-08-06

DIM a(9)
a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6

Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.Fields.Append "val", 3
rs.Fields.Append "cnt", 3
rs.Open
rs.AddNew
rs("val") = a(0)
rs("cnt") = 1
rs.Update
For i=1 To UBound(a)
rs.MoveFirst
rs.Find "val=" & a(i)
If Not rs.EOF Then
rs("cnt") = rs("cnt") + 1
Else
rs.AddNew
rs("val") = a(i)
rs("cnt") = 1
End If
rs.Update
Next

rs.Sort = "cnt desc"
rs.MoveFirst
i=1
s = ""
Do While Not rs.EOF
s = s & i & ". " & rs("val") & " " & rs("cnt") & VbCrLf & "<br>"
rs.MoveNext
i = i + 1
Loop
Response.Write s
回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告