Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修改BubbleSort代码_Java_Sorting_Bubble Sort - Fatal编程技术网

Java 修改BubbleSort代码

Java 修改BubbleSort代码,java,sorting,bubble-sort,Java,Sorting,Bubble Sort,我正在努力准备一个测试,其中一个学习问题我无法与BubbleSort达成协议。问题是: 修改下面的BubbleSort代码以缩短其执行。我的意思是,对它进行修改,以便在不进行任何交换的情况下完成传递时,执行将停止 public static<T extends Comparable<T>> void bubbleSort(T[] data) { int position, scan; T temp; for (position = data.length -

我正在努力准备一个测试,其中一个学习问题我无法与BubbleSort达成协议。问题是:

修改下面的BubbleSort代码以缩短其执行。我的意思是,对它进行修改,以便在不进行任何交换的情况下完成传递时,执行将停止

public static<T extends Comparable<T>> void bubbleSort(T[] data)
{
  int position, scan;
  T temp;

  for (position = data.length - 1; position >= 0; position--)
  {
    for (scan = 0; scan <= position - 1; scan++)
    {
      if (data[scan].compareTo(data[scan+1]) > 0)
      {
        /**Swap the values*/
        temp = data[scan];
        data[scan] = data[scan+1];
        data[scan + 1] = temp;
      }
    }
  }
}    
publicstaticvoidbubblesort(T[]数据)
{
int位置,扫描;
温度;
对于(位置=data.length-1;位置>=0;位置--)
{
用于(扫描=0;扫描0)
{
/**交换值*/
温度=数据[扫描];
数据[扫描]=数据[扫描+1];
数据[扫描+1]=温度;
}
}
}
}    

您需要一个布尔标志或状态

快速的谷歌搜索可以帮你省去麻烦:


我想我错过了那个链接,但谢谢你!我真的很感激
repeat
    hasChanged := false
    decrement itemCount
    repeat with index from 1 to itemCount
        if (item at index) > (item at (index + 1))
            swap (item at index) with (item at (index + 1))
            hasChanged := true
until hasChanged = false