How to resolve the algorithm Sort three variables step by step in the Ksh programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort three variables step by step in the Ksh programming language

Table of Contents

Problem Statement

Sort   (the values of)   three variables   (X,   Y,   and   Z)   that contain any value   (numbers and/or literals). If that isn't possible in your language, then just sort numbers   (and note if they can be floating point, integer, or other). I.E.:   (for the three variables   x,   y,   and   z),   where: After sorting, the three variables would hold: For numeric value sorting, use: I.E.:   (for the three variables   x,   y,   and   z),   where: After sorting, the three variables would hold: The variables should contain some form of a number, but specify if the algorithm used can be for floating point or integers.   Note any limitations. The values may or may not be unique. The method used for sorting can be any algorithm;   the goal is to use the most idiomatic in the computer programming language used. More than one algorithm could be shown if one isn't clearly the better choice.

One algorithm could be:

Another algorithm   (only for numeric values):

Show the results of the sort here on this page using at least the values of those shown above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort three variables step by step in the Ksh programming language

Source code in the ksh programming language

#!/bin/ksh

# Sort three variables that may contain any value (numbers and/or literals)

#	# Variables:
#
xl='lions, tigers, and'
yl='bears, oh my!'
zl='(from the "Wizard of OZ")'

typeset -i xn=77444
typeset -F yn=-12.0
typeset -i zn=0

#	# Functions:
#

#	# Function _intoarray(x, y, z, arr) - put 3 variables into arr[]
#
function _intoarray {
	typeset _x ; nameref _x="$1"
	typeset _y ; nameref _y="$2"
	typeset _z ; nameref _z="$3"
	typeset _arr ; nameref _arr="$4"

	_arr=( "${_x}" "${_y}" "${_z}" )
}

#	# Function _arraysort(arr) - return sorted array (any type of elements)
#
function _arraysort {
	typeset _arr ; nameref _arr="$1"
	typeset _i _j ; integer _i _j

	_sorttype _arr
	case $? in
		0)	# Literal sort
			for (( _i=1; _i<${#_arr[*]}; _i++ )); do
				_val="${_arr[_i]}"
				(( _j = _i - 1 ))
				while (( _j>=0 )) && [[ "${_arr[_j]}" > "${_val}" ]]; do
					_arr[_j+1]="${_arr[_j]}"
					(( _j-- ))
				done
				_arr[_j+1]="${_val}"
			done
		;;

		1)	# Numeric sort
			for (( _i=1; _i<${#_arr[*]}; _i++ )); do
				_val=${_arr[_i]}
				(( _j = _i - 1 ))
				while (( _j>=0 && _arr[_j]>_val )); do
					_arr[_j+1]=${_arr[_j]}
					(( _j-- ))
				done
				_arr[_j+1]=${_val}
			done
		;;
	esac
}

#	# Function _sorttype(_arr) - return 0 = Literal sort; 1 = Numeric sort
#
function _sorttype {
	typeset _arr ; nameref _arr="$1"
	typeset _i ; integer _i

	for ((_i=0; _i<${#_arr[*]}; _i++)); do
		[[ ${_arr[_i]} != *(\-)+(\d)*(\.)*(\d) ]] && return 0
	done
	return 1
}

#	# Function _outofarray(x, y, z, arr) - Put array elements into x, y, z
#
function _outofarray {
	typeset _x ; nameref _x="$1"
	typeset _y ; nameref _y="$2"
	typeset _z ; nameref _z="$3"
	typeset _arr ; nameref _arr="$4"

	_x="${_arr[0]}"
	_y="${_arr[1]}"
	_z="${_arr[2]}"
}

 ######
# main #
 ######

unset x y z
printf "Numeric Variables:\n%s\n%s\n%s\n\n" "${xn}" "${yn}" "${zn}"
typeset -a arrayn
_intoarray xn yn zn arrayn
_arraysort arrayn
_outofarray x y z arrayn
printf "Sorted Variables:\n%s\n%s\n%s\n\n" "${x}" "${y}" "${z}"

unset x y z
printf "Literal Variables:\n%s\n%s\n%s\n\n" "${xl}" "${yl}" "${zl}"
typeset -a arrayl
_intoarray xl yl zl arrayl
_arraysort arrayl
_outofarray x y z arrayl
printf "Sorted Variables:\n%s\n%s\n%s\n\n" "${x}" "${y}" "${z}"


  

You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Conjugate transpose step by step in the PL/I programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the AWK programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Sidef programming language
You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the Perl programming language