How to resolve the algorithm Largest int from concatenated ints step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest int from concatenated ints step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer. Use the following two sets of integers as tests   and   show your program output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest int from concatenated ints step by step in the FreeBASIC programming language

Source code in the freebasic programming language

#define MAXDIGITS 8

function catint( a as string, b as string ) as uinteger
    return valint(a+b)
end function

function grt( a as string, b as string ) as boolean
    return catint(a, b)>catint(b, a)
end function

sub shellsort( a() as string )
    'quick and dirty shellsort, not the focus of this exercise
    dim as uinteger gap = ubound(a), i, j, n=ubound(a)
    dim as string temp
    do 
        gap = int(gap / 2.2)
        for i=gap to n
            temp = a(i)
            j=i
            while j>=gap andalso grt( a(j-gap), temp )
                a(j) = a(j - gap)
                j -= gap
            wend 
            a(j) = temp
        next i
    loop until gap = 1
end sub

sub sort_and_print( a() as string )
    dim as uinteger i
    dim as string outstring = ""
    shellsort(a())
    for i=0 to ubound(a)
        outstring = a(i)+outstring
    next i
    print outstring
end sub

dim as string set1(8) = {"1", "34", "3", "98", "9", "76", "45", "4"}
dim as string set2(4) = {"54", "546", "548", "60"}

sort_and_print(set1())
sort_and_print(set2())

  

You may also check:How to resolve the algorithm Program termination step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the C# programming language
You may also check:How to resolve the algorithm Generate Chess960 starting position step by step in the 11l programming language
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Raku programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Quackery programming language