[英]JAVA // How do i make it case-insensitive?
import java.util.Random;
public class MergeSortEx {
public static void mergeSort(char[] array) {
sortArray(array, 0, array.length);
}
private static void sortArray(char[] array, int start, int end) {
if (end-start <2)
return;
int mid = (start + end) / 2;
sortArray(array, 0, mid);
sortArray(array, mid, end);
mergeArray(array, start, mid, end);
}
private static void mergeArray(char[] array, int start, int mid, int end) {
char[] temp = new char[end - start];
int t = 0, s = start, m = mid;
while (s < mid && m < end) {
if (array[s] < array[m])
temp[t++] = array[s++];
else
temp[t++] = array[m++];
}
while (s < mid) {
temp[t++] = array[s++];
}
while (m < end) {
temp[t++] = array[m++];
}
for(int i=start;i<end;i++) {
array[i]=temp[i-start];
}
}
public static void main(String[] args) {
char[] randomString = new char[20];
Random rnd = new Random();
for (int i = 0; i < 20; i++) {
if (i < 10)
randomString[i] = (char) (rnd.nextInt(6) + 'A');
else
randomString[i] = (char) (rnd.nextInt(6) + 'a');
}
System.out.println(randomString.length);
for (int i = 0; i < 20; i++)
System.out.print(randomString[i] + " ");
mergeSort(randomString);
System.out.println();
for (int i = 0; i < 20; i++)
System.out.print(randomString[i] + " ");
}
}
I used the translator.我用了翻译器。 It's a university algorithm assignment, Merge sort implemented successfully.
这是一个大学算法作业,合并排序成功实现。 Now, capital letters come out first, and lowercase letters come out.
现在,大写字母先出来,小写字母出来。 Can make the code case-insensitive?
可以使代码不区分大小写吗? I want the results to be like this.
我希望结果是这样的。 ex) a AAB b C c c D d... plz help.
例如)a AAB b C c c D d ... 请帮助。
Instead of comparing using if (array[s] < array[m])
directly, convert the characters to uppercase before comparing, similar to what String.compareToIgnoreCase(...)
does:不是直接使用
if (array[s] < array[m])
进行比较,而是在比较之前将字符转换为大写,类似于String.compareToIgnoreCase(...)
所做的:
if (Character.toUpperCase(array[s]) < Character.toUpperCase(array[m]))
That is for sorting individual characters.那是为了对单个字符进行排序。 For sorting
String
values, there are two ways to make a case-insensitive sort:对于对
String
值进行排序,有两种方法可以进行不区分大小写的排序:
Use the predefined String.CASE_INSENSITIVE_ORDER
Comparator
.使用预定义的
String.CASE_INSENSITIVE_ORDER
Comparator
器。
stringList.sort(String.CASE_INSENSITIVE_ORDER);
Collator collator = Collator.getInstance(Locale.US); stringList.sort(collator);
That will sort localized alphabets correctly, eg if you specified Locale.GERMANY
, it would sort upper- and lower-case letters together, but will eg also sort Ð
between D
and E
, and sort ß
same as S
.这将正确地对本地化字母进行排序,例如,如果您指定
Locale.GERMANY
,它会将大写和小写字母排序在一起,但也会对D
和E
之间的Ð
进行排序,并将ß
排序为与S
相同。
Just replace:只需更换:
while (s < mid && m < end) {
if (arr[s] < arr[m])
temp[t++] = arr[s++];
else
temp[t++] = arr[m++];
}
with和
while (s < mid && m < end) {
if (Character.toLowerCase(arr[s]) < Character.toLowerCase(arr[m]))
temp[t++] = arr[s++];
else
temp[t++] = arr[m++];
}
If you want small letters come first like aAbbbBBBcCCCD
then try following.如果您想要小写字母,例如
aAbbbBBBcCCCD
,请尝试以下操作。
Instead of:代替:
array[s] < array[m]
Use:利用:
private static boolean charCompare(char a, char b) {
char ua = Character.toUpperCase(a);
char ub = Character.toUpperCase(b);
if(ua == ub) {
return a > b;
} else {
return ua < ub;
}
}
Output: Output:
20
F E E A D A B C A E a e c f d f c a f e
a a A A A B c c C d D e e E E E f f f F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.