#! /usr/bin/expect --
# Use recursive procedure to perform a bubble sort on a list of data
proc recBubblesort {sData sLen} {
set debug 0
# Print the primary data
if {$debug == 1} {
send_user "Primary Data is $sData\n"
}
# Do the bubble sort
if {$sLen > 1} {
for {set i 0} {$i < [expr $sLen - 1]} {incr i} {
set aTmp [lindex $sData $i]
set bTmp [lindex $sData [expr $i + 1]]
if {[string compare -nocase $aTmp $bTmp] > 0} {
set temp [lindex $sData $i]
set sData [lreplace $sData $i $i [lindex $sData [expr $i + 1]]]
set sData [lreplace $sData [expr $i + 1] [expr $i + 1] $temp]
}
}
recBubblesort $sData [expr $sLen - 1]
} else {
# Print the final data
if {$debug == 1} {
send_user "Final Data is $sData\n"
}
return $sData
}
}
set str [lindex $argv 0]
if {$str == ""} {
puts "Please input string!"
exit
} else {
set sLen [llength $str]
set nData [recBubblesort $str $sLen]
send_user "The primary data is <$str>\n"
send_user "The data sorted is <$nData>\n"
}
# Use recursive procedure to perform a bubble sort on a list of data
proc recBubblesort {sData sLen} {
set debug 0
# Print the primary data
if {$debug == 1} {
send_user "Primary Data is $sData\n"
}
# Do the bubble sort
if {$sLen > 1} {
for {set i 0} {$i < [expr $sLen - 1]} {incr i} {
set aTmp [lindex $sData $i]
set bTmp [lindex $sData [expr $i + 1]]
if {[string compare -nocase $aTmp $bTmp] > 0} {
set temp [lindex $sData $i]
set sData [lreplace $sData $i $i [lindex $sData [expr $i + 1]]]
set sData [lreplace $sData [expr $i + 1] [expr $i + 1] $temp]
}
}
recBubblesort $sData [expr $sLen - 1]
} else {
# Print the final data
if {$debug == 1} {
send_user "Final Data is $sData\n"
}
return $sData
}
}
set str [lindex $argv 0]
if {$str == ""} {
puts "Please input string!"
exit
} else {
set sLen [llength $str]
set nData [recBubblesort $str $sLen]
send_user "The primary data is <$str>\n"
send_user "The data sorted is <$nData>\n"
}