![]() |
|
[Java] Optimizing this simple method? - 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] Optimizing this simple method? (/showthread.php?tid=5410421) |
Optimizing this simple method? - Dyst0pia - Sep 19, 2016 I need to make 1 array out of two different arrays and then sort the new array, I've done it this way and it works perfectly but I keep thinking that its not the most effective way to do it. Does anyone have any input on how I can optimize this code? PHP Code * - Stealth™ - Sep 19, 2016 With Java 8, you can do it in a very simple way! * - Dyst0pia - Sep 20, 2016 (Sep 19, 2016 05:06 PM)Stealth™ Wrote: With Java 8, you can do it in a very simple way! Sorry I should have explained better, I'm not allowed to use built in java methods. So that won't be possible. * - Sogomn - Sep 20, 2016 You could use another sorting algorithms than bubblesort. It has a runtime of O(n²). * - Goodies - Sep 20, 2016 (Sep 19, 2016 05:06 PM)Stealth™ Wrote: With Java 8, you can do it in a very simple way! Not to mention java 8 streams, while sexy, are not efficient. At least not yet. * - Stealth™ - Sep 20, 2016 (Sep 20, 2016 08:35 AM)Goodies Wrote: Not to mention java 8 streams, while sexy, are not efficient. At least not yet. Well there are parallel streams, but I still have no clue how to use them properly yet :P And efficiency shouldn't really be an issue for what OP is trying to achieve. * - Goodies - Sep 20, 2016 The route I went for efficiency and maintaining a low time complexity: ~ quicksort for sorting each array individually with O(n log n) average case. The sorting algorithm was derived straight from the wiki. https://en.wikipedia.org/wiki/Quicksort ~ merging the two sorted arrays efficiently with O(n). I tested it with this. Takes an average of about 43ms on my PC. PHP Code |