How to resolve the algorithm Quickselect algorithm step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Quickselect algorithm step by step in the Action! programming language

Table of Contents

Problem Statement

Use the quickselect algorithm on the vector To show the first, second, third, ... up to the tenth largest member of the vector, in order, here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Quickselect algorithm step by step in the Action! programming language

Source code in the action! programming language

PROC Swap(BYTE ARRAY tab INT i,j)
  BYTE tmp

  tmp=tab(i) tab(i)=tab(j) tab(j)=tmp
RETURN

BYTE FUNC QuickSelect(BYTE ARRAY tab INT count,index)
  INT px,i,j,k
  BYTE pv

  DO
    px=count/2
    pv=tab(px)
    Swap(tab,px,count-1)
    
    i=0
    FOR j=0 TO count-2
    DO
      IF tab(j)
        Swap(tab,i,j)
        i==+1
      FI
    OD

    IF i=index THEN
      RETURN (pv)
    ELSEIF i>index THEN
      ;left part of tab from 0 to i-1
      count=i
    ELSE
      Swap(tab,i,count-1)
      ;right part of tab from i+1 to count-1
      tab==+(i+1)
      count==-(i+1)
      index==-(i+1)
    FI
  OD
RETURN (0)

PROC Main()
  DEFINE COUNT="10"
  BYTE ARRAY data=[9 8 7 6 5 0 1 2 3 4],tab(COUNT)
  BYTE i,res

  FOR i=0 TO COUNT-1
  DO
    MoveBlock(tab,data,COUNT)
    res=QuickSelect(tab,COUNT,i)
    PrintB(res) Put(32)
  OD
RETURN

  

You may also check:How to resolve the algorithm Dragon curve step by step in the Nim programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the D programming language
You may also check:How to resolve the algorithm 21 game step by step in the BASIC programming language
You may also check:How to resolve the algorithm Abstract type step by step in the Scala programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the PHP programming language