How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
' converted pseudo code into FreeBASIC code

' shared variables need to be declared before first use
Dim Shared As Long cs(-7 To 7)

Function circlesort(lo As Long, hi As Long, swaps As ULong) As ULong

    ' array is declared shared 
    ' sort from lower bound to the highter bound
    ' array's can have subscript range from -2147483648 to +2147483647

    If lo = hi Then Return swaps

    Dim As Long high = hi
    Dim As Long low = lo
    Dim As Long mid_ = (hi - lo) \ 2

    While lo < hi
        If cs(lo) > cs(hi) Then
            Swap cs(lo), cs(hi)
            swaps += 1
        End If
        lo += 1
        hi -= 1
    Wend
    If lo = hi Then
        If cs(lo) > cs(hi +1) Then
            Swap cs(lo), cs(hi +1)
            swaps += 1
        End If
    End If
    swaps = circlesort(low          , low + mid_, swaps)
    swaps = circlesort(low + mid_ +1,       high, swaps)

    Return swaps

End Function

' ------=< MAIN >=------

Dim As Long i, a = LBound(cs), b = UBound(cs)

Randomize Timer
For i = a To b : cs(i) = i  : Next
For i = a To b ' little shuffle
    Swap cs(i), cs(Int(Rnd * (b - a +1)) + a)
Next

Print "unsorted ";
For i = a To b : Print Using "####"; cs(i); : Next : Print

' sort the array, loop until sorted
While circlesort(a, b, 0) : Wend

Print "  sorted ";
For i = a To b : Print Using "####"; cs(i); : Next : Print

' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Metronome step by step in the Racket programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Arturo programming language
You may also check:How to resolve the algorithm OpenGL step by step in the R programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Apex programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Emacs Lisp programming language