气泡排序代码

时间:2018-06-27 22:31:19

标签: bubble-sort java

这是我的冒泡排序代码,但是我很困惑为什么输出仅显示125。

int secondArray[] = { 0, 1, 5, 2 };
int num;

for (int i = 1; i < secondArray.length; i++) {
    for (int j = 0; j < secondArray.length - i ; j++) {
        if (secondArray[j] > secondArray[j + 1]) {
            num = secondArray[j];
            secondArray[j] = secondArray[j+1];
            secondArray[j+1] = num;
        }
    }
    System.out.print(secondArray[i]);
}

7 个答案:

答案 0 :(得分:2)

这是因为您要从1进行迭代-> int i = 1;,但是数组从0开始,所以System.out.print(secondArray[i]);永远没有机会显示第一个元素。

答案 1 :(得分:0)

请运行以下代码:

    int secondArray[] = { 0, 1, 5, 2 };
    int num;

    for (int i = 0; i < secondArray.length - 1; i++) {
        for (int j = 0; j < secondArray.length - 1 - i; j++) {
            if (secondArray[j] > secondArray[j + 1]) {
                num = secondArray[j];
                secondArray[j] = secondArray[j+1];
                secondArray[j+1] = num;
            }
        }
    }
    for(int number : secondArray){
        System.out.print(number);
    }

答案 2 :(得分:0)

检查一下。

int secondArray[] = { 0, 1, 5, 2 };
    int temp;

    for(int i = 0, k=secondArray.length; i < secondArray.length / 2;i++) {
        temp = secondArray[i];
        secondArray[i] = secondArray[--k];
        secondArray[k] = temp;
        System.out.println(secondArray[0]+" "+secondArray[1]+" "+secondArray[2]+" "+secondArray[3]);
    }

答案 3 :(得分:0)

public class Example {
public static void sortArray(int[] x){
    for(int j=0; j<x.length-1; j++){
        for(int i=0; i<x.length-1;i++){
            if(x[i]>x[i+1]){
                int temp=x[i];
                x[i]=x[i+1];
                x[i+1]=temp;
            }
        }
    }
}
public static void main(String[] args) {
    int[] x={32,98,35,76,26,89,1,46,21,7};
    System.out.println(Arrays.toString(x));
    sortArray(x);
    System.out.println(Arrays.toString(x));
}

答案 4 :(得分:0)

冒泡排序 - Java 代码

import java.util.*;
class bubblesort {
    public static void main(String[] args) {
        int ar[] = {34, 12, 45, 3, 6, 7, 20};
        sort(ar);
        System.out.print("After sort :  ");
        for (int j = 0; j < ar.length; j++) {
            System.out.print(ar[j] + " ");
        }
    }

    public static void sort(int[] arr) {
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < (arr.length - 1 - i); j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

答案 5 :(得分:0)

冒泡排序的完整代码:

class BubbleSort {
    void bubbleSort(int array[]) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    void printArray(int array[]) {
        for (int i = 0; i < array.length; ++i) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    // Main method
    public static void main(String args[]) {
        BubbleSort bubbleSort = new BubbleSort();
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        System.out.print("Original array : ");
        bubbleSort.printArray(array);
        bubbleSort.bubbleSort(array);
        System.out.print("Sorted array : ");
        bubbleSort.printArray(array);
    }
}

输出:

Original array : 64 34 25 12 22 11 90 
Sorted array : 11 12 22 25 34 64 90 

答案 6 :(得分:0)

外层 do-while-loop 重复遍历数组直到所有元素顺序正确,而内层 for-loop 遍历数组并进行比较相邻元素。

public static void bubbleSort(int[] arr) {
    boolean swapped;
    // repeat the passes through the array until
    // all the elements are in the correct order
    do {
        swapped = false;
        // pass through the array and
        // compare adjacent elements
        for (int i = 0; i < arr.length - 1; i++) {
            // if this element is greater than
            // the next one, then swap them
            if (arr[i] > arr[i + 1]) {
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
                swapped = true;
            }
        }
        // if there are no swapped elements at the
        // current pass, then this is the last pass
    } while (swapped);
}
public static void main(String[] args) {
    int[] arr = {6, 1, 5, 8, 4, 3, 9, 2, 0, 7};
    System.out.println(Arrays.toString(arr));
    bubbleSort(arr);
    System.out.println(Arrays.toString(arr));
}

输出:

[6, 1, 5, 8, 4, 3, 9, 2, 0, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

另见:Bubble sort with step-by-step outputJava 8