Selection Sort in Tcl

Published on 09 October 2025 (Updated: 09 October 2025)

Welcome to the Selection Sort in Tcl page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

proc usage {} {
	puts stderr {Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"}
	exit 1
}

proc parseList {s} {
	set tokens [split [string trim $s] ","]
	if {[llength $tokens] < 2} { usage }

	set result {}

	set result {}
	foreach token $tokens {
		set t [string trim $token]
		if {$t eq "" || [catch {expr {int($t)}} val]} usage
		lappend result $val
	}
	return $result
}

proc isSorted {lst} {
	set prev [lindex $lst 0]
	foreach x [lrange $lst 1 end] {
		if {$x < $prev} {return 0}
		set prev $x
	}
	return 1
}

proc selectionSort {lstVar} {
    upvar 1 $lstVar lst
    set n [llength $lst]

    for {set i 0} {$i < $n - 1} {incr i} {
        set minIdx $i
        for {set j [expr {$i + 1}]} {$j < $n} {incr j} {
            if {[lindex $lst $j] < [lindex $lst $minIdx]} {
                set minIdx $j
            }
        }
        if {$minIdx != $i} {
            set temp [lindex $lst $i]
            lset lst $i [lindex $lst $minIdx]
            lset lst $minIdx $temp
        }
    }
}

proc formatList {lst} { return [join $lst ", "] }

if {$argc != 1} { usage }

set numbers [parseList [lindex $argv 0]]

if {![isSorted $numbers]} {
    selectionSort numbers
}

puts [formatList $numbers]

Selection Sort in Tcl was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.