import java.util.ArrayList;
import java.util.Arrays;

public class SearcherAndSorter 
{
    private ArrayList<Integer> values;
    private int counter;

public SearcherAndSorter()
{
    values = new ArrayList<Integer>();
}

private void insert(int value, int[] sorted, int count)
{   
    /*for(int j = 0; j < values.size(); j++)
    {
        count++;
        if(value < values.get(j))
        {
            values.add(j, value);
            return;
        }
    }
    values.add(value);*/

    int lastValue = 0;
    sorted[count] = value;

    for(int i = count-1; i >= 0; i--)
    {
        counter++;
        if(value < sorted[i])
        {   
            lastValue = sorted[i];
            sorted[i+1] = lastValue;
            sorted[i] = value;
        }
    }

    System.out.println("Inserted " + value + " size " + (count+1));
    for(int j = 0; j < count + 1; j++)
    {
        System.out.println(sorted[j]);
    }
    System.out.println("Done!");
}

/*
 * Insertion sort
 */
public int[] sort(int[] val)
{
    int[] values = new int[val.length];

    for(int i = 0; i < val.length; i++)
    {
        insert(val[i], values, i);
    }
    System.out.println(counter);
    return values;
}

/*
 * Selection sort
 */
public int[] sortTwo(int[] val)
{
    int count = 0;
    int[] value = val;

    for(int i = 0; i < value.length - 1; i++)
    {
        count++;
        int minIndex = i;
        for(int j = i + 1; j < value.length; j++)
        {
            if(value[minIndex] > value[j])
            {
                minIndex = j;
            }
        }

        if(minIndex != i)
        {
            int lastValue = value[i];
            value[i] = value[minIndex];
            value[minIndex] = lastValue;
        }
    }
    //System.out.println("Number of times the for loops run for: " + count);
    return value;
}

public int[] sortThree(int[] val)
{
    if(val.length <= 1)
    {
        return val;
    }
    int[] valOne = new int[val.length/2];
    int[] valTwo = new int[val.length - valOne.length];
    for(int i = 0; i < valOne.length; i++)
    {
        valOne[i] = val[i];
    }
    for(int n = valOne.length; n < valTwo.length; n++)
    {
        valTwo[n - valOne.length] = val[n];
    }

    sortThree(valOne);
    sortThree(valTwo);

    while()
    {
        int indexOne = 0;
        int indexTwo = 0;
        int arrayIndex = 0;

        if(valOne[indexOne] < valTwo[indexTwo])
        {

        }
    }

    return val;
}

@Override
public String toString()
{
    String value = "";
    for(int i = 0; i < values.size(); i++)
    {
        value = value + values.get(i) + " ";
    }
    //System.out.println(count);
    return value;
}

}

I haven't finished the sortThree. it is a merge sort. I am not sure how to do it so I need a little starting help. And no there shouldn't be changes to anything else other than the while loop in the sort three. thanks for the help

Can you explain what the problem is?
Do you have the algorithm for the sort and are having trouble writing the code for the algorithm
or do you need an algorithm?

I am having trouble writing the code for the while loop in the sortThree method. I am not sure what should I compare in the if statement in the while loop. I want to know what I am supposed to write in that loop.

I don't know without seeing the algorithm/design. Do you have the algorithm for the sort? Does it list the steps a program should take to do the sort? Given that the code could be written. You need to know what the code is supposed to do before you try to write it.

If I pass in an array of numbers it should sort it from low to high.

Yes, that is what a sort does. The question is: what does the code need to do to make that happen?
What technique is the sortThree() method supposed to use?

it is a merge sort

check this page out. it has pictures that helps understand it faster:

www.roseindia.net/java/beginners/arrayexamples/mergeSort.shtml

i don't know how to write the while statment thought. and also there shouldn't be any changes other than in the while loop. or adding after the while loop

What does the algorithm say about how to code the while loop? You need to digest what is shown on that site you posted the link to so you can specify the steps your code is supposed to take.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.