我不知道为什么我的数组没有保存在这个冒泡排序中的随机值
I don't know why my array doesn't have its random values saved in this bubble sort
我想做一个简单的冒泡排序练习,但我不知道我应该改变什么才能完成这项工作,同时又不影响我现有的绘图方法。这是我的代码:
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;
}
}
我的猜测是我的值数组没有保存在 paint 方法中创建的值,这就是交换方法不起作用的原因,但我不确定。
This is my output
if (a > b){
swap(values, j, j+1);
}
应该是
if (r > b){
swap(values, j, j+1);
}
下面是固定的for循环。您正在使用不正确的索引,以及比较不正确的值。
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);
}
}
}
在冒泡排序中,您将遍历整个数组,如果在索引 i
和 i+1
处有一对似乎未排序的两个数字,则交换它们的值索引,你就这样继续,直到当前循环结束。然后你必须再次循环,因为检测到未分类的部分。当您遍历所有条目并且检测到数组没有变化时,算法结束。
我建议使用带有 while
和 for
循环的简单的 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
}
}
}
我想做一个简单的冒泡排序练习,但我不知道我应该改变什么才能完成这项工作,同时又不影响我现有的绘图方法。这是我的代码:
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;
}
}
我的猜测是我的值数组没有保存在 paint 方法中创建的值,这就是交换方法不起作用的原因,但我不确定。
This is my output
if (a > b){
swap(values, j, j+1);
}
应该是
if (r > b){
swap(values, j, j+1);
}
下面是固定的for循环。您正在使用不正确的索引,以及比较不正确的值。
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);
}
}
}
在冒泡排序中,您将遍历整个数组,如果在索引 i
和 i+1
处有一对似乎未排序的两个数字,则交换它们的值索引,你就这样继续,直到当前循环结束。然后你必须再次循环,因为检测到未分类的部分。当您遍历所有条目并且检测到数组没有变化时,算法结束。
我建议使用带有 while
和 for
循环的简单的 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
}
}
}