The Java standard library has three groups of overloaded methods for sorting:java.util.Arrays.sort(prim, ...) - for grading Amounts of primitives prim (Prim)int♪ byte♪ charetc.). Uses algorithm. https://ru.wikipedia.org/wiki/%D0%91%D1%8B%D1%81%D1%82%D1%80%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0 (quick sort).java.util.Arrays.sort(T, ...) and java.util.Arrays.sort(Object, ...) - for grading complex sites arbitrary type T or Object♪ Uses algorithm. https://ru.wikipedia.org/wiki/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D1%81%D0%BB%D0%B8%D1%8F%D0%BD%D0%B8%D0%B5%D0%BC (merge sort). https://docs.oracle.com/javase/9/docs/api/java/util/Collections.html#sort-java.util.List- - for the collections. Uses a merger algorithm (merge sort).The reason why a merger algorithm is chosen for the collection and collection of complex objects is that the algorithm is Sustainable, unlike fast sorting, fragile♪ https://ru.wikipedia.org/wiki/%D0%A3%D1%81%D1%82%D0%BE%D0%B9%D1%87%D0%B8%D0%B2%D0%B0%D1%8F_%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0 It is called an algorithm that retains the sequence of components with the same sorting keys. For example, let us have the following pairs of numbers on the first pair:(4, 2) (3, 7) (3, 1) (5, 6)
Then on the exit, depending on the algorithm used, we can get two different results:(3, 7) (3, 1) (4, 2) (5, 6) (порядок сохранен)
(3, 1) (3, 7) (4, 2) (5, 6) (порядок изменен)
In the first case, couples (3, 7) and (3, 1) with equal keys of grading remained in the same order as in the original. In the second case, they changed places.Why is it important to have a steady sorting? It allows for the production of a chain of sequential grading seeking to be sorted in groups and sub-groups of the collection♪ Let's show what this means, in the example (examples are recorded in the Scala language to minimize the excess code inherent in Java, but the point will be clear). Let us have a data structure to present musical records:// `albumName` - имя музыкального альбома;
// `trackNumber` - номер трека в альбоме.
case class Record(albumName: String, trackNumber: Int)
Let us place on the list several musical works (Mtallica diskography) arbitrarily:val records = List(Record("Master of Puppets", 7), Record("Load", 4),
Record("Load", 2), Record("Reload", 5),
Record("Load", 3), Record("Reload", 3))
Now we'll sort out the track number:scala> val byTrackNumber = records.sortBy(_.trackNumber)
byTrackNumber: List[Record] = List(Record(Load,2), Record(Load,3), Record(Reload,3), Record(Load,4), Record(Reload,5), Record(Master of Puppets,7))
And then we fight again, but it's Album's name:scala> byTrackNumber.sortBy(_.albumName)
res4: List[Record] = List(Record(Load,2), Record(Load,3), Record(Load,4), Record(Master of Puppets,7), Record(Reload,3), Record(Reload,5))
What did we get? We've got a list of records that's on the name of the album, and in every album, we've got a track-listed! To this end, we first used a less relevant key (trace number) that sorts for us sub-groups (albomas). Then it's album's name. This sequence is important. It should be noted that this result could have been obtained if the album and track numbers were quashed once, but there are situations where we are unknown in advance all the keys we will sort the collection. For example, the user of the program can select in the graphic interface first the grading of a track number field, and then the field of album.If we had used an unstable sorting, the above result is not guaranteed. Why then is rapid grading used to sort primitives that are unsustainable? Because equal awards (numbers, symbols) are indifferent to each other when converting, unlike integrated objects. In other words, primitives can only be sorted by one key (their values). Therefore, they are allowed to use a faster algorithm, which is fast-track.