【问题标题】:Insertion Sort using a String使用字符串的插入排序
【发布时间】:2015-02-12 19:54:33
【问题描述】:

我将 2 个主要类合二为一,以显示插入的数组字符串的未排序和排序值。给出的代码使用整数,我将其更改为字符串。我的插入排序()有问题。与行的比较导致它崩溃,我不知道为什么!

 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()

这是我的主要课程:

   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

这是更新后的 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

【问题讨论】:

  • 错误是什么? a[]nElems 的值是多少?
  • 错误是:在 java.lang.String.compareTo(Unknown Source)
  • a[ ] 的值是插入的 13 个单词,nElems 的值是 0-12
  • 整个错误是什么?
  • 这是我的主要课程:

标签: java arrays string insertion-sort


【解决方案1】:

您需要将 a[in] 与 a[in-1] 而不是 a[in+1] 进行比较。您的交换已经使用了正确的元素。使用原样的代码,a[in+1] 可以超过数组的上限,

【讨论】:

  • 它确实停止了崩溃,谢谢。但是,它仍然弄乱了排序。我会尽快发布更深入的结果。
【解决方案2】:

你真的很接近解决方案

您应该比较 tempa[in-1] 而不是 a[in]a[in+1]

您还需要检查inwhile loop 上是否始终为正,以避免IndexOutOfBoundsException

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

【讨论】:

    【解决方案3】:

    这是我现在可以使用的选择排序:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多