婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Lua中寫排序算法實例(選擇排序算法)

Lua中寫排序算法實例(選擇排序算法)

熱門標簽:怎么去除地圖標注 清朝地圖標注哈爾濱 冀州市地圖標注 地圖標注大廈 百度地圖標注早餐區域 新岸線智能電銷機器人 武漢外呼防封系統多少錢 個人怎么在地圖標注需要的店鋪 漳州智云呼電話機器人

早在12年的時候,學過一個月的lua,當時看的是《programming in lua》,一直沒用過,然后就忘了。現在我下定決心重新學習它。

時間久了,對編程的熱情也隨之消失殆盡,很難找回當初編程的樂趣了。近來一放假就玩英雄聯盟,太浪費時間,玩個十來局一天就過去了,渾渾噩噩的,這實在不是我想過的。所以,今天我把它卸載了。如果你也是英雄聯盟玩家,希望你不要沉迷其中。

從事游戲開發還不到一年,已經有點厭倦了,同事們一致認為游戲公司普遍很浮躁,有些小公司沒有一點技術氛圍。我知道的有些程序員,技術遠遠比普通游戲程序員強,由于靠譜的游戲公司太少而做其他開發了。

吐槽完了,用lua 寫個選擇排序:

復制代碼 代碼如下:

--select sort
function select_sort(t)
     for i=1, #t - 1 do
          local min = i
          for j=i+1, #t do
               if t[j] t[min]  then
                    min = j
               end
          end
          if min ~= i then
               t[min], t[i] = t[i], t[min]
          end
     end
end
tb = {77, 99, 2, 334, 22, 32, 9}
print("-------------before--------------")
print(table.concat(tb, " "))
print("-------------after---------------")
select_sort(tb)
print(table.concat(tb, " "))

table帶有個sort函數,手冊說明如下:
復制代碼 代碼如下:

Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator is used instead.
The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

因此你也可以這么寫:
復制代碼 代碼如下:

function comp(a, b)
     return a b
end

table.sort(tb, comp)


當然,通常你可以使用匿名的函數
復制代碼 代碼如下:

table.sort(tb, function(a, b)
     return a b
end)

標簽:臺灣 天門 金昌 儋州 濰坊 德宏 宣城 天門

巨人網絡通訊聲明:本文標題《Lua中寫排序算法實例(選擇排序算法)》,本文關鍵詞  Lua,中寫,排序,算法,實例,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Lua中寫排序算法實例(選擇排序算法)》相關的同類信息!
  • 本頁收集關于Lua中寫排序算法實例(選擇排序算法)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 千阳县| 镇沅| 礼泉县| 邵阳市| 吴忠市| 赤峰市| 延边| 祁阳县| 都江堰市| 类乌齐县| 抚州市| 顺平县| 德惠市| 叙永县| 思南县| 治多县| 原平市| 襄垣县| 泸州市| 德保县| 南投市| 北川| 奎屯市| 卫辉市| 香港| 同德县| 木里| 乌拉特中旗| 大丰市| 都匀市| 扶风县| 象山县| 怀宁县| 新宁县| 许昌县| 六安市| 棋牌| 石河子市| 游戏| 内丘县| 上林县|