scg/ch10/SortStrings

From FANG

Jump to: navigation, search

01 package scg.ch10;
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)) 0{
51         largestNdx = contenderNdx;
52       }
53     }
54     return largestNdx;
55   }
56 
57   /**
58    * Sort aList in descending order. Uses {@link
59    * #largestIndex(ArrayListint)} and {@link #swap(ArrayListint,
60    int)} to do much of the work.
61    *
62    @param  aList  the list to sort
63    */
64   private static void sort(ArrayList<String> aList{
65     for (int firstUnsortedIndex = 0; firstUnsortedIndex != aList.size();
66         ++firstUnsortedIndex{
67       int largestIndex = largestIndex(aList, firstUnsortedIndex);
68       swap(aList, firstUnsortedIndex, largestIndex);
69     }
70   }
71 
72   /**
73    * Swap elements aList[a] and aList[b] (using array notation). Works
74    for all valid index value for a and b (even if they are equal).
75    * Does not do anything crafty when they are equal.
76    *
77    @param  aList  list in which elements should be changed
78    @param  a      an index into aList
79    @param  b      an index into aList
80    */
81   private static void swap(ArrayList<String> aList, int a, int b{
82     String temp = aList.get(a);
83     aList.set(a, aList.get(b));
84     aList.set(b, temp);
85   }
86 }
87 
88 //Uploaded on Mon Mar 29 21:38:58 EDT 2010


Download/View scg/ch10/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