this is the first algorithm that i made:
int tmp = null;
for (int i = arr.length-1; i > 0; i--){
tmp = arr[i];
arr[i] = arr[i-1];
arr[i-1] = tmp;
}
and this is the second algorithm which my computer teacher says to me:
int tmp = null;
for (int i = arr.length-1; i > 0; i--){
for(int j = 0; j < arr.length; j++){
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
help me about which is more efficient. In my experiment using
System.currentTimeMillis();
this method with an array with 100000 datas, the first algorithm was faster but my teacher says the second one would be faster in bigger databases with various things. I know that a LinkedList would do good, but i want to know about this problem.
int tmp = arr[arr.length - 1]; for (int i = arr.length - 1; i > 0; --i) { arr[i] = arr[i - 1]; } arr[0] = tmp;or something like that.int tmp = null;isn't legal.