【问题标题】:Quicksort algorithm in OZ LanguageOZ 语言中的快速排序算法
【发布时间】:2016-09-09 07:54:45
【问题描述】:

以下 OZ 程序使用内置的分区函数来执行快速排序算法。需要使用原有的分区方案而不是内置的分区函数来修改程序。 我搜索并找到了2个原始方案:Lomuto分区方案和Hoare分区方案,但我无法修改程序(我是OZ语言的新手)!所需的两种分区方案在 Wikipedia 中的以下链接中进行了说明:https://en.wikipedia.org/wiki/Quicksort

我正在尝试修改的 OZ 程序:

声明 有趣的{QuickSort Xs} 情况 Xs 为 nil 然后 nil [] 枢轴|Xr 然后 有趣 {IsSmaller X} X

【问题讨论】:

  • declare fun {QuickSort Xs} case Xs of nil then nil [] Pivot|Xr then fun {IsSmaller X} X

标签: quicksort oz


【解决方案1】:
proc {Partition Xs Pivot Left Right}
   case Xs
   of X|Xr then
      if X < Pivot
      then Ln in
         Left = X | Ln
         {Partition Xr Pivot Ln Right}
      else Rn in
         Right = X | Rn
         {Partition Xr Pivot Left Rn}
      end
   [] nil then Left=nil Right=nil
   end
end

fun lazy {LQuickSort Xs}
   case Xs of X|Xr then Left Right SortedLeft SortedRight in
      {Partition Xr X Left Right}
      {LAppend {LQuickSort Left} X|{LQuickSort Right}}
   [] nil then nil
   end
end

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    相关资源
    最近更新 更多