How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the uBasic/4tH programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the uBasic/4tH programming language
Table of Contents
Problem Statement
Sort an array of integers (of any convenient size) into ascending order using Circlesort. In short, compare the first element to the last element, then the second element to the second last element, etc. Then split the array in two and recurse until there is only one single element in the array, like this: Repeat this procedure until quiescence (i.e. until there are no swaps). Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations (like doing 0.5 log2(n) iterations and then continue with an Insertion sort) are optional. Pseudo code:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the uBasic/4tH programming language
Source code in the ubasic/4th programming language
PRINT "Circle sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
PROC _Circlesort (n)
PROC _ShowArray (n)
PRINT
END
_InnerCircle PARAM (2)
LOCAL (3)
c@ = a@
d@ = b@
e@ = 0
IF c@ = d@ THEN RETURN (0)
DO WHILE c@ < d@
IF @(c@) > @(d@) THEN PROC _Swap (c@, d@) : e@ = e@ + 1
c@ = c@ + 1
d@ = d@ - 1
LOOP
e@ = e@ + FUNC (_InnerCircle (a@, d@))
e@ = e@ + FUNC (_InnerCircle (c@, b@))
RETURN (e@)
_Circlesort PARAM(1) ' Circle sort
DO WHILE FUNC (_InnerCircle (0, a@-1))
LOOP
RETURN
_Swap PARAM(2) ' Swap two array elements
PUSH @(a@)
@(a@) = @(b@)
@(b@) = POP()
RETURN
_InitArray ' Init example array
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
FOR i = 0 TO 9
@(i) = POP()
NEXT
RETURN (i)
_ShowArray PARAM (1) ' Show array subroutine
FOR i = 0 TO a@-1
PRINT @(i),
NEXT
PRINT
RETURN
You may also check:How to resolve the algorithm Animate a pendulum step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Frink programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Sed programming language
You may also check:How to resolve the algorithm OLE automation step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Date format step by step in the Lua programming language