返回首页

排序问题 - 单程算法
整数作为文件输入给定的n排序名单,写了一通算法产生一个排序的输出文件,输出的是n个输入文件排序合并。

我想使用合并排序,但其复杂度为O(nlogn)。
你们有什么建议吗?{B​​R}我如何申请一通算法n个整数排序名单?
提出了一些策略,这将使我开始踢。

回答

评论会员:纳吉Vilmos 时间:2012/02/06
"作为文件输入给定n排序整数列表"

我输入排序的手段,所以这是一个优化输出的情况下。某处沿着这条线将做肮脏的:

for each file

  read the first number into a sorted list

end loop

while the list is non empty and the files have more items

  write the first item in the sorted list to the output

  if the item's file has more

    read next item from file into sorted list

  end if

end loop

你只需要记录每个项目来自哪个文件,有一个排序列表,可以添加到项目。
我会离开的实施行使作为一个OP
评论会员:游客 时间:2012/02/06
迈赫迪吴拉姆:在这篇文章在这里imgsrc=一看
Stefan_Lang
评论会员:游客 时间:2012/02/06
1。初始化这样的算法:{C}2。主要算法应该是这样的:codeprelang="c++"spanclass="code-keyword"while/span(filelistnotempty){spanclass="code-keyword"do/span{storethenumberfromthefirstfilegetthenextnumberfromthatfilespanclass="code-keyword"if/span(endoffile){removefilefromlist}}spanclass="code-keyword"while/span(mostrecentnumber<=lastnumberreadfromthesecondfile)or(filelistnotempty) spanclass="code-keyword"if/span(filelistnotempty){sortfilesbylastreadnumber(youonlyneedtorepositionthefirstfile)}}/pre/codeP.S.:删除意外副本这一解决方案: