As early as 12, Lua, who had studied one months, looked at "programming in Lua", never used it, and then forgot. Now I am determined to learn it again.
For a long time, the enthusiasm for programming disappeared, it is difficult to get back the fun of the original programming. Recently a holiday to play the League of Heroes, too waste of time, playing a dozen innings a day passed, the unexamined, this is really not what I thought of. So, today I unloaded it. If you are a Hero league player, I hope you don't get addicted to it.
Engaged in game development less than a year, has been a bit tired, colleagues agree that the game company is generally very impetuous, some small companies do not have a bit of technical atmosphere. Some of the programmers I know are far more skilled than ordinary game programmers, and have done other things because of the lack of reliable gaming companies.
After the spit is over, write a selection sort with Lua:
Copy Code code as follows:
--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 comes with a sort function, as described in the manual:
Copy Code code as follows:
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 being a function that receives two table elements, and returns True when the This is less tha n the second (so, not comp (a[i+1],a[i)) is true after the sort. If comp is isn't given, then the standard Lua operator < is used instead.
The sort algorithm is not stable; That's, elements considered equal by the given of the order may have their relative-positions by the sort.
So you can also write this:
Copy Code code as follows:
Function Comp (A, B)
Return a < b
End
Table.sort (TB, comp)
Of course, you can usually use anonymous functions
Copy Code code as follows:
Table.sort (TB, function (A, B)
Return a < b
End
Finish