- It's best practices to let statements like
if, for, do, while
be followed by a space to distinguish them from method invocations. Sometimes you do it, sometimes you don't. - Sometimes you enclose operators with spaces, sometimes you don't. It's best practices to do it always.
- Ending classes with
}}
or}}}
is unusual. - Variable names like
obj
aren't to descriptive. Any decent java programmer knows that a reference variable refers to an object. I'd call themsort.
- SelectionSort:
int listLength = totalPass;
isn't necessary since you don't change its value, you just use it. - Declaring variables inside a loop (
Integer temp<n>, int pointerSmallPosition, int temp<n>, int j, int small, int large
) is advised in generalisn't a good idea in general.
In cases where performance really matters (Diego Martinoia mentioned it in his answer) it's slightly different since the stack element (and heap space in case of an object) for every variable is thrown away and re-allocated with every iteration rather than being reused. (Unless the compiler or the JVM optimizes such. But that's not guaranteed.) Declaring them before the loop also gives the readers of your code (including you in, let's say, 5 years) an idea what's going on inside it.
See also Declare Variables Inside or Outside a Loop.
You can combine both, limited scope and reuse, if you declare variables in for's
initialization in InsertionSort:
private void sortData() {
int size = unsortedList.size();
for (int sortedSize = 0, i = 1, j = i, small, large;
i < size - 1; i++) {
do {
if (unsortedList.get(j) < unsortedList.get(j - 1)) {
small = unsortedList.get(j);
large = unsortedList.get(j - 1);
unsortedList.set(j - 1, small);
unsortedList.set(j, large);
}
} while (--j > sortedSize);
}
}
Note also the decrement of j
that has been moved.