不使用lsort的TCL排序程序 [英] TCL Sort program without using lsort

查看:126
本文介绍了不使用lsort的TCL排序程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用任何排序技术

array包含变量arr: arr {1 5 2 9 3 8 10 6}

array Variable arr contained : arr { 1 5 2 9 3 8 10 6}

不使用lsort

最终o/p在同一数组变量中: arr {1 2 3 5 6 8 9 10}

finally o/p to be in same array variable : arr { 1 2 3 5 6 8 9 10}

推荐答案

虽然我们不会给您答案,但是我们可以提出一些有用的建议.例如,您将列表的两个元素(在$idx1$idx2处)与此进行比较:

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:

string compare [lindex $theList $idx1] [lindex $theList $idx2]

您可以使用此过程交换这两个元素:

And you might use this procedure to swap those two elements:

proc swap {nameOfListVar idx1 idx2} {
    upvar 1 $nameOfListVar theList
    set tmp [lindex $theList $idx1]
    lset theList $idx1 [lindex $theList $idx2]
    lset theList $idx2 $tmp
    return
}

您希望打给谁:

# Pass the list variable *name*
swap theList $idx1 $idx2

通常,您可以执行如下排序算法:

Generally, you can do a sorting algorithm like this:

  • 虽然列表未排序,
    • 以错误的顺序找到一对元素并交换它们.
    • While the list is not sorted,
      • find a pair of elements in the wrong order and swap them.

      其他所有事情都是优化. (请记住,真正的Tcl程序只使用lsort,因为它实现了具有许多良好属性的高效算法,而其余的则不需要排序或将问题委托给数据库引擎……)

      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…)

      这篇关于不使用lsort的TCL排序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆