scg/ch11/SortStrings

From FANG

Jump to: navigation, search

01 package scg.ch11;
02 
03 import java.util.ArrayList;
04 import java.util.Arrays;
05 
06 /**
07  * Initialize a literal ArrayList, print it, then sort it and print it
08  * again. The three method sort matches the example algorithm in SCG
09  * chapter 10.
10  */
11 public class SortStrings {
12   /**
13    * Initialize a list of {@link Integer} values and sort them using an
14    * insertion sort (find largest remaining, put it in the right spot).
15    *
16    @param  args  command-line arguments ignored by this program
17    */
18   public static void main(String[] args{
19     System.out.println("SortSrrings:");
20     ArrayList<String> someAnimals = new ArrayList<String>(Arrays.asList(
21           "dog""cat""hamster""catfish""pig""aardvark",
22           "zebra"));
23 
24     System.out.println("Before:");
25     System.out.println(someAnimals);
26 
27     sort(someAnimals);
28 
29     System.out.println("After:");
30     System.out.println(someAnimals);
31   }
32 
33   /**
34    * Find the index of the largest value inside of aList at or after the
35    * given starting index
36    *
37    @param   aList       reference to the list in which the index of
38    *                      the largest element is to be found
39    @param   startIndex  start searching at this index in the list
40    *
41    @return  a number >= to startIndex, an index into aList; if
42    *          startIndex is out of range, will return startIndex;
43    *          otherwise will always return a valid index
44    */
45   private static int largestIndex(ArrayList<String> aList,
46     int startIndex{
47     int largestNdx = startIndex;
48     for (int contenderNdx = startIndex + 1;
49         contenderNdx != aList.size()++contenderNdx{
50       if (aList.get(contenderNdx).compareTo(aList.get(largestNdx)) >
51           0{
52         largestNdx = contenderNdx;
53       }
54     }
55     return largestNdx;
56   }
57 
58   /**
59    * Sort aList in descending order. Uses {@link
60    * #largestIndex(ArrayListint)} and {@link #swap(ArrayListint,
61    int)} to do much of the work.
62    *
63    @param  aList  the list to sort
64    */
65   private static void sort(ArrayList<String> aList{
66     for (int firstUnsortedIndex = 0; firstUnsortedIndex != aList.size();
67         ++firstUnsortedIndex{
68       int largestIndex = largestIndex(aList, firstUnsortedIndex);
69       swap(aList, firstUnsortedIndex, largestIndex);
70     }
71   }
72 
73   /**
74    * Swap elements aList[a] and aList[b] (using array notation). Works
75    for all valid index value for a and b (even if they are equal).
76    * Does not do anything crafty when they are equal.
77    *
78    @param  aList  list in which elements should be changed
79    @param  a      an index into aList
80    @param  b      an index into aList
81    */
82   private static void swap(ArrayList<String> aList, int a, int b{
83     String temp = aList.get(a);
84     aList.set(a, aList.get(b));
85     aList.set(b, temp);
86   }
87 }
88 
89 //Uploaded on Mon Mar 29 21:42:04 EDT 2010


Download/View scg/ch11/SortStrings.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games