scg/ch10/SortIntegers

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 SortIntegers {
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("SortIntegers:");
20     ArrayList<Integer> theInts = 
21       new ArrayList<Integer>(Arrays.asList(94283171015));
22 
23     System.out.println("Before:");
24     System.out.println(theInts);
25 
26     sort(theInts);
27 
28     System.out.println("After:");
29     System.out.println(theInts);
30   }
31 
32   /**
33    * Find the index of the largest value inside of aList at or after the
34    * given starting index
35    *
36    @param   aList       reference to the list in which the index of
37    *                      the largest element is to be found
38    @param   startIndex  start searching at this index in the list
39    *
40    @return  a number >= to startIndex, an index into aList; if
41    *          startIndex is out of range, will return startIndex;
42    *          otherwise will always return a valid index
43    */
44   private static int largestIndex(ArrayList<Integer> aList,
45     int startIndex{
46     int largestNdx = startIndex;
47     for (int contenderNdx = startIndex + 1;
48         contenderNdx != aList.size()
49         ++contenderNdx{
50       if (aList.get(contenderNdx> aList.get(largestNdx)) {
51         largestNdx = contenderNdx;
52       }
53     }
54     return largestNdx;
55   }
56 
57   /**
58    * Sort aList in descending order. Uses {@link
59    * #largestIndex(ArrayListint)} and {@link
60    * #swap(ArrayListintint)} to do much of the work.
61    *
62    @param  aList  the list to sort
63    */
64   private static void sort(ArrayList<Integer> aList{
65     for (int firstUnsortedIndex = 0
66         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<Integer> aList, int a,
83     int b{
84     Integer temp = aList.get(a);
85     aList.set(a, aList.get(b));
86     aList.set(b, temp);
87   }
88 }
89 
90 //Uploaded on Mon Mar 29 21:40:27 EDT 2010


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