2013-12-13 147 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。 –