简体   繁体   English

我不知道为什么我的数组没有在此冒泡排序中保存其随机值

[英]I don't know why my array doesn't have its random values saved in this bubble sort

I wanted to do a simple bubble sort exercise but I don't know what should I change in order to make this work but also not to screw with the drawing method that I have.我想做一个简单的冒泡排序练习,但我不知道我应该改变什么才能完成这项工作,但也不知道我拥有的绘图方法。 Here is my code:这是我的代码:

package sorting;

import java.awt.Graphics;
import javax.swing.JFrame;

public class Sorting extends JFrame{

    public int[] values = new int[800];

    public static void main(String[] args) {
        Sorting sort = new Sorting();
    }

    public Sorting(){
        setSize(800, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setTitle("Sorting");
    }

    @Override
    public void paint(Graphics g){        
        for (int i = 0; i < 800; i++){
            values[i] = (int)(Math.random()*500);
            for (int k = 0; k < 800; k++){
                g.drawLine(k, 500, k, 500-values[k]);
            }
        }
        for (int j = 0; j < 800; j++){
            for (int a = 0; a < 800 - j - 1; a++){
                int r = values[j];
                int b = values[j + 1];
                if (a > b){
                    swap(values, j, j+1);
                }
            }
        }
    }

    private void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}

My guess is that my values array doesn't have the values created in paint method saved and that's why the swapping method isn't working but I'm not sure.我的猜测是我的 values 数组没有保存在paint方法中创建的值,这就是交换方法不起作用但我不确定的原因。

This is my output这是我的输出

if (a > b){
    swap(values, j, j+1);
}

Should be应该

if (r > b){
    swap(values, j, j+1);
}

Following is the fixed for loop.以下是固定的 for 循环。 You are using incorrect index, as well as comparing incorrect values.您使用了不正确的索引,以及比较了不正确的值。

for (int j = 0; j < 800; j++){
    for (int a = 0; a < 800 - j - 1; a++){
        int r = values[a];
        int b = values[a + 1];
        if (r > b){
            swap(values, a, a+1);
        }
    }
}

In bubble sort you would be looping through whole array and if there is a pair of two numbers at index i and i+1 that does not seem to be sorted, you swap values at those indexes and you proceed like that till the end of your current loop.在冒泡排序中,您将遍历整个数组,如果在索引ii+1处有一对似乎未排序的两个数字,则您交换这些索引处的值,然后继续执行直到结束电流回路。 Then you have to loop through again because there was a unsorted part detected.然后您必须再次循环,因为检测到未排序的部分。 The algorithm ends when you loop through all entries and you detect no change to the array.当您遍历所有条目并且检测到数组没有变化时,算法结束。

I would propose to use simple buuble sort implementation with while and for loop :我建议在whilefor循环中使用简单的 buuble 排序实现:

boolean isSorted = false; // assume array is not sorted

while (!isSorted) {  //repeat until array is sorted

     isSorted = true;  // assume array is sorted

     for (int i = 0; i < values.length - 1; i++) { //loop through array
         int a = values[i];  //get first value to compare
         int b = values[i + 1];  // get second value to compare

         if (a < b) {  // if first value is less than second, swap them
             swap(values, i, i + 1);
             isSorted = false;  // there was a change so array is not actually sorted! So we will have to loop through whole array again
         }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的冒泡排序不能对我的对象数组进行排序? - Why won't my bubble sort sort my array of objects? 我不知道为什么我的主类没有从我的 Gameplay 类中获取数据 - I don't know why my main class doesn't obtain data from my Gameplay class 我不知道为什么我非常简单的 JPA 项目不起作用 - I don't know why my very simple JPA project doesn't work 数组中的一个数字应该分配给一个变量,但它没有,我不知道为什么 - A number from array SHOULD get assigned to a variable, but it doesn't and i don't know why transactionTemplate不适用于mybatis,但我不知道为什么 - transactionTemplate doesn't work with mybatis but I don't know why Hibernate Validator不起作用,但我不知道为什么 - Hibernate Validator doesn't work but I don't know why 我不知道为什么循环不会停止! 爪哇 - I don't know why the loop doesn't stop! Java 扑克骰子游戏,我不知道为什么我的方法有错误 - Poker dice game, and I don't know why my methods have errors on them 即使所有值都保存在数组中,为什么我的 find 方法在这里不起作用? - Why doesn't my find method work here even with all the values saved on the array? 我的二叉搜索树不打印值,不知道是打印方式还是添加方式有问题 - My binary search tree won't print the values, and I don't know whether its the printing method or the adding method that's wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM