mardi 3 mars 2015

Beginner, looking for example for a SortString function

I have started learning Java and the lesson was about strings. I don't understand them well so I tried finding other algorithms to try and learn from them. I found this one but I don't understand it how it works. I know it's something similar to QuickSort method but don't actually understand it.


Here is the code:



1 package junit.runner;
2
3 import java.util.*;
4
5 import junit.runner.*;
6
7 /**
8 * A custom quick sort with support to customize the swap behaviour.
9 * NOTICE: We can't use the the sorting support from the JDK 1.2 collection
10 * classes because of the JDK 1.1.7 compatibility.
11 */
12 public class Sorter {
13 public static interface Swapper {
14 public void swap(Vector values, int left, int right);
15 }
16
17 public static void sortStrings(Vector values , int left, int right, Swapper swapper) {
18 int oleft= left;
19 int oright= right;
20 String mid= (String)values.elementAt((left + right) / 2);
21 do {
22 while (((String)(values.elementAt(left))).compareTo(mid) < 0)
23 left++;
24 while (mid.compareTo((String)(values.elementAt(right))) < 0)
25 right--;
26 if (left <= right) {
27 swapper.swap(values, left, right);
28 left++;
29 right--;
30 }
31 } while (left <= right);
32
33 if (oleft < right)
34 sortStrings(values, oleft, right, swapper);
35 if (left < oright)
36 sortStrings(values, left, oright, swapper);
37 }
38 }


Here is the link to the function: http://ift.tt/1zWgGaY


If somebody could use an example and describe it I would be really thankful. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire