Seed7 
 FAQ 
 Manual 
 Screenshots 
 Examples 
 Algorithms 
 Download 
 Links 

 Algorithms 
 Sorting 
 Searching 
 Date & Time 
 String 
 Mathematics 
 File 
 Puzzles 
 Others 
 Algorithms 
Sorting
 previous   up   next 

Seed7 contains a sort function to sort arrays with the quicksort algorithm. The argorithms here demonstrate various sorting methods.

Bubble sort

const proc: bubbleSort (inout array elemType: arr) is func
  local
    var integer: i is 0;
    var integer: j is 0;
    var elemType: help is elemType.value;
  begin
    for i range 1 to length(arr) do
      for j range succ(i) to length(arr) do
        if arr[i] > arr[j] then
          help := arr[i];
          arr[i] := arr[j];
          arr[j] := help;
        end if;
      end for;
    end for;
  end func;

Selection sort

const proc: selectionSort (inout array elemType: arr) is func
  local
    var integer: i is 0;
    var integer: j is 0;
    var integer: min is 0;
    var elemType: help is elemType.value;
  begin
    for i range 1 to length(arr) do
      min := i;
      for j range succ(i) to length(arr) do
        if arr[j] < arr[min] then
          min := j;
        end if;
      end for;
      help := arr[min];
      arr[min] := arr[i];
      arr[i] := help;
    end for;
  end func;

Shaker sort

const proc: shakerSort (inout array elemType: arr) is func
  local
    var integer: i is 1;
    var integer: j is 0;
    var integer: k is 0;
    var integer: min is 0;
    var integer: max is 0;
    var elemType: help is elemType.value;
  begin
    k := length(arr);
    while i < k  do
      min := i;
      max := i;
      for j range succ(i) to k do
        if arr[j] < arr[min] then
          min := j;
        end if;
        if arr[j] > arr[max] then
          max := j;
        end if;
      end for;
      help := arr[min];
      arr[min] := arr[i];
      arr[i] := help;
      if max = i then
        help := arr[min];
        arr[min] := arr[k];
        arr[k] := help;
      else
        help := arr[max];
        arr[max] := arr[k];
        arr[k] := help;
      end if;
      incr(i);
      decr(k);
    end while;
  end func;

Insertion sort

const proc: insertionSort (inout array elemType: arr) is func
  local
    var integer: i is 0;
    var integer: j is 0;
    var elemType: help is elemType.value;
  begin
    for i range 2 to length(arr) do
      j := i;
      help := arr[i];
      while j > 1 and arr[pred(j)] > help do
        arr[j] := arr[pred(j)];
        decr(j);
      end while;
      arr[j] := help;
    end for;
  end func;

Merge sort

const proc: mergeSort (inout array elemType: arr, in var integer: lo, in integer: hi) is func
  local
    var integer: mid is 0;
    var elemType: help is elemType.value;
    var integer: k is 0;
  begin
    if lo < hi then
      mid := (lo + hi) div 2;
      mergeSort(arr, lo, mid);
      mergeSort(arr, succ(mid), hi);
      incr(mid);
      while lo < mid and mid <= hi do
        if arr[lo] < arr[mid] then
          incr(lo);
        else
          help := arr[mid];
          for k range mid downto succ(lo) do
            arr[k] := arr[pred(k)];
          end for;
          arr[lo] := help;
          incr(lo);
          incr(mid);
        end if;
      end while;
    end if;
  end func;

const proc: mergeSort (inout array elemType: arr) is func
  begin
    mergeSort(arr, 1, length(arr));
  end func;

Merge sort with additional storage

const proc: mergeSort2 (inout array elemType: arr, in integer: lo, in integer: hi, inout array elemType: scratch) is func
  local
    var integer: mid is 0;
    var integer: k is 0;
    var integer: t_lo is 0;
    var integer: t_hi is 0;
  begin
    if lo < hi then
      mid := (lo + hi) div 2;
      mergeSort2(arr, lo, mid, scratch);
      mergeSort2(arr, succ(mid), hi, scratch);
      t_lo := lo;
      t_hi := succ(mid);
      for k range lo to hi do
        if t_lo <= mid and (t_hi > hi or arr[t_lo] < arr[t_hi]) then
          scratch[k] := arr[t_lo];
          incr(t_lo);
        else
          scratch[k] := arr[t_hi];
          incr(t_hi);
        end if;
      end for;
      for k range lo to hi do
        arr[k] := scratch[k];
      end for;
    end if;
  end func;

const proc: mergeSort2 (inout array elemType: arr) is func
  local
    var array elemType: scratch is 0 times elemType.value;
  begin
    scratch := length(arr) times elemType.value;
    mergeSort2(arr, 1, length(arr), scratch);
  end func;

Heap sort

const proc: downheap (inout array elemType: arr, in var integer: k, in integer: n) is func
  local
    var elemType: help is elemType.value;
    var integer: j is 0;
  begin
    if k <= n div 2 then
      help := arr[k];
      repeat
        j := 2 * k;
        if j < n and arr[j] < arr[succ(j)] then
          incr(j);
        end if;
        if help < arr[j] then
          arr[k] := arr[j];
          k := j;
        end if;
      until help >= arr[j] or k > n div 2;
      arr[k] := help;
    end if;
  end func;

const proc: heapSort (inout array elemType: arr) is func
  local
    var integer: n is 0;
    var integer: k is 0;
    var elemType: help is elemType.value;
  begin
    n := length(arr);
    for k range n div 2 downto 1 do
      downheap(arr, k, n);
    end for;
    repeat
      help := arr[1];
      arr[1] := arr[n];
      arr[n] := help;
      decr(n);
      downheap(arr, 1, n);
    until n <= 1;
  end func;

Shell sort

const proc: shellSort (inout array elemType: arr) is func
  local
    var integer: i is 0;
    var integer: j is 0;
    var integer: increment is 0;
    var elemType: help is elemType.value;
  begin
    increment := length(arr) div 2;
    while increment > 0 do
      for i range 1 to length(arr) do
        j := i;
        help := arr[i];
        while j > increment and arr[j - increment] > help do
          arr[j] := arr[j - increment];
          j -:= increment;
        end while;
        arr[j] := help;
      end for;
      increment := increment div 2;
    end while;
  end func;

Quicksort

const proc: quickSort (inout array elemType: arr, in integer: left, in integer: right) is func
  local
    var elemType: compare_elem is elemType.value;
    var integer: less_idx is 0;
    var integer: greater_idx is 0;
    var elemType: help is elemType.value;
  begin
    if right > left then
      compare_elem := arr[right];
      less_idx := pred(left);
      greater_idx := right;
      repeat
        repeat
          incr(less_idx);
        until arr[less_idx] >= compare_elem;
        repeat
          decr(greater_idx);
        until arr[greater_idx] <= compare_elem or greater_idx = left;
        if less_idx < greater_idx then
          help := arr[less_idx];
          arr[less_idx] := arr[greater_idx];
          arr[greater_idx] := help;
        end if;
      until less_idx >= greater_idx;
      arr[right] := arr[less_idx];
      arr[less_idx] := compare_elem;
      quickSort(arr, left, pred(less_idx));
      quickSort(arr, succ(less_idx), right);
    end if;
  end func;

const proc: quickSort (inout array elemType: arr) is func
  begin
    quickSort(arr, 1, length(arr));
  end func;


 previous   up   next