![]() |
|
[Java] Need help about Selection Sort in Java ! - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Need help about Selection Sort in Java ! (/showthread.php?tid=5736127) |
Need help about Selection Sort in Java ! - Teleforce - Oct 13, 2017 Hi guys, I need answer for this question if your guys know the answer. - What is the big-O running time for selection sort if all of the elements are equal ? I appreciate your help 🤝 * - GammaG - Oct 13, 2017 Can you post a code snipped of the algorithm you use? I'd say depends on how much loops are in there and how the comparison of the elements is implemented. * - Teleforce - Oct 13, 2017 (Oct 13, 2017 01:37 AM)GammaG Wrote: Can you post a code snipped of the algorithm you use? For example this simple program Code package Selection_sort;* - Solixious - Oct 13, 2017 The run time of selection sort is typically O(n^2), where n is the size of the input. * - Teleforce - Oct 13, 2017 (Oct 13, 2017 05:29 AM)Solixious Wrote: The run time of selection sort is typically O(n^2), where n is the size of the input. could you give me more details ? 🤝 * - UB3R5W4G0V3RL0RD - Oct 13, 2017 (Oct 13, 2017 07:21 AM)Teleforce Wrote: could you give me more details ? I'll just copy an explanation from another website which I found fairly comprehensible. The site itself explains the most common ones and how to find them, along with some nice gifs to help visualize them. Link: corejavainterviewquestions.com/idiots-guide-big-o/ (Can't post links yet, sorry! Just add www to the beginning) tl;dr Wrote:O(n^2)/Quadratic Complexity: Things are getting extra slow. 1 item takes 1 second, 10 items takes 100, 100 items takes 10000. Explanation Wrote:The Bubble Sort algorithm is everyone’s first algorithm in school, and interestingly it is quadratic complexity. If you need a reminder; we go through the list and compare each element with the one next to it, swapping the elements if they are out of order. At the end of the first iteration, we then start again from the beginning, with the caveat that we now know the last element is correct. * - Solixious - Oct 13, 2017 (Oct 13, 2017 07:21 AM)Teleforce Wrote: could you give me more details ? Certainly. The outer loop in the selection sort runs n times. For each time the outer loop is executed, the inner loop is executed up to a maximum of n times and average of n/2 times. Thus, the total number of times the statements within the inner loop is executed is averagely Code n * n / 2That is in order of n^2, hence the run time complexity of selection sort is O(n^2). You may google for theoretical definition of run time complexity and Big O notation to understand what it means exactly if you are having trouble with that. Regards. |