Use recursive procedure to perform a bubble sort on a list of data
2014-03-24 17:58
330 查看
#! /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"
}
相关文章推荐
- Use loop-based procedure to perform a bubble sort on a list of data
- [转载]:How to pass a list of values or array to SQL Server stored procedure?
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- (原創) 如何解決warning:『PLL output port feeds output pin via non-dedicated routing -- jitter performance depends on switching rate of other design elements. Use PLL dedicated clock outputs to ensure jitter performance』? (SOC) (Quartus II) (DE2-70)
- "Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
- [Android] Use Jsoup to grab the web data and process the data with string.indexOf()
- dataframe插入数据报错SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a
- Loading Data when the User Scrolls to the End of a List in Windows Phone 7
- MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control.
- How to bind a GridView to a list of multiple types? NHibernate proxy causing problems with databinding [From stack overflow]
- How to delete a large number of data in SharePoint for List when refreshing data?
- A value is trying to be set on a copy of a slice from a DataFrame
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- Use bubble sort to sort the input intergers
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- Hot to use typelist of Loki??
- How to use HttpWebRequest to post data to another page which is on another server
- To find the list of all the available drives on...
- Core Data on iOS 5 Tutorial: How To Use NSFetchedResultsController