Lua write Sorting Algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.