冒泡排序输出不正确

问题描述 投票:0回答:7

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

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]);
}
bubble-sort java
7个回答
2
投票

这是因为你是从 1 ->

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


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);
}

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]);
}

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 

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));
    }
}

输出:

[32, 98, 35, 76, 26, 89, 1, 46, 21, 7]
[1, 7, 21, 26, 32, 35, 46, 76, 89, 98]

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]

另请参阅:带有逐步输出的冒泡排序Java 8


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 - 1; 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;
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.