How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

Heapsort is an in-place sorting algorithm with worst case and average complexity of   O(n logn). The basic idea is to turn the array into a binary heap structure, which has the property that it allows efficient retrieval and removal of the maximal element. We repeatedly "remove" the maximal element from the heap, thus building the sorted list from back to front. A heap sort requires random access, so can only be used on an array-like data structure. Pseudocode:

Write a function to sort a collection of integers using heapsort.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main()                     #: demonstrate various ways to sort a list and string 
   demosort(heapsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end

procedure heapsort(X,op)                            #: return sorted list ascending(or descending)
local head,tail

   op := sortop(op,X)                               # select how and what we sort

   every head := (tail := *X) / 2  to 1 by -1 do    # work back from from last parent node
      X := siftdown(X,op,head,tail)                 # sift down from head to make the heap 

   every tail := *X to 2 by -1 do {                 # work between the beginning and the tail to final positions
      X[1] :=: X[tail]
      X := siftdown(X,op,1,tail-1)                  # re-sift next (previous) branch after shortening the heap
      }

   return X
end

procedure siftdown(X,op,root,tail)                  #: the value @root is moved "down" the path of max(min) value to its level
local child

   while (child :=  root * 2) <= tail do {          # move down the branch from root to tail

      if op(X[child],X[tail >= child + 1]) then     # choose the larger(smaller) 
         child +:= 1                                # ... child 

      if op(X[root],X[child]) then  {               # root out of order? 
         X[child] :=: X[root]                       
         root := child                              # follow max(min) branch
         }
      else 
         return X
      }
   return X
end


  

You may also check:How to resolve the algorithm Colour bars/Display step by step in the Raku programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the C programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the XBS programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the C programming language