-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathQuick3string.java
73 lines (69 loc) · 1.11 KB
/
Quick3string.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//ÈýÏò×Ö·û´®¿ìËÙÅÅÐò
public class Quick3string {
public static void sort (String[] a)
{
sort(a, 0, a.length - 1, 0);
}
private static void sort(String[] a, int lo, int hi, int d)
{
if(hi <= lo)
{
return;
}
int lt = lo, gt = hi;
int v = charAt(a[lo], d);
int i = lo + 1;
while(i <= gt)
{
int t = charAt(a[i], d);
if(t < v)
{
swap(a, lt, i);
lt++;
i++;
}
else if(t > v)
{
swap(a, gt, i);
gt--;
}
else
{
i++;
}
}
sort(a, lo, lt - 1, d);
if(v >= 0)
{
sort(a, lt, gt, d + 1);
}
sort(a, gt + 1, hi, d);
}
private static int charAt(String s, int d)
{
if(d < s.length())
{
return s.charAt(d);
}
else
{
return -1;
}
}
private static void swap(String[] a, int i, int j)
{
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args)
{
String[] a= {"she", "sells", "seashells", "by", "the", "sea", "shore", "the", "shells", "she",
"sells", "are", "surely", "seashells"};
Quick3string.sort(a);
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
}