using any sorting technique
array Variable arr contained : arr { 1 5 2 9 3 8 10 6}
Without using lsort
finally o/p to be in same array variable : arr { 1 2 3 5 6 8 9 10}
using any sorting technique
array Variable arr contained : arr { 1 5 2 9 3 8 10 6}
Without using lsort
finally o/p to be in same array variable : arr { 1 2 3 5 6 8 9 10}
While we won't give you the answer, we can suggest a few useful things. For example, you compare two elements of a list (at
$idx1
and$idx2
) with this:And you might use this procedure to swap those two elements:
Which you'd call like:
Generally, you can do a sorting algorithm like this:
Everything else is optimization. (Bear in mind that real Tcl programs just use
lsort
because it implements an efficient algorithm with many good properties, and the rest either don't need sorting or delegate the problem to a database engine…)