Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

quicksort_array.fz


quicksort_array =>

  print(T type, arr array T) =>
    for x in arr do
      yak "$x, "
    say

  quick_sort(T type : property.orderable, arr mutate.new (array T), low i32, high i32) =>
    partition(T type : property.orderable, low i32, high i32) =>
      swap(x i32, y i32) =>
        tmp := arr.get[x]
        arr <- arr.get.put x arr.get[y]
        arr <- arr.get.put y tmp

      pivot := arr.get[high]
      i := mut (low - 1)
      for j in low..(high-1) do
        if (arr.get[j] < pivot)
          i <- i.get + 1
          swap i.get j
      swap (i.get + 1) high
      i.get + 1

    if (low < high)
      pi := partition T low high
      arr <- quick_sort arr low (pi - 1)
      arr <- quick_sort arr (pi + 1) high

    arr.get

  a := array 10 (x -> random.next_f64)

  say "unsorted"
  print a

  b := quick_sort (mut a) 0 (a.length - 1)

  say "sorted"
  print b