Java 我的冒泡排序算法不会对数组进行排序

Java 我的冒泡排序算法不会对数组进行排序,java,arrays,algorithm,sorting,bubble-sort,Java,Arrays,Algorithm,Sorting,Bubble Sort,这是我的密码: import java.util.Scanner; public class BubbleSort{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int[] array = new int[5]; for(int i = 0; i < array.length; i++){ Syste

这是我的密码:

import java.util.Scanner;
public class BubbleSort{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int[] array = new int[5];
        for(int i = 0; i < array.length; i++){
            System.out.println("Enter value: ");
            array[i] = sc.nextInt();
        }
        int temp;
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array.length - 1; j++) {
                if(array[j] > array[j + 1]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = array[j];
                }
            }
        }
        System.out.print("Sorted Array: ");
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

你需要换一行

array[j + 1] = array[j];
array[j+1]=array[j]

对此


数组[j+1]=temp

您的整数交换逻辑不正确

更换这条线

array[j + 1] = array[j];

import java.util.Scanner;
公共类泡泡糖{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int[]数组=新的int[5];
for(int i=0;i数组[j+1]){
温度=阵列[j];
数组[j]=数组[j+1];
数组[j+1]=temp;//######
}
}
}
System.out.print(“排序数组:”);
for(int i=0;i
改进 在每一步中,最大的元素都在最右边的位置移动。因此,您不需要每次都迭代到最右侧的内部循环

for(int i = 0; i < array.length; i++) {
    for(int j = 0; j < array.length - i - 1; j++) { // early pruning of loop
        if(array[j] > array[j + 1]) {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}
for(int i=0;i数组[j+1]){
温度=阵列[j];
数组[j]=数组[j+1];
阵列[j+1]=温度;
}
}
}

您忘记重新分配温度:

array[j + 1] = array[j];
应该是

array[j + 1] = temp;
此外,由于数组的右侧已经排序,因此可以跳过内部循环执行中的该部分以获得性能提升

如果你想看完整的参考资料,这是我的完整代码

public int[] bubbleSort(int[] list) {
  int len = list.length;
  if (len <= 1) 
    return list;

  for (int i = 0; i < len - 1; i++) {
     // The right side of the list is ordered and have no need to check
    for (int j = 0; j < len - i - 1; j++) { 
      if (list[j] > list[j + 1]) {
        int temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
      }
    }
  }     
}
public int[]bubbleSort(int[]list){
int len=list.length;
if(len list[j+1]){
int temp=列表[j];
列表[j]=列表[j+1];
列表[j+1]=温度;
}
}
}     
}

检查您的交换逻辑。您在哪里设置
temp
?更改
array[j+1]=array[j]
数组[j+1]=温度这个问题的标题很好。你和OP已经实现了选择排序而不是冒泡排序。@BrijeshBhatt看一看很抱歉。。抱歉…!!:-)
array[j + 1] = temp;
public int[] bubbleSort(int[] list) {
  int len = list.length;
  if (len <= 1) 
    return list;

  for (int i = 0; i < len - 1; i++) {
     // The right side of the list is ordered and have no need to check
    for (int j = 0; j < len - i - 1; j++) { 
      if (list[j] > list[j + 1]) {
        int temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
      }
    }
  }     
}