简体   繁体   中英

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.

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.

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. 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.

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.

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.

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]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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