7

I have a homework assignment to sort an array in ascending order. Obviously, this is to be done manually without using any kind of sort() function.

I figured to do it, I would need two for loops: the first one will loop through the existing array and create a temporary value with the value and index of the array. The second loop will compare the temporary values to the existing values and sort them. I keep trying to write the code, but I just can’t seem to get it right. Here is the latest method I came up with:

public int[] sortArray (int[] inArray)
{
    //Construct the array we're using here
    int[] newArray = inArray;

    for(int x = 0; x < a.length; x++) //a.length = # of indices in the array
    {
        int tempValue = a[x];
        int tempIndex = x;

        for(int y = 0; y < a.length; y++)
        {
            if(tempValue < a[y])
            {
                newArray[x] = tempValue;
            }
        }
    }

    return newArray;
}

I’m pretty sure this is incorrect, but if someone could push me in the right direction it would be very appreciated!

6
  • 1
    It might be worth you firstly looking at some pseudo code for different sorting algorithms: maven.smith.edu/~thiebaut/java/sort Commented Mar 29, 2012 at 14:45
  • Are you supposed to use a certain sort algorithm? Commented Mar 29, 2012 at 14:46
  • Unless you were explicitly asked to think up your sorting algorithm, I recommend finding a simple one and implementing it in your code. And instead of being "pretty sure" that your code is incorrect, just test it and find out. Commented Mar 29, 2012 at 14:46
  • Yes, the algorithm must be my own. And by "pretty sure it doesn't work", I mean it doesn't work, but I'm hoping to only have to slightly modify the code to get it working, rather then starting from scratch again. Commented Mar 29, 2012 at 14:47
  • You use variable a which is nowhere to be seen. Use inArray instead of it. The right way to create a new array is int[] newArray = new int[inArray.length];. Commented Mar 29, 2012 at 14:50

6 Answers 6

8

You have a nearly OK version of the Selection Sorter. You need to start your y at x+1, not at 0. Otherwise you're re-scanning the sorted portion of the array. You should also note that selection sort is an in-place algorithm; if you are looking to make a copy of the array, you should use Arrays.copy method, otherwise int[] newArray = inArray; is creating an alias, not a copy. Finally, the if statement in the nested loop should swap a[x] and a[y], not simply put tempValue in:

if(newArray[x] < newArray [y]) {
    int tempValue = newArray[y];
    newArray[y] = newArray[x];
    newArray[x] = tempValue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could you elaborate a little on the swap part?
Thanks for the tip about Arrays.copy! I kinda have been overlooking that, but it turns out that was my problem :)
2

Instead of trying to invent your own sorting algorithm, I'd urge you to study what already exists. There's a ton of prior art on this.

Take a look at the Wikipedia article: Sorting algorithm.

Bubble sort is very easy to implement, but has quadratic complexity (the same as your current attempt).

Quicksort isn't too hard to implement either, and has better average complexity.

Comments

2

The sorting you are trying to achieve is called Bubble sort - the wikipedia entry is pretty good you should read it. Though, it's never really used because there is better alternative - Insertion sort (An example is Timsort in Python which is a hybrid of Merge sort and Insertion sort). These two are the basic algorithms that fits your idea with two loops, hence O(n2) complexity.

You should also consider different algorithms for your assignment or, at least, be aware of:

Hope it helps.

Comments

2
int minval = input[0];
int temp=0;


for(int i = 0; i< input.length; i++)
{ 
    for(int j = 0; j< input.length-1; j++)
    {
        if(input[j+1]<input[j])
        {
            temp=input[j+1];
            input[j+1]=input[j];
            input[j]=temp;  
        }
    }
}

Comments

0
int arr[] = new int[]{10, 20, 5, 6, 30, 1, 2};
    boolean bool = true;
    int t = 0;
    while (bool) {
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                int c = arr[i];

                arr[i] = arr[i + 1];
                arr[i + 1] = c;
                t++;
            }
        }
        if (t == 0) {
            bool = false;
        }
        t = 0;
    }

    for (int y : arr) {
        System.out.println(y);
    }

Comments

0
int[] number = { 1,2,1,3,5,4 };
    int temp;
     for (int i = 0; i < number.length; i++)
        {
            for (int j = i + 1; j < number.length; j++)
            {
                if (number[i] > number[j])
                {
                    temp =  number[i];
                    number[i] = number[j];
                    number[j] = temp;
                }
            }
        }

        for (int i = 0; i <number.length; ++i)
            System.out.println(number[i]);
    }

1 Comment

Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term educational value. Please edit your answer to add explanation, including what limitations and assumptions apply.

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.