Java 通过选择排序按字母顺序对字符串数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20557900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting a String Array alphabetically by Selection Sort?
提问by Sad CRUD Developer
Im working on a program that alphabetically sorts a string array using compareTo method and selection sort.
我正在开发一个程序,该程序使用 compareTo 方法和选择排序按字母顺序对字符串数组进行排序。
Im having an issue within my minimumPositionmethod below. The method is designed to take the smallest element in a tail region of the array so that the selection sort program can conveniently sort the list.
我在下面的minimumPosition方法中遇到了问题。该方法设计为取数组尾部区域中的最小元素,以便选择排序程序可以方便地对列表进行排序。
My issue is that when I sort the list and print it via the tester it prints it out reverse alphabetically with a decrepency in-front. e.g. (c ,z,x,y...,b,a) opposed to(a,b,c.. y,x,z)
我的问题是,当我对列表进行排序并通过测试仪打印它时,它会按字母顺序反向打印出来,前面有一个递减。例如 (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 class: Relevant but there are no issues here.
测试员类:相关但这里没有问题。
/**
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 ;
}
}
I think the issue lies within the minimumPosition method but it looks correct to me.
我认为问题出在 minimumPosition 方法中,但在我看来是正确的。
采纳答案by nachokk
If you want ascendant order,
如果你想要升序,
Change
改变
if (a [i].compareTo (holder) > 0)
to
到
if (a [i].compareTo (holder) < 0)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
将此对象与指定的对象进行比较以进行排序。当此对象小于、等于或大于指定对象时,返回一个负整数、零或正整数。
Read more: Comparable#compareTo(..)