简体   繁体   English

用Java对随机数组排序

[英]Sorting a randomized array in Java

I am trying to make an app for sorting a randomized array I made some code and I can not see what is wrong with it that it returns wrong values Notes: I am trying to learn programming. 我正在尝试制作一个用于对随机数组进行排序的应用程序,我编写了一些代码,但看不到返回错误值的问题是什么。注意:我正在尝试学习编程。 So don't suggest whole different ways of solving the problem. 因此,请勿提出解决问题的完全不同的方法。 I just want to see what is wrong with this code so I can get better. 我只想看看这段代码有什么问题,以便我可以做得更好。 What RandomArrayCreator.create() returns is just an array of numbers in randomized order. RandomArrayCreator.create()返回的只是随机排列的数字数组。

public class ArraySorter
{
public static void main(String[] args)
{
    int[] siyahi = RandomArrayCreator.create();
    int[] siralanmish = new int[siyahi.length];

    for (int i=0;i<siyahi.length;i++)
    {
        for (int j=0;j<siyahi.length;j++)
        {
            for (int k=j+1;k<siyahi.length;k++)
            {
                if (siyahi[k]<siyahi[j]) j=k;
            }
            siralanmish[i]=siyahi[j];
            siyahi[j]=siyahi.length+1;
        }
        System.out.println(siralanmish[i]);
    }
}

} }

I know you did not want suggestions but I'm going to offer one anyway. 我知道您不想要建议,但无论如何我都会提供建议。

Hopefully this will help guide you along the way, but still allow you to come up with your own solution. 希望这会帮助您一路走好,但仍然可以让您提出自己的解决方案。

Sort smallest to biggest. 从小到大排序。

did I have swap an element?
while I swapped an element
    assume I did not swap an element
    for element i in the array
        is i > i+1?
            if yes 
                swap the elements
                I did swap an element
            else
                do nothing

Given that you mentioned you wanted to learn how to improve your current program, here are minimalist changes that will have your code produce a sorted array. 鉴于您提到的您想学习如何改进当前程序,以下是一些极简的更改,这些更改将使您的代码产生一个排序数组。

A few notes on the changes: 有关更改的一些注意事项:

1. 1。

if (siyahi[k]<siyahi[j]) j=k;

This I assume is for trying to swap the values at each indexes. 我认为这是为了尝试交换每个索引的值。 Instead you are assigning the value of k to j which will cause problems with the entire for loop. 相反,您将k的值分配给j,这将导致整个for循环出现问题。 I replaced this with the following: 我将其替换为以下内容:

    if (siyahi[k]<siyahi[j]) 
    {
        int temp = siyahi[j];
        siyahi[j] = siyahi[k];
        siyahi[k] = temp;
    } 

This creates a temporary variable to store one of the values so that you can swap the value at each index without losing one of your values. 这将创建一个临时变量来存储值之一,以便您可以在每个索引处交换值而不会丢失其中一个值。

2. 2。

siralanmish[i]=siyahi[j];

This was replaced with: 替换为:

siralanmish[j]=siyahi[j];

This allows you to directly copy the values from the same index from the source array to the target array. 这使您可以将相同索引中的值直接从源数组复制到目标数组。

3. 3。

siyahi[j]=siyahi.length+1;

This code will just fill up your array with the value of length+1 for your original array and you will lose your other values. 这段代码只会用原始数组的length + 1值填充您的数组,而您将丢失其他值。

Your code with the fixes are below: 带有修复程序的代码如下:

    public class ArraySorter
    {
    public static void main(String[] args)
    {
        int[] siyahi = RandomArrayCreator.create();
        int[] siralanmish = new int[siyahi.length];

        for (int i=0;i<siyahi.length;i++)
        {
            for (int j=0;j<siyahi.length;j++)
            {
                for (int k=j+1;k<siyahi.length;k++)
                {
                    if (siyahi[k]<siyahi[j]) 
                    {
                      int temp = siyahi[j];
                      siyahi[j] = siyahi[k];
                      siyahi[k] = temp;
                    }
                }
                siralanmish[j]=siyahi[j];
            }
            System.out.println(siralanmish[i]);
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM