这个合并排序有什么问题?
我正在尝试在 Coldfusion 中实现合并排序,但它输出了错误的结果,代码:
<cffunction name="mergeSort" hint="Sorts arrays of structs">
<cfargument name="arr" type="Array" required="yes">
<cfif Arraylen(arr) LTE 1>
<cfreturn arr />
</cfif>
<cfset left_ = ArrayNew(1)>
<cfset right_ = ArrayNew(1)>
<cfset mid_ = Int(Arraylen(arr) / 2)>
<cfloop index="i" from="1" to="#mid_#">
<cfset arrayAppend(left_, arr[i])>
</cfloop>
<cfloop index="j" from="#mid_+1#" to="#ArrayLen(arr)#">
<cfset arrayAppend(right_, arr[j])>
</cfloop>
<cfreturn merge( mergeSort(left_), mergeSort(right_) )>
</cffunction>
<cffunction name="merge" hint="Merges two arrays">
<cfargument name="left_" required="yes" type="Array">
<cfargument name="right_" required="yes" type="Array">
<cfset result = ArrayNew(1)>
<cfloop condition="ArrayLen(left_) GT 0 AND ArrayLen(right_) GT 0">
<cfif left_[1].attr3 LTE right_[1].attr3>
<cfset arrayAppend(result, left_[1])>
<cfset arrayDeleteAt(left_, 1)>
<cfelse>
<cfset arrayAppend(result, right_[1])>
<cfset arrayDeleteAt(right_, 1)>
</cfif>
</cfloop>
<cfif ArrayLen(left_) GT 0>
<cfloop array="#left_#" index="v">
<cfset ArrayAppend(result, v)>
</cfloop>
</cfif>
<cfif ArrayLen(right_) GT 0>
<cfloop array="#right_#" index="v">
<cfset ArrayAppend(result, v)>
</cfloop>
</cfif>
<cfreturn result />
</cffunction>
它正在对名为“attr3”的结构键上的结构数组进行排序。所发生的情况是,它似乎正确地分割了列表,但随后继续将相同的列表附加到结果集中。因此,例如,如果我将 left_.attr3 作为“标题”,将 right_.attr3 作为“另一个”,则结果最终将是“标题”、“另一个”、“另一个”、“另一个”.. 等等。
I'm trying to implement mergesort in Coldfusion, but it is spitting out incorrect results, code:
<cffunction name="mergeSort" hint="Sorts arrays of structs">
<cfargument name="arr" type="Array" required="yes">
<cfif Arraylen(arr) LTE 1>
<cfreturn arr />
</cfif>
<cfset left_ = ArrayNew(1)>
<cfset right_ = ArrayNew(1)>
<cfset mid_ = Int(Arraylen(arr) / 2)>
<cfloop index="i" from="1" to="#mid_#">
<cfset arrayAppend(left_, arr[i])>
</cfloop>
<cfloop index="j" from="#mid_+1#" to="#ArrayLen(arr)#">
<cfset arrayAppend(right_, arr[j])>
</cfloop>
<cfreturn merge( mergeSort(left_), mergeSort(right_) )>
</cffunction>
<cffunction name="merge" hint="Merges two arrays">
<cfargument name="left_" required="yes" type="Array">
<cfargument name="right_" required="yes" type="Array">
<cfset result = ArrayNew(1)>
<cfloop condition="ArrayLen(left_) GT 0 AND ArrayLen(right_) GT 0">
<cfif left_[1].attr3 LTE right_[1].attr3>
<cfset arrayAppend(result, left_[1])>
<cfset arrayDeleteAt(left_, 1)>
<cfelse>
<cfset arrayAppend(result, right_[1])>
<cfset arrayDeleteAt(right_, 1)>
</cfif>
</cfloop>
<cfif ArrayLen(left_) GT 0>
<cfloop array="#left_#" index="v">
<cfset ArrayAppend(result, v)>
</cfloop>
</cfif>
<cfif ArrayLen(right_) GT 0>
<cfloop array="#right_#" index="v">
<cfset ArrayAppend(result, v)>
</cfloop>
</cfif>
<cfreturn result />
</cffunction>
It's sorting an array of structs, on the struct key called "attr3". What happens is that it splits the lists correctly, it seems, but then continues to attach the same list to the resultset. So, for example, if I have left_.attr3 as "Title" and right_.attr3 as "Another", the result ends up being "Title", "Another", "Another", "Another" .. etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过如何在 ColdFusion 中对结构数组进行排序< /a>?
顺便说一句,请 var 范围所有变量!
顺便说一句,您可以使用 Java 在 ColdFusion 中连接两个数组
顺便说一句,您可以使用
mid_ = Arraylen(arr) \ 2
对于整数 divHave you looked at How to sort an array of structs in ColdFusion ?
btw, pls var scope all your variables!
btw, you can join arrays with Java Join Two Arrays in ColdFusion
btw, you can use
mid_ = Arraylen(arr) \ 2
for integer div