不使用冒泡排序对数组进行排序。

huangapple go评论74阅读模式
英文:

Not sorting an array with bubble sorting

问题

对不起,初学者问题真是抱歉。我遇到了使用冒泡排序对数组进行排序的问题:

class Search {
    public static void main(String args[]) {
        int temp;
        boolean fixed = false;
        while (fixed = false) {
            fixed = true;
            int number[] = {6, 7, 3, 2, 9};
            for (int i = 0; i < number.length - 1; i++) {
                if (number[i] > number[i + 1]) {
                    temp = number[i + 1];
                    number[i + 1] = number[i];
                    number[i] = temp;
                    fixed = false;
                }
            }

            for (int i = 0; i < number.length; i++) {
                System.out.println(number[i]);
            }
        }
    }
}

我应该怎么做来解决我的问题呢?

英文:

Sorry for the beginner question, I faced a problem sorting an array using bubble sorting:

class Search {
    public static void main(String args[]) {
        int temp;
        boolean fixed = false;
        while (fixed = false) {
            fixed = true;
            int number[] = {6, 7, 3, 2, 9};
            for (int i = 0; i < number.length - 1; i++) {
                if (number[i] > number[i + 1]) {
                    temp = number[i + 1];
                    number[i + 1] = number[i];
                    number[i] = temp;
                    fixed = false;
                }
            }

            for (int i = 0; i < number.length; i++) {
                System.out.println(number[i]);
            }
        }
    }
}

what can I do to fix my problem?

答案1

得分: 1

你有一个条件问题(fixed = false),这个条件始终为真,你应该在循环后打印结果,而且你将数字排序错误(从小到大,你本意是相反的)。你还需要在循环之外定义数组。

int temp;
boolean fixed = false;
int number[] = {6, 7, 3, 2, 9};
while (!fixed) {
    fixed = true;
    for (int i = 0; i < number.length - 1; i++) {
        if (number[i] < number[i + 1]) {
            temp = number[i + 1];
            number[i + 1] = number[i];
            number[i] = temp;
            fixed = false;
        }
    }
}

for (int i = 0; i < number.length; i++) {
    System.out.println(number[i]);
}
英文:

You have a condition problem (fixed = false) which is always true, your should print your result after your loop and you sorted the numbers wrong (small to bigger, you intended the other way). You also need to define your array outside the loop.

int temp;
boolean fixed=false;
int number[]= {6, 7, 3, 2, 9 };
while(!fixed) {
    fixed = true;
    for(int i=0; i&lt;number.length-1; i++) {
        if(number[i] &lt; number[i+1]) {
            temp = number[i + 1];
            number[i+1] = number[i];
            number[i] = temp;
            fixed = false;
        }
    }
}

for(int i=0; i &lt; number.length; i++)    {
    System.out.println(number[i]);
}

答案2

得分: -1

1. 1)修复 while (fixed == false)  
2) 修复数组初始化
while 循环外部
3)while 循环外添加 sysout

public class Test {
    
    public static void main(String[] args) {
        int temp;
        boolean fixed = false;
        int number[] = {6, 7, 3, 2, 9};
        while (fixed == false) {
            fixed = true;
    
            for (int i = 0; i < number.length - 1; i++) {
                if (number[i] > number[i + 1]) {
                    temp = number[i + 1];
                    number[i + 1] = number[i];
                    number[i] = temp;
                    fixed = false;
                }
            }
        }
        for (int i = 0; i < number.length; i++) {
            System.out.println(number[i]);
        }
    }
}
输出结果:
2
3
6
7
9
英文:
  1. 1)Fixed while (fixed == false)
  1. Fixed array initialization
    outside while loop
  2. added sysout outside of while loop

public class Test {

    public static void main(String[] args) {
        int temp;
        boolean fixed = false;
        int number[] = {6, 7, 3, 2, 9};
        while (fixed == false) {
            fixed = true;

            for (int i = 0; i &lt; number.length - 1; i++) {
                if (number[i] &gt; number[i + 1]) {
                    temp = number[i + 1];
                    number[i + 1] = number[i];
                    number[i] = temp;
                    fixed = false;
                }
            }


        }
        for (int i = 0; i &lt; number.length; i++) {
            System.out.println(number[i]);
        }

    }

Output:
2
3
6
7
9

huangapple
  • 本文由 发表于 2020年9月29日 14:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64114158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定