scg/ch11/SortIntegers

From FANG

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


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