Example of a write sort algorithm in Lua (select sort algorithm) _lua

Source: Internet
Author: User

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

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.