Open Suiyueemperor opened 10 months ago
public static void bubbleSort(int[] arr) { var lastIndex = arr.length - 1; for(var j = 0; j < lastIndex; j++) { for(var i = 0; i < lastIndex - j; i++) { if(arr[i] > arr[i + 1]) { var tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; } } } }
The outermost loop can be run at most n-1 times, so the changes are as follows
for(var j = 0; j < lastIndex - 1; j++) { for(var i = 0; i < lastIndex - j - 1; i++) {
The outermost loop can be run at most n-1 times, so the changes are as follows