Posted to tcl by saedelaere at Tue Apr 03 06:22:47 GMT 2012view raw

  1. proc quicksort {low high} {
  2. if {$low < $high} {
  3. set pivLoc [partition $low $high]
  4. quicksort $low [expr $pivLoc - 1]
  5. quicksort [expr $pivLoc + 1] $high
  6. }
  7. }
  8.  
  9. proc partition {low high} {
  10. set i [expr $low + 1]
  11.  
  12. set pivot $::sortme($low)
  13.  
  14. set ::comparisons [expr ($::comparisons + (($high-$low)-1))]
  15.  
  16. for {set j $i} {$j < $high} {incr j} {
  17. if {$::sortme($j) < $pivot} {
  18. set temp $::sortme($j)
  19. set ::sortme($j) $::sortme($i)
  20. set ::sortme($i) $temp
  21. incr i
  22. }
  23. }
  24. #put pivot in correct position
  25. set temp $::sortme($low)
  26. set ::sortme($low) $::sortme([expr $i-1])
  27. set ::sortme([expr $i-1]) $temp
  28. return $i-1
  29. }
  30.  
  31. set open_txt [open "/home/saedelaere/Downloads/Samples/IntegerArray.txt" r]
  32. set i 0
  33. while {[gets $open_txt line]!=-1} {
  34. if {[string match #* $line] || [string trim $line] == {} } continue
  35. array set ::sortme [list $i $line]
  36. incr i
  37. }
  38. close $open_txt
  39.  
  40. set comparisons 0
  41.  
  42. quicksort 0 9999
  43.  
  44. for {set k 0} {$k < 10000} {incr k} {
  45. puts $sortme($k)
  46. }