简体   繁体   English

使用插入排序对对象的数组列表进行排序

[英]sorting an arraylist of objects using insertion sort

here's my code that uses selection. 这是我使用选择的代码。 i need to use a insertion and do not use temporary arrays or arraylist. 我需要使用插入,而不要使用临时数组或arraylist。 i need help of how to do a insertion sort. 我需要如何进行插入排序的帮助。

public static void sortStudents(ArrayList<Student> list)
 {//selection sort
  Student tempStudent;
  int count1;
  int count2;
  int largest;

  for (count1=0; count1<list.size()-1; count1++)
  {
   largest = 0;
   for (count2=largest+1; count2<list.size()-count1; count2++)
   {
    if ((list.get(largest)).compareTo(list.get(count2)) < 0)
    {
     largest = count2;
    }
   }
   tempStudent = list.get(list.size()-1-count1);
   list.set(list.size()-1-count1, list.get(largest));
   list.set(largest, tempStudent);
  }
 }
}

You don't define variables outside the loop if it is only used in the loop. 如果仅在循环中使用变量,则无需在循环外部定义变量。 Restricting the lifetime of your variables makes it more easy to reason about the code. 限制变量的生存期可以使代码推理更加容易。

public static void sortStudents (ArrayList<Student> list)
{
  int largest;

  for (int i=0; i < list.size () - 1; i++)
  {
    largest = 0;
    for (int j=largest + 1; j < list.size () - i; j++)
    {
      if ((list.get (largest)).compareTo (list.get (j)) < 0)
      {
         largest = j;
      }
    }
    Student tempStudent = list.get (list.size () - 1 - i);
    list.set (list.size () - 1 - i, list.get (largest));
    list.set (largest, tempStudent);
  }
}

A bit more indentation makes your code more readable. 缩进一点会使代码更具可读性。 Now what's your concrete error - doesn't it compile, throws it an exception or does it produce the wrong result? 现在,您的具体错误是什么-它不会编译,引发异常或产生错误的结果吗?

Here is something suspicious in the inner loop: 这是可疑的东西在内部循环:

    largest = 0;
    for (int j=largest + 1; j < list.size () - i; j++)

If you set largest to 0, then j will be initialized to 0 + 1 => 1 . 如果将maximum设置为0,则j将被初始化为0 + 1 => 1 I guess you had another intention. 我想你还有另一个打算。 Did you mean j = i + 1; 您的意思是j = i + 1; ?

Both selection sort and insertion sort work quite similarly, by having a "not yet sorted" part of the list, and an "already sorted" part. 选择排序和插入排序的工作方式都非常相似,因为它具有列表的“尚未排序”部分和“已排序”部分。 In the beginning, the first one is the whole list, and the second part an empty list at the start or end. 首先,第一个是整个列表,第二个部分是开头或结尾的空列表。 While sorting the "not yet sorted" part shrinks, while the "already sorted" part grows, by one element per iteration. 排序时,“尚未排序”的部分缩小,而“已经排序”的部分则增长,每次迭代增加一个元素。

The difference between selection sort and insertion sort is this: 选择排序和插入排序之间的区别是:

  • For selection sort, you search the minimum (or maximum) element of the "not yet sorted" part, remove it there and then add it to the end (or beginning) of the already sorted part. 对于选择排序,您搜索“尚未排序”部分的最小(或最大)元素,将其删除,然后将其添加到已排序部分的末尾(或开始)。
  • For insertion sort, you take the next element of the "not yet sorted" part of the list, find it's insertion point in the "already sorted" part and insert it there. 对于插入排序,请获取列表中“尚未排序”部分的下一个元素,在“已排序”部分中找到它的插入点,然后将其插入。

This should be enough to change your selection sort to insertion sort. 这足以将您的选择排序更改为插入排序。

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

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