0

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

}

4
  • This siyahi[j]=siyahi.length+1; looks suspiouis Commented Mar 25, 2014 at 1:03
  • Well... what sorting method are you trying to implement? Commented Mar 25, 2014 at 1:14
  • 2
    What type of sort uses three nested loops? Commented Mar 25, 2014 at 1:20
  • What are these variable names? Commented Mar 25, 2014 at 1:29

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thanks a lot, it works. But when I try this code, I get an error like "reached end of file while parsing", any idea why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.