【问题标题】:Insertion sorting an double array list, first item doesn't get sorted插入排序双数组列表,第一项未排序
【发布时间】:2014-01-02 02:42:33
【问题描述】:

排序类中的arraylist插入排序:

public void insertionSort(ArrayList <Double>list){
    double temp;
    int  previousIndex;

    for(int index = 1; index < list.size(); index++){
        temp = list.get(index);
        previousIndex = index - 1;
        while((list.get(previousIndex) > temp) && (previousIndex > 0)){
            list.set((previousIndex+1), list.get(previousIndex));
            previousIndex-=1;
        }
        if(list.get(previousIndex) > temp){
            list.set((previousIndex+1), list.get(previousIndex));
           list.set((previousIndex+1), temp);
        }else{
           list.set((previousIndex+1), temp);
        }
    } 
}

主类:

package sorts;

import java.util.*;

public class TestSorts {


public static void displayArrayList(ArrayList <Double> list){
    for (int i=0; i<list.size();i++){
        System.out.print(list.get(i)+" \n");
    }
    System.out.println("\n");       
}

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int numItems,searchNum,location;
    Sorts sort=new Sorts();

    Scanner in=new Scanner(System.in);
    int numItems,searchNum,location;
    Sorts sort=new Sorts();
    System.out.print("Enter the number of elements: ");
    numItems=in.nextInt();

    ArrayList <Double> list=new ArrayList<>();


    for (int i=0;i<numItems;i++){
        list.add((int)100*Math.random());
    }


    System.out.println("Unsorted: ");
    displayArrayList(list);
    sort.insertionSort(list);
    System.out.println("Sorted: ");
    displayArrayList(list);               

  }  
}

基本上现在发生的事情是数字将很好地生成,并且排序很好,除了排序时,排序列表中的第一个项目将不正确。如:

未分类:

58.754608713273925 

77.15776272269233 

61.752499151303795 

1.9942069463339207 

55.30460705281677 

78.06371704304172

排序:

58.754608713273925

1.9942069463339207 

55.30460705281677 

61.752499151303795 

77.15776272269233 

78.06371704304172 

所以看起来一切都很好,但是列表中的第一个随机数永远不会被排序,并最终停留在与以前完全相同的位置。

【问题讨论】:

  • 好吧,既然您明确地从索引 1 开始插入,那将会发生 ;)

标签: java sorting arraylist


【解决方案1】:

index = 1 在您的排序例程中应该是 index = 0。目前,您正在跳过 List 中的第一个项目,该项目存储在索引零处。

【讨论】:

  • 那有关系吗?因为我的 previousindex=index-1 仍然没有访问 0 的索引吗?
【解决方案2】:

试试这个:

public static void insertionSort(ArrayList<Double> list) {
        double temp;
        int previousIndex;

        for (int index = 1; index < list.size(); index++) {
            temp = list.get(index);
            previousIndex = index - 1;
            while (previousIndex >= 0 && (list.get(previousIndex) > temp)) {
                list.set((previousIndex + 1), list.get(previousIndex));
                previousIndex -= 1;
            }
            if (previousIndex >= 0 && list.get(previousIndex) > temp) {
                list.set((previousIndex + 1), list.get(previousIndex));
                list.set((previousIndex + 1), temp);
            } else {
                list.set((previousIndex + 1), temp);
            }
        }
    }

【讨论】:

  • 你能解释一下为什么在将其更改为 >=0 而不是 =0 后它会起作用吗?我从最初为 int 数组编写的代码更改了此代码,=0 适用于数组但不适用于 arraylist,我不确定为什么会这样
  • 因为你想在while循环中改变位置,对吧?但是在你对代码进行排序时,while循环的条件是previousIndex > 0,所以当previousIndex为0(第一个随机数)时,while循环中的代码不能工作,我认为这是问题所在。
猜你喜欢
  • 1970-01-01
  • 2014-06-04
  • 2018-11-04
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
相关资源
最近更新 更多