---- three sorting (Quick3string) string to the string of fast

Previous describes two classical string sorting method (LSD MSD):  https://www.cnblogs.com/Unicron/p/11531111.html

 

Three quick sort strings we just need to improve it quick sort code can achieve it, it is particularly suitable for longer string containing the common prefix and does not require any additional space. The code is relatively simple, the main idea is to understand it.

 

First, the core idea

Thought the use of the partition, the middle of the string through the string each time the array is divided into three groups.

Panel then recursively performs the same processing until the end of the string come, the final form of an array of strings ordered nature.

 

Second, the specific practices:

1, (by default this article the first selected character string) as an intermediate character with a character, it is moved to the end than the large string array smaller than it is moved in front of it.

Such play again after traversing the formation of three groups, which are strings beginning with the letter, character less than the intermediate, the intermediate character is equal to, larger than the intermediate character.

(Note that, here by moving the string EXCH () method, the position of a character string in the string array exchanged directly, without resorting to additional arrays) 

2, three of the string array classification step 1 one by one until all the characters in the string traversal. Finally, the formation of a string of natural order.

 

Third, the examples demonstrate

Follow the above steps, we have to come to a complete process instance:

 

 

Fourth, compared with LSD, MSD's

LSD has no notion of grouping, simply right-to-left sort each character.

MSD joined the grouping concept, but also for each packet from start to finish, as every sort must create a secondary array will use a lot of space in the longer array.

quick3string with two different applications that no additional space, and an array of strings for a large number of the same prefix, it can also have a good handle. 

 

Five complete code

 1 public class Quick3string {
 2     private static int charAt(String s,int d){
 3         if(d<s.length()){
 4             return  s.charAt(d);
 5         }else{
 6             return -1;
 7         }
 8     }
 9 
10     private  static void exch(String [] s,int a,int b){
11         String temp=s[a];
12         s[a]=s[b];
13         s[b]=temp;
14     }
15 
16     public static void sort(String[] a){
17         sort(a,0,a.length-1,0);
18     }
19 
20     public static void sort(String[] a,int lo,int hi,int d){
21         if(hi<=lo){
22             return;
23         }
24         int lt=lo,gt=hi;
25         int v=charAt(a[lo],d);
26         int i=lo+1;
27         while(i<=hi){
28             int t=charAt(a[i],d);
29             if (t<v){
30                 exch(a,lt++,i++);
31             }
32             else if (t>v){
33                 exch(a,gt--,i);
34             }else {
35                 i++;
36             }
37         }
38         sort(a,lo,lt-1,d);
39         if (v>=0){
40             sort(a,lt,gt,d+1);
41         }
42         sort(a,gt+1,hi,d);
43     }
44 
45

 

Guess you like

Origin www.cnblogs.com/Unicron/p/11567092.html