Hello,
I have a general question about sorting arrays.
Why is the variable in the first case j = i+1; in the other case j = 0;
Why is that not in both cases j = i + 1;
Thank you in advance!
First array:
String[] strings = {"x", "d", "b", "a", "c", "h"};
sort(strings);
public static void sort(String[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
String temp="";
if (array[i].compareTo(array[j])>0) {
temp= array[i];
array[i] = array[j];
array[j]=temp;
}
}
}
}
Second array:
int[] array= {5,2,1,8,0};
sort(array);
public static void sort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int temp;
if (array[i] < array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}