Posted to tcl by saedelaere at Tue Apr 03 06:22:47 GMT 2012view raw
- proc quicksort {low high} {
- if {$low < $high} {
- set pivLoc [partition $low $high]
- quicksort $low [expr $pivLoc - 1]
- quicksort [expr $pivLoc + 1] $high
- }
- }
-
- proc partition {low high} {
- set i [expr $low + 1]
-
- set pivot $::sortme($low)
-
- set ::comparisons [expr ($::comparisons + (($high-$low)-1))]
-
- for {set j $i} {$j < $high} {incr j} {
- if {$::sortme($j) < $pivot} {
- set temp $::sortme($j)
- set ::sortme($j) $::sortme($i)
- set ::sortme($i) $temp
- incr i
- }
- }
- #put pivot in correct position
- set temp $::sortme($low)
- set ::sortme($low) $::sortme([expr $i-1])
- set ::sortme([expr $i-1]) $temp
- return $i-1
- }
-
- set open_txt [open "/home/saedelaere/Downloads/Samples/IntegerArray.txt" r]
- set i 0
- while {[gets $open_txt line]!=-1} {
- if {[string match #* $line] || [string trim $line] == {} } continue
- array set ::sortme [list $i $line]
- incr i
- }
- close $open_txt
-
- set comparisons 0
-
- quicksort 0 9999
-
- for {set k 0} {$k < 10000} {incr k} {
- puts $sortme($k)
- }