1

I combined 2 main classes into one to display the unsorted and sorted values of the inserted array Strings. The code given was using integers and I changed it to do Strings instead. I am having a problem with my insertionSort(). The compare to line is causing it to crash and I cannot figure out why!

 public void insertionSort()
  {
  int in, out;

  for(out=1; out<nElems; out++)     // out is dividing line
     {
     String temp = a[out];            // remove marked item
     in = out;                      // start shifts at out
     System.out.println(a[in]);
   --->while(a[in].compareTo(a[in+1])>0 ) // until one is smaller,
        {
        a[in] = a[in-1];            // shift item to right
        --in;                       // go left one position
        }
     a[in] = temp;                  // insert marked item
     }  // end for
  }  // end insertionSort()

Here is my main class:

   class SortApp
  {
  public static void main(String[] args)
    {
    int maxSize = 100;            // array size

  ArraySel arr;//reference to ray1--> selection sort
  ArrayIns arr2;// reference to array2--> insertion sort
  arr = new ArraySel(maxSize);  // create the array
  arr2 = new ArrayIns(maxSize);

  arr.insert("hello"); //insert words into the array
    arr.insert("this");
    arr.insert("is");
    arr.insert("a");
    arr.insert("random");
    arr.insert("weird ");
    arr.insert("sentence");
    arr.insert("that");
    arr.insert("does");
    arr.insert("not");
    arr.insert("make");
    arr.insert("any");
    arr.insert("sense");

    arr2.insert("hello");
    arr2.insert("this");
    arr2.insert("is");
    arr2.insert("a");
    arr2.insert("random");
    arr2.insert("weird ");
    arr2.insert("sentence");
    arr2.insert("that");
    arr2.insert("does");
    arr2.insert("not");
    arr2.insert("make");
    arr2.insert("any");
    arr2.insert("sense");

    arr.display();                // display items
  arr2.display();

  arr.selectionSort();//sort the 2 arrays
  arr2.insertionSort();

  arr.display();                // display them again
  arr2.display();
  }  // end main()
   }  // end class SelectSortApp

And here is the updated selectionSort class

public void selectionSort()
  {
  int out, in, min;

  for(out=0; out<nElems-1; out++)   // outer loop
     {
     min = out;                     // minimum
     for(in=out+1; in<nElems; in++) // inner loop

         if((a[in].compareTo(a[in-1])>0 ))        // if min greater,
            min = in;               // we have a new min
     swap(out, min);                // swap them
6
  • And what is the error? What are the values of a[] and nElems? Commented Feb 12, 2015 at 19:57
  • the error is: at java.lang.String.compareTo(Unknown Source) Commented Feb 12, 2015 at 20:06
  • The values for a[ ] are 13 words that were inserted and the values for nElems would be 0-12 Commented Feb 12, 2015 at 20:07
  • And what is the whole error? Commented Feb 12, 2015 at 20:24
  • here is my main class: Commented Feb 12, 2015 at 20:32

3 Answers 3

1

You need to compare a[in] with a[in-1] not a[in+1]. Your swap already uses the correct elements. With the code as is, a[in+1] can exceed the upper bound of your array,

Sign up to request clarification or add additional context in comments.

1 Comment

it definitely stopped crashing, thank you. However, it is still messing up the sort. I will post a more in depth result shortly.
0

You are really close to the solution

You should compare temp vs a[in-1] instead of a[in] vs a[in+1]

Also you need to check the in is always positive on the while loop in order to avoid IndexOutOfBoundsException

while(in > 0 && temp.compareTo(a[in-1]) < 0) 
{

Comments

0

Here is my selection sort which works now:

public void selectionSort() {
   int out, in, min;
    for(out=0; out<nElems-1; out++)   // outer loop
    {
       min = out;                     // minimum
       for(in=out+1; in<nElems; in++) // inner loop
           if((a[in].compareTo(a[min])<0 ))        // if min greater,
               min = in;               // we have a new min
       swap(out, min);                // swap them
     }  // end for(out)
  }  // end selectionSort()

Comments

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.