Java implementation of sorting algorithm
Collections. sort()
Java sorting can be done with collections Sort() sorting function implementation. Use collections The sort method has two methods for sorting lists: the first is that the objects in the list implement the comparable interface, as follows:
Test:
The output results are as follows
The second method is based on collections Sort overload method, for example:
Write this in the main class:
The output results are as follows
The former has a simple code structure, but can only be sorted according to fixed attributes. The latter is flexible and can temporarily specify sorting items, but the code is not concise enough
Choose the best.
Common sorting algorithms let's look at the Java code practice of several classical sorting algorithms:
Bubble sorting
Direct insert sort
Direct selection sort
Heap sort
Quick sort
Merge sort
Test procedure
Let's summarize the above algorithms: