[phpBB Debug] PHP Warning: in file /includes/bbcode.php on line 118: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead [phpBB Debug] PHP Warning: in file /includes/bbcode.php on line 118: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead [phpBB Debug] PHP Warning: in file /includes/bbcode.php on line 118: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead [phpBB Debug] PHP Warning: in file /includes/bbcode.php on line 118: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead CareersNG • View topic - Merge Sort Recursion in Java
CareersNG
Career Development for Nigerians ... A Knowledge Sharing Forum!
What was the objective of the exercise? Was it to test your fluency with the language or to examine your ability to construct a working routine? Or both?
Even in extreme case where I had to implement sorting for complex object types (not primitive or Wrappers) in code, I simply marked my class as implementing the Comparable interface and my implementation of the interface was a Façade or Decorator to the actual implementations of the Comparable interface in the primitive box classes. So with literally 2 lines of code, I achieved full sorting functionality for the more complicated object instances associated with my problem.
1. The method loadMyArrayList() can be avoided. The Collections framework provides convenience Array<->Collection "migration" methods -
To convert an array to a collection use Array.asList() found in the Arrays class See here for javadoc
To do the reverse (i.e convert Collection to Array, use Collections.toArray() found in the Collection class See here for javadoc
2. Having "for" loops, indeed any loop/conditional statement for that matter, without braces is not a good programming practice as it can be the reason for many hard to find bugs. This practice is frequent in your code (see the "for" loop in the print method as well as other "if" statements)
3. In printMyArrayList(), explicitly calling toString() is not required as there is an implicit call to the toString method for any object by the PrintStream instance (i.e System.out). Excluding the call makes the code more readable.
4. Looks like method mergeSort() does not do any merging or sorting. Looks like it only splits. If you want to document the code without writing any documentation, then you may want to name that method "split()" or "recursiveSplit()" or something along those lines. That way, anybody can pick up your code and understand what is happening, even without you explaining. Again this is just from my book of good practice, I realise this is just a demo, but then na from clap dem dey enter dance so I might as well just mention.
When I started to write solutions involving thousands of lines of code and I hard to test, debug, modify and re-test, re-debug etc all on my own (and only in spare time, which isnt very plenty nowadays), nobody needed to tell me to embrace best (or better) coding practices.
Users browsing this forum: No registered users and 1 guest
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum