Answer the question
In order to leave comments, you need to log in
How to apply array sorting in Java?
Hello Toaster Users!
I had a problem when learning Java, I read books on it, everything was clear, even in practice I could do something, but 2 days ago I stumbled over the topic of sorting arrays. I read, searched in books, but there are no examples available to my understanding.
Can someone explain to me:
- how to combine nested loops with sorting?
- how to add sorting itself? (for example, fast or through Array.sort())
I would be very grateful for a detailed answer, thank you very much for your attention!
Answer the question
In order to leave comments, you need to log in
Can someone explain to me:
- how to combine nested loops with sorting?
- how to add sorting itself?
- Nested loops can be counted when there is an understanding of what a loop is, if you decide to write a nested loop, then for each outer loop, the inner loop will be executed the number of times that you set, after which, as soon as the inner loop is completed, the second outer loop will start, and so on until the outer loop will end (the main loop) which will run the inside. (I hope I answered the question because it is not entirely clear). To count them here is already a matter of the need for how many times to sort into which cycle to push (external or internal) ..
- Add sorting I think it will turn out the code will understand how cycles work and what each one does
If you need speed, then look for examples of fast sorting algorithms, it is searched pretty quickly in Google.
If you want to understand sorting, then you need to "google" "sorting algorithms", there will be examples in different languages.
java.util.Arrays.sort(primitive types) - implemented quick sort algorithm;
java.util.Arrays.sort(objects) - merge sort algorithm;
It is unlikely that you will be able to make a more efficient implementation ...
If you are interested in sorting collections, you can use the Collections.sort(Collection, Comparator) method.
More details can be read here .
In all sorting algorithms, only two operations are used: comparison and exchange of elements (swap). Nested loops are just used for this, to compare each element of the array with the others (for example, in bubble sort).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question