Lua write Sorting Algorithm
As early as 12 years ago, I learned lua for a month. At that time, I watched programming in lua. I never used it, and I forgot it. Now I make up my mind to learn it again.
After a long time, the enthusiasm for programming disappears, and it is difficult to find the fun of programming. Playing the League of legends has been a waste of time for a recent holiday, and it has been a waste of time for a dozen games and a day has passed. So today I uninstalled it. If you are a hero league player, do not indulge in it.
Less than a year ago, I was a little tired. My colleagues agreed that gaming companies are generally impetuous, and some small companies do not have any technical atmosphere. I know that some programmers are far more technical than ordinary game programmers, because there are too few reliable game companies and other developers.
When the spam is finished, write a selection order using lua: -- select sortfunction 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 has a sort function. The manual description is as follows: Sorts table elements in a given order, in-place, fromtable[1]Totable[n], WherenIs the length of the table. IfcompIs given, then it must be a function that includes es two table elements, and returns true when the first is less than the second (so thatnot comp(a[i+1],a[i])Will be true after the sort). IfcompIs 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. so you can also write: function comp (a, B)
Return a <B
End
Table. sort (tb, comp)
Of course, you can usually use the anonymous function table. sort (tb, function (a, B)
Return a <B
End ))
End