2013-12-13 148 views
0

我正在使用compareTo方法和选择排序按字母顺序排序字符串数组的程序。按选择排序按字母顺序排序字符串数组?


林具有低于我的minimumPosition方法中的一个问题。该方法旨在获取数组尾部区域中的最小元素,以便选择排序程序可以方便地对列表进行排序。

我的问题是,当我对列表进行排序并通过测试器进行打印时,它会按照字母顺序将其打印出来,并在前面进行减值处理。例如(C,Z,X,Y ...,B,A)反对(A,B,C ... Y,X,Z)

/** 
    SelectionSorter class sorts an array of Strings alphabetically. 
    It uses the selection sort algorithm. 

*/ 
public class SelectionSorter 
{ 
    private String[] a; 
    /** 
     Constructs the selection sorter 
     @param anArray the array to sort 
    */ 
    public SelectionSorter4 (String[] anArray) 
    { 
     a = anArray; 
    } 


    /** 
     Sorts the array managed by this selection sorter 
    */ 
    public void sort() 
    { 
     for (int i = 0 ; i < a.length - 1 ; i++) 
     { 
      int minPos = minimumPosition (i); 
      swap (minPos, i); 
     } 
    } 


    /** 
     Finds the smallest element in a tail region of the array. 
     The elements are String objects in this case, and the 
     comparison is based on the compareTo method of String. 
     @param from the first position of the tail region 
     @return the position of the smallest element in tail region 
    */ 
    private int minimumPosition (int from) 
{ 

    String holder = a [from]; 
    int position = from; 
    for (int i = from ; i < a.length ; i++) 
    { 
     if (a [i].compareTo (holder) > 0) 
     { 
      holder = a [i]; 
      position = i; 
     } 

    } 
    return position;     
} 

     /** 
      Swaps two entries of the array 
      @param i the first position to swap 
      @param j the second position to swap 
     */ 
     private void swap (int i, int j) 
     { 

      String temp = a [i]; 
      a [i] = a [j]; 
      a [j] = temp; 

     } 
    } 

Tester类:相关性,但没有任何问题这里。

/** 
     Tests the SelectionSorter4 class which sorts an array of Strings 
     alphabetically. 
    */ 
    import java.util.* ; 

    public class SelectionSorterTester 
    { 
     public static void main(String[] args) 
     { 
      String[] a = randomStringArray(40, 6) ; 
      SelectionSorter sorter = new SelectionSorter(a) ; 

      System.out.println(toString(a)) ; 
      sorter.sort() ; 
      System.out.println("----------Sorted:") ; 
      System.out.println(toString(a)) ; 
      System.out.println("--------------------------------") ; 
     } 
     /** 
      Returns a string representation of the array of Strings 
      @param array the array to make a string from 
      @return a string like [a1, a2, ..., a_n] 
     */ 
     public static String toString(String[] array) 
     { 
      String result = "[" ; 
      for (int i = 0 ; i < array.length - 1; i++) { 
       result += array[i] + ", " ; 
      } 
      result += array[array.length - 1] + "]" ; 
      return result ; 
     } 
     /** 
      Creates an array filled with random Strings. 
      @param length the length of the array 
      @param n the number of possible letters in a string 
      @return an array filled with length random values 
     */ 
     public static String[] randomStringArray(int length, int n) 
     { 
      final int LETTERS = 26 ; 
      String[] a = new String[length] ; 
      Random random = new Random(53) ; 
      for (int i = 0 ; i < length ; i++) { 
       String temp = "" ; 
       int wordLength = 1 + random.nextInt(n) ; 
       for (int j = 0 ; j < wordLength ; j++) { 
        char ch = (char)('a' + random.nextInt(LETTERS)) ; 
        temp += ch ; 
       } 
       a[i] = temp ; 
      } 
      return a ; 
     } 
    } 

我认为问题在于minimumPosition方法,但它对我来说看起来是正确的。

+0

请看看[如何调试Java代码(http://www.vogella.com/articles/EclipseDebugging/article.html) – gerrytan

+0

乍一看,不应该't'position'可能被设置为'from'而不是0? – splrs

+0

我的不好。谢谢分割器。尽管数组按字母顺序打印,但仍然存在问题。我想调试将是一件好事 – BDillan

回答

3

如果你想方兴未艾秩序,

变化

if (a [i].compareTo (holder) > 0) 

if (a [i].compareTo (holder) < 0) 

比较与指定对象这个对象的顺序。返回 负整数,零或正整数,因为此对象比指定对象的 小,等于或大于此值。

了解更多:Comparable#compareTo(..)

+0

这不是不稳定的排序吗? –

+0

@ElliottFrisch unstable表示不稳定?AFAIK选择排序不稳定 – nachokk

+0

它[可以](http://en.wikipedia.org/wiki/Selection_sort)是,如果最小值插入到第一个位置(而不是交换) - 说使用LinkedList。 –