'Default algorithm for lsearch in tcl and way to improve its runtime?

I want to know which search algorithm does lsearch in tcl uses in the background. Also, if the list is sorted and we have not given the option -sorted in the lsearch, does it still uses binary search to perform the search. And if there is any way to make lsearch more efficient, Do let me know.

I tried different cases and i am still confused as which way is the most efficient.



Solution 1:[1]

If you don't specify -sorted it will do a linear scan, note that you can change the starting point for this with the -start option, see https://www.tcl-lang.org/man/tcl/TclCmd/lsearch.htm#M14 , also using -sorted implies exact matching, not glob or regexp.

If you need to search for a value in a large set it's likely to be more efficient to use a Tcl array or a dict since these use hashing to find exact matches quickly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Colin Macleod