Bubblesort不工作,为什么? [英] Bubblesort not working, why?

查看:124
本文介绍了Bubblesort不工作,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包程序; 

import java.util.Arrays;

public class bubbleSort {

public static void main( String [] args){
// TODO自动生成方法存根

int [] array = { 2 ,< span class =code-digit> 3 , 45 66 4 343 243 452 532 235 6 };
sort(array);
系统。 out .println( 已排序数组是 + + Arrays.toString(array));


}

private static void sort( int [] array){

if (array == null || array.length< 2){
return ;
}
int lenthOfArray = array.length;
for int i = 0 ; i < lenthOfArray-1; i ++){
if (array [i ]> array [i + 1]){
int temp = array [i];
array [i] = array [i + 1];
array [i] = temp;
}

}

解决方案





它不起作用,因为for循环只被调用一次。实际上,如此处,维基百科 [ ^ ],bubblesort算法应该运行该循环,直到数组被排序。



你应该添加一个 do {...} while(array_not_sorted); 和那个 array_not_sorted 是一个布尔变量。如果在最后一次迭代中你交换了一些元素,则数组不会被排序,并且应该重新执行do循环,否则数组将被排序,并且do和for循环都应该结束。现在尝试自己编写代码;-)



希望这有帮助



LG

我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



调试器是你的朋友。它会告诉你你的代码到底在做什么。

一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]


package programs;

import java.util.Arrays;

public class bubbleSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[] array={2,3,45,66,4,343,243,452,532,235,6};
		sort(array);
		System.out.println("Sorted Array is "+ " "+Arrays.toString(array));
		

	}

	private static void sort(int[] array) {
		
		if(array==null||array.length<2){
			return;
		}
		int lenthOfArray=array.length;
		for(int i=0;i < lenthOfArray-1;i++){
			if(array[i]>array[i+1]){
				int temp=array[i];
				array[i]=array[i+1];
				array[i]=temp;
			}
			
		}

解决方案

Hi,

It's not working because the for loop is called only once. Actually, as seen here, on Wikipedia[^], the bubblesort algorithm should run that loop until the array is sorted.

Do you should add a do{...}while(array_not_sorted); and that array_not_sorted is a boolean variable. If in the last iteration you've swapped some elements, the array is not sorted and the do loop should be re-executed, otherwise the array is sorted and both the do and for loop should end. Try to write the code by yourself now ;-)

Hope this helps

LG


I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于Bubblesort不工作,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆