简体   繁体   English

使用字符串进行插入排序

[英]Insertion Sort using a String

I combined 2 main classes into one to display the unsorted and sorted values of the inserted array Strings. 我将2个主要类组合为一个,以显示插入的数组Strings的未排序和排序值。 The code given was using integers and I changed it to do Strings instead. 给定的代码使用整数,我改为将其改为字符串。 I am having a problem with my insertionSort(). 我的insertSort()有问题。 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 这是更新的selectionSort类

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

You need to compare a[in] with a[in-1] not a[in+1]. 您需要将a [in]与a [in-1]而不是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, 照原样编写代码,a [in + 1]可以超出数组的上限,

You are really close to the solution 您真的很接近解决方案

You should compare temp vs a[in-1] instead of a[in] vs a[in+1] 您应该比较temp vs a[in-1]而不是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 loop上检查in始终为正,以避免IndexOutOfBoundsException

while(in > 0 && temp.compareTo(a[in-1]) < 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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