#!/usr/bin/ksh93

########################################################################
#                                                                      #
#               This software is part of the ast package               #
#                 Copyright (c) 2006-2012 Roland Mainz                 #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#          http://www.eclipse.org/org/documents/epl-v10.html           #
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
#                                                                      #
#                                                                      #
#                 Roland Mainz <roland.mainz@nrubsig.org>              #
#                                                                      #
########################################################################

#
# Copyright (c) 2006, 2012, Roland Mainz. All rights reserved.
#

# simple bubblesort
# don't blame me about the quality - it was written at 3:00am and there is no
# gurantee that this thing even works

function compare_string
{
#set -x
    nameref str1=$1
    nameref str2=$2
    integer str1_len=${#str1}
    integer str2_len=${#str2}
    integer i1=0
    integer i2=0
        
    [[ "${str1}" = "${str2}" ]] && { print "0" ; return 0 ; }

    # this is horrible inefficient
    while [[ "${str1:i1:1}" == "${str2:i2:1}" ]] &&
          (( i1 < str1_len && i2 < str2_len )) ; do
        (( i1++ , i2++ ))
    done

    (( '${str1:i1:1}' > '${str2:i2:1}' )) && print -- "-1" || print "1"
    return 0
}

function swap_string
{
#set -x
    nameref s1=$1
    nameref s2=$2
    typeset tmp

    tmp="${s1}"
    s1="${s2}"
    s2="${tmp}"
}

function bubblesort
{
#set -x
    nameref ar=$1  # data array
    typeset cmpfunc=$2 # compare function
    typeset swapfunc=$3

    typeset swapped="true"
    integer i
    integer num_ar=$((${#ar[*]}-1))
    typeset e1 e2
    
    while ${swapped} ; do
	swapped="false"
	for (( i=0 ; i < num_ar ; i++ )) ; do
	    e1="${!ar}[${i}]"
	    e2="${!ar}[$((i+1))]"
	    if (( $(${cmpfunc} "${e1}" "${e2}") == -1 )) ; then
	       ${swapfunc} "${e1}" "${e2}"

	       swapped="true"
	    fi
	done
    done
}

#compare_string "aa"  "aa"
#compare_string "ab"  "aa"
#compare_string "aa"  "ab"
#compare_string "cbd" "cad"
#compare_string "123" "312"
#compare_string "123" "1234"

typeset -a data

if false ; then
    largestring="largestring"
    for ((i=0 ; i < 7 ; i++)) ; do
	largestring+="${largestring}"
    done

    for (( i=8 ; i > 0 ; i-- )) ; do
	data+=( "${largestring}$((i**10))" )
    done
else
    data+=( "aa" )
    data+=( "foo4" )
    data+=( "foo2" )
    data+=( "a" )
    data+=( "z" )
    data+=( "foo3" )
    data+=( "foo3" )
    data+=( "foo1" )
    data+=( "foo9" )
    data+=( "zb" )
    data+=( "za" )
fi

print "# Sorting $([[ -n "${largestring}" ]] && print ${#largestring})"
bubblesort data compare_string swap_string

# print elemets, one per line
(IFS=$'\n' ; print -- "${data[*]}")

#EOF.
