They seem right to me. A few minor comments:
You don't need to construct your lists by adding .add(pos, value)
.add(pos, value)
. You can build them from an array as:int[] array = {6,12,9,-14}; List list = new ArrayList<>(Arrays.asList(array);
int[] array = {6,12,9,-14}; List<Integer> list = new ArrayList<>(Arrays.asList(array);
which is more concise and readable.
In your bubblesort, you do:
Integer temp1= unsortedList.get(i + 1); Integer temp2= unsortedList.get(i);
Integer temp1= unsortedList.get(i + 1); Integer temp2= unsortedList.get(i);
accessing unsortedListunsortedList
twice. If you assign outside the IFif
, you can recycle it and make it more readable. Also, consider using better names than temp1temp1
and temp2temp2
: not much difference here, but it's a good habit to get into. Same, same for insertion.