|
01 packagepackage is used to name the directory or folder a class is in scg.ch11;
02
03 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
04 importimport means to make the classes and/or packages available in this program 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 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects SortIntegers {open braces start code blocks and must be matched with a close brace
12 /**
13 * Initialize a list of {open braces start code blocks and must be matched with a close brace@link Integer}close braces end code blocks and must match an earlier open brace values and sort them using an
14 * insertion sort (find largest remaining, put it in the right spot).
15 *
16 * @paramnull args command-line arguments ignored by thisthis means the current object (the implicit parameter) program
17 */
18 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
19 System.out.println("SortIntegers:");
20 ArrayList<Integer> theInts =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Integer>(Arrays.asList(9,
21 4, 2, 8, 3, 17, 10, 15));
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 }close braces end code blocks and must match an earlier open brace
31
32 /**
33 * Find the index of the largest value inside of aList at or after the
34 * given starting index
35 *
36 * @paramnull aList reference to the list in which the index of
37 * the largest element is to be found
38 * @paramnull startIndex start searching at thisthis means the current object (the implicit parameter) index in the list
39 *
40 * @returnnull a number >=this evaluates to true if the left side is not less than the right side to startIndex, an index into aList; ifif executes the next statement only if the condition in parenthesis evaluates to true
41 * startIndex is out of range, will returnreturn means to provide the result of the method and/or cease execution of the method immediately startIndex;
42 * otherwise will always returnreturn means to provide the result of the method and/or cease execution of the method immediately a valid index
43 */
44 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) intint is the type for whole numbers and it is short for integer largestIndex(ArrayList<Integer> aList,
45 intint is the type for whole numbers and it is short for integer startIndex) {open braces start code blocks and must be matched with a close brace
46 intint is the type for whole numbers and it is short for integer largestNdx =this assignment operator makes the left side equal to the right side startIndex;
47 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer contenderNdx =this assignment operator makes the left side equal to the right side startIndex +adds two numbers together or concatenates Strings together 1;
48 contenderNdx !=this is the not equals operator which evaluates to true if both sides are different aList.size(); ++this is the increment operator, which increases the variable by 1contenderNdx) {open braces start code blocks and must be matched with a close brace
49 ifif executes the next statement only if the condition in parenthesis evaluates to true (aList.get(contenderNdx) > aList.get(largestNdx)) {open braces start code blocks and must be matched with a close brace
50 largestNdx =this assignment operator makes the left side equal to the right side contenderNdx;
51 }close braces end code blocks and must match an earlier open brace
52 }close braces end code blocks and must match an earlier open brace
53 returnreturn means to provide the result of the method and/or cease execution of the method immediately largestNdx;
54 }close braces end code blocks and must match an earlier open brace
55
56 /**
57 * Sort aList in descending order. Uses {open braces start code blocks and must be matched with a close brace@link
58 * #largestIndex(ArrayList, intint is the type for whole numbers and it is short for integer)}close braces end code blocks and must match an earlier open brace and {open braces start code blocks and must be matched with a close brace@link #swap(ArrayList, intint is the type for whole numbers and it is short for integer,
59 * intint is the type for whole numbers and it is short for integer)}close braces end code blocks and must match an earlier open brace to dodo is part of the do-while looping structure (post condition loop) much of the work.
60 *
61 * @paramnull aList the list to sort
62 */
63 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value sort(ArrayList<Integer> aList) {open braces start code blocks and must be matched with a close brace
64 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer firstUnsortedIndex =this assignment operator makes the left side equal to the right side 0; firstUnsortedIndex !=this is the not equals operator which evaluates to true if both sides are different aList.size();
65 ++this is the increment operator, which increases the variable by 1firstUnsortedIndex) {open braces start code blocks and must be matched with a close brace
66 intint is the type for whole numbers and it is short for integer largestIndex =this assignment operator makes the left side equal to the right side largestIndex(aList, firstUnsortedIndex);
67 swap(aList, firstUnsortedIndex, largestIndex);
68 }close braces end code blocks and must match an earlier open brace
69 }close braces end code blocks and must match an earlier open brace
70
71 /**
72 * Swap elements aList[brackets are typically used to declare, initialize and index (indicate which element of) arraysa]brackets are typically used to declare, initialize and index (indicate which element of) arrays and aList[brackets are typically used to declare, initialize and index (indicate which element of) arraysb]brackets are typically used to declare, initialize and index (indicate which element of) arrays (using array notation). Works
73 * forfor is a looping structure for repeatedly executing a block of code all valid index value forfor is a looping structure for repeatedly executing a block of code a and b (even ifif executes the next statement only if the condition in parenthesis evaluates to true they are equal).
74 * Does not dodo is part of the do-while looping structure (post condition loop) anything crafty when they are equal.
75 *
76 * @paramnull aList list in which elements should be changed
77 * @paramnull a an index into aList
78 * @paramnull b an index into aList
79 */
80 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value swap(ArrayList<Integer> aList, intint is the type for whole numbers and it is short for integer a, intint is the type for whole numbers and it is short for integer b) {open braces start code blocks and must be matched with a close brace
81 Integer temp =this assignment operator makes the left side equal to the right side aList.get(a);
82 aList.set(a, aList.get(b));
83 aList.set(b, temp);
84 }close braces end code blocks and must match an earlier open brace
85 }close braces end code blocks and must match an earlier open brace
86
87 //Uploaded on Mon Mar 29 21:39:07 EDT 2010
|