So basically, I have set up bubblesort algorithm and weirdly enough it does not return the sorted array just the unsorted array.
The given array;
int[] array = { 20, 5, 1, 6, 23, 52, 15, 12 };
Bubble sort algorithm;
public static int[] sort_array(int[] array) {
int [] sorted = array;
int temp = 0;
for (int i = 0; i < sorted.length - 1; i++) {
for (int j = 0; i < sorted.length - i - 1; i++) {
if (sorted[j] > sorted[j + 1]) {
temp = sorted[j];
sorted[j] = sorted[j + 1];
sorted[j + 1] = temp;
}
}
}
return sorted;
}
Also made an array return method;
public static void return_list(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
After the methods are used it just returns me the unsorted array.
int[] array = { 20, 5, 1, 6, 23, 52, 15, 12 };
sort_array(array);
return_list(array);
Output = 20, 5, 1, 6, 23, 52, 15, 12;