-2

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

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

我仍然只是一个初学者。请帮忙。提前致谢 :)

4

3 回答 3

2

您的问题是您没有定义qw- 大概您希望它们成为数字数组及其长度。此外,由于冒泡排序不会自动检测列表何时排序并停止,它更像是组合的冒泡/选择排序。

于 2015-03-08T06:02:57.870 回答
0
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] + " ");
        }
    }
}
于 2015-03-08T06:14:20.970 回答
0
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)是错误的,全错了,作为未来的提示,用代码发布错误,这可能就是你得到的原因投反对票,或者因为这很简单。

也请原谅我,但我没有 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)

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

于 2015-03-08T06:40:23.220 回答