package 快速排序算法;
public class ZiFuChuan {
static final int N = 5;
static void quick(String[] arr, int left, int right) {
String f, t;
int rtemp, ltemp;
ltemp = left;
rtemp = right;
f = arr[(left + right) / 2];// 分界值
while (ltemp < rtemp) {
while (arr[ltemp].compareTo(f) < 0) {
++ltemp;
}
while (arr[rtemp].compareTo(f) > 0) {
--rtemp;
}
if (ltemp <= rtemp) {
t = arr[ltemp];
arr[ltemp] = arr[rtemp];
arr[rtemp] = t;
--rtemp;
++ltemp;
}
}
if (ltemp == rtemp) {
ltemp++;
}
if (left < rtemp) {
quick(arr, left, ltemp - 1);// 递归调用
}
if (ltemp < right) {
quick(arr, ltemp + 1, right);// 递归调用
}
}
public static void main(String[] args) {
ZiFuChuan zz = new ZiFuChuan();
String[] arr = new String[] { "One", "World", "Dream", "Beijing", "Olympic" };// 声明并且初始化
int i;
System.out.print("排序前:\n");
for (i = 0; i < N; i++) {
System.out.println(arr[i]);
}
quick(arr, 0, N - 1);// 排序
System.out.print("排序后:\n");
for (i = 0; i < N; i++) {
System.out.println(arr[i]);
}
}
}
算法-->字符串数组(快 排)
最新推荐文章于 2022-03-23 12:09:14 发布