Generic Mergesort在整数上工作,但没有用字符串完成?

时间:2011-04-29 21:29:30

标签: java sorting merge

无论出于何种原因,尽管它是完全通用的,与整数工作时,它与串测试它似乎跳过合并的最后一次迭代。我已经把我的代码扫了几个小时了,我不明白为什么它不能正常工作,所以任何人都有的见解会受到好评!

  

import java.util.Iterator;进口   java.util.LinkedList中;

     

/ ** * * @author paul * / public   class MergeSort> {

LinkedList<T> theList;

MergeSort(LinkedList<T> toBeSorted) {
    theList = toBeSorted;
}

public LinkedList<T> sort() {
    return trueSort(theList);
}

private LinkedList<T> trueSort(LinkedList<T> sorting) {
    if (sorting.size() <= 1) {
        return sorting;
    }
    LinkedList<T> left, right, sorted;
    left = new LinkedList<T>();
    right = new LinkedList<T>();
    int middle = sorting.size() / 2;
    Iterator<T> sojourner = sorting.iterator();
    for (int i = 0; sojourner.hasNext(); i++) {
        if (i < middle) {
            left.add(sojourner.next());
        } else {
            right.add(sojourner.next());
        }
    }
    return trueMerge(trueSort(left),
     

trueSort(右));       }

private LinkedList<T> trueMerge(LinkedList<T> left,
     

LinkedList right){           LinkedList result = new LinkedList();           while(left.size()&gt; 0 || right.size()&gt; 0){               if(left.size()&gt; 0&amp;&amp; right.size()&gt; 0){                   if(left.getFirst()。compareTo(right.getFirst())   &LT; 0){                       result.add(left.pop());                   } else {                       result.add(right.pop());                   }               } else if(left.size()&gt; 0){                   result.add(left.pop());               } else {                   result.add(right.pop());               }           }           返回结果;       }}

这是我的主要java文件

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

/**
*
* @author paul
*/
public class Main {

    public static Random Rand;

    public static int randomNumber(int min, int max) {
  return min + (int) (Rand.nextDouble() * ((max - min) +
     

1));           }

    public static <T> String getString(LinkedList<T> linkInt) {
  String s = "";
  Iterator<T> interLink = linkInt.iterator();
  for (int i = 0; interLink.hasNext(); i++) {
    s = s + interLink.next().toString();
    if (interLink.hasNext()) {
    s = s + ", ";
    }
    if ((i + 1) % 10 == 0) {
    s = s + "\n";
    }
  }
  return s;
    }

    /**
  * @param args the command line arguments
  */
    public static void main(String[] args) {
  Rand = new Random();

  LinkedList<Integer> numbers = new LinkedList<Integer>();
  for(int i = 0; i< 100;i++){
    numbers.add(randomNumber(1,1000));
  }
  System.out.println(getString(numbers));
  MergeSort m = new MergeSort(numbers); //change this
     

后来纯粹是静态的         numbers = m.sort();         的System.out.println(的getString(数字));

  LinkedList<String> words = new LinkedList<String>();
  words.add("Hello");
  words.add("MY");
  words.add("name");
  words.add("Is");
  words.add("Barthoal");
  words.add("I");
  words.add("Enjoy");
  words.add("long");
  words.add("beach");
  words.add("walks");
  words.add("would");
  words.add("you");
  words.add("like");
  words.add("to");
  words.add("come");
  words.add("Join");
  words.add("me");
  words.add("in");
  words.add("my");
  words.add("StarDestroyer-MobileHome?
     

(TM)“);         的System.out.println(的getString(字));         MergeSort mm = new MergeSort(单词);         words = mm.sort();         的System.out.println(的getString(字));           }       }

输出:

  

304,842,342,794,574,99,250,885,   408,387,899,73,391,883,771,   848,968,504,129,370,994,897,   649,345,983,326,688,547,541,   567,777,987,201,326,298,959,   166,962,864,797,512,505,609,   208,21,43,458,442,138,570,455,   442,516,294,406,310,215,212,   397,98,938,496,263,973,571,   861,687,276,927,608,421,831,   820,510,68,172,504,8,976,992,   68,497,33,233,607,587,611,695,   834,338,448,978,359,413,1,819,   18,977,693,649

     

1,8,18,21,33,43,68,68,73,98,   99,129,138,166,172,201,208,212,   215,233,250,263,276,294,298,   304,310,326,326,338,342,345,   359,370,387,391,397,406,408,   413,421,442,442,448,455,458,   496,497,504,504,505,510,512,   516,541,547,567,570,571,574,   587,607,608,609,611,649,649,   687,688,693,695,771,777,794,   797,819,820,831,834,842,848,   861,864,883,885,897,899,927,   938,959,962,968,973,976,977,   978,983,987,992,994

     

您好,我的名字,是,Barthoal,我,   享受,长,海滩,散步,会,   你,喜欢,来,加入,我,我,我的,   灭星者,MobileHome? (TM)

     

Barthoal,享受,你好,我,是,加入,   我的StarDestroyer-MobileHome? (TM值),   海滩,来,在,像,长,我,我的,   name,to,walking,would,you

如您所见,数字已完全排序。字符串似乎错过了最后一次合并迭代。究竟出了什么问题?

2 个答案:

答案 0 :(得分:4)

字符串比较区分大小写。也就是说,所有大写字符串都将放在小写字符串之前。你的合并排序算法应该允许你自己的比较器,你的用例可以是String.CASE_INSENSITIVE_ORDER。

答案 1 :(得分:1)

您的代码很好,并且排序正确。 您需要了解大写字母与小写字母不同。当比较“我的”机智“海滩”时,你会得到“我的”应该在“海滩”之前(因为大写字母在小写字母之前)。

这就是为什么在你的结果中,所有大写字母都将所有以大写字母开头的单词放在开头。

您可能希望使用compareToIgnoreCase()方法(在字符串上定义)来比较字符串,而忽略字母的大小写。

在此处查看此方法的详细信息:http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String

(请重新格式化您的代码)