JAVA中的冒泡排序代码

时间:2015-03-08 05:57:04

标签: java arrays sorting drjava

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

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

我还是个初学者。请帮忙。在此先感谢:)

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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] + " ");
        }
    }
}

答案 2 :(得分: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)是错误的,都是错的,并且作为未来的提示,发布错误,用代码,&# 39;这可能是你为什么有一个downvote,或者因为这很简单。

同样请原谅我,但我没有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&gt;中并没有真正体验过。&gt;,顺便说一下,有大量书籍教授算法,你应该首先仔细查看你的代码,因为它&#39像现在一样简单的错误,这些堆栈溢出的人并不怜悯,所有经验丰富的程序员都非常努力,但他们很聪明。 (像我一样:D)

[编辑] -btw,只需将其与您的代码合并,但这里有一些有用的网站。 随机int&#39; s - http://www.javapractices.com/topic/TopicAction.do?Id=62 用于冒泡排序 - http://examples.javacodegeeks.com/core-java/bubble-sort-algorithm-in-java-code-example/