Java interview questions write bubble sorting algorithm

Write the bubble sort algorithm: Detailed explanation of the bubble sort process

for(int i = 0;i < arr.length-1;i++){
    
    	//控制轮数
	for(int j = 0;j < arr.length-1-i;j++){
    
    	//控制每一轮的次数
		if(arr[j] > arr[j+1]){
    
    	//每次都是和他下一个元素比
			int t = arr[j];		
			arr[j] = arr[j+1];
			arr[j+1] = t;
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108322165