JAVA 中的冒泡排序代码

Bubble sort code in JAVA

我还是没弄清楚。冒泡排序中的代码不正确。我怎样才能做到这一点?我应该更改或添加什么以获得正确的结果?提前致谢。 :)

import java.util.Random;
import java.util.Scanner;

public class HomeWork {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        int choice;
        int e;

        Random t = new Random();
        for (e = 1; e <= 5; e++) {
            System.out.println(t.nextInt(1000));
        }
        System.out.println(" \n1: BUBBLE SORT ");
        System.out.println(" 2: SELECTION SORT ");
        System.out.println(" 3: QUICK SORT ");
        System.out.println(" Choose a number from 1-3 ");
        choice= s.nextInt();

        if(choice == 1) {
            System.out.print("You chose BUBBLE sort!");
            int temp, q, w;
            for(int i=0;i<w-1;i++) { //I think there is something wrong here in my bubble sort code. 
                       // What should I add or change to make this correct?
                for(int j=0;j<w-1-i;j++) {
                    if(q[j]>q[j+1]) {
                        temp = q[j];
                        q[j] = q[j+1];
                        q[j+1] = temp;
                        System.out.println(q[i]+""); // What should I change here to print the correct results?
                    } else if(choice == 2) {
                        System.out.print("You chose SELECTION sort!");
                    } else if(choice == 3) {
                        System.out.println("You chose QUICK sort!");
                    } else {
                        System.out.println("Not in the choices!");
                    }
                }
            }
        }
    }
}

我还是个初学者。请帮忙。提前致谢:)

您的问题是您没有定义 qw——您可能希望它们是数字数组及其长度。此外,因为冒泡排序不会自动检测列表何时排序并停止,所以它更像是组合 bubble/selection 排序。

public class BubbleSort {

    public static void main(String[] args) {

        int a[] = { 1, 5, 100, 40, 80, 50 };

        int length = a.length;
        int temp;

        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length - i; j++) {
                if (a[j - 1] > a[j]) {
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }

        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}
public static void main(String[] args) {

    int a[] = { 1, 5, 100, 40, 80, 50 };

    int length = a.length;
    int temp;

    for (int i = 0; i < length; i++) {
        for (int j = 1; j < length - i; j++) {
            if (a[j - 1] > a[j]) {
                temp = a[j - 1];
                a[j - 1] = a[j];
                a[j] = temp;
            }
        }
    }

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

就像上面评论的那个人,这是冒泡排序算法,上面那个人,你的变量(w)是错误的,都是错误的,作为未来的提示,post错误,有了代码,这可能就是你投反对票的原因,或者因为这很简单。

也请原谅我,但我没有 eclipse,现在正在用 C++ 编写代码,但这应该可以工作

int x[] = new int[5]
Random t = new Random();
for (e = 1; e <= 5; e++) {
    x[e] = t.nextInt(1000); // you didn't even assign any variables to sort, that's one problem, you just printed them.
} // and idk how you get random int's in java, but if this doesn't work, just make your own random int generator.
  // There's LOTS of better ones than the one your using now.

int temp, q, w;

for(int i=0;i< 5;i++) { 
    for(int j=0;j<5-i;j++) {
        if(q[j]>q[j+1]) {
            temp = q[j-1];
            q[j-1] = q[j];
            q[j] = temp;
        } 
        // Add 'else ifs' here
    }
}

for (int i = 0; i < 5; i++) { // and this will print the results
    System.out.print(q[i] + " ");
}

这应该行得通,idk,在 java >.> 方面没有真正的经验,顺便说一句,有大量的书籍教算法的,你应该首先仔细阅读你的代码,因为它是一个简单的像现在这样的错误,堆栈溢出的这些人并不仁慈,所有经验丰富的程序员都对你很苛刻,但他们很聪明。 (像我一样 :D)

[编辑] - 顺便说一句,只需将其与您的代码合并即可,但这里有一些有用的站点。 对于随机整数 - http://www.javapractices.com/topic/TopicAction.do?Id=62 对于冒泡排序 - http://examples.javacodegeeks.com/core-java/bubble-sort-algorithm-in-java-code-example/