|
01 packagepackage is used to name the directory or folder a class is in scg.ch10;
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 * @paramthis is the Javadoc tag for documenting the purpose of parameters 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
21 newnew is used to create objects by calling the constructor ArrayList<Integer>(Arrays.asList(9, 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 * @paramthis is the Javadoc tag for documenting the purpose of parameters aList reference to the list in which the index of
37 * the largest element is to be found
38 * @paramthis is the Javadoc tag for documenting the purpose of parameters 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();
49 ++this is the increment operator, which increases the variable by 1contenderNdx) {open braces start code blocks and must be matched with a close brace
50 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
51 largestNdx =this assignment operator makes the left side equal to the right side contenderNdx;
52 }close braces end code blocks and must match an earlier open brace
53 }close braces end code blocks and must match an earlier open brace
54 returnreturn means to provide the result of the method and/or cease execution of the method immediately largestNdx;
55 }close braces end code blocks and must match an earlier open brace
56
57 /**
58 * Sort aList in descending order. Uses {open braces start code blocks and must be matched with a close brace@link
59 * #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
60 * #swap(ArrayList, intint is the type for whole numbers and it is short for integer, 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.
61 *
62 * @paramthis is the Javadoc tag for documenting the purpose of parameters aList the list to sort
63 */
64 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
65 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;
66 firstUnsortedIndex !=this is the not equals operator which evaluates to true if both sides are different aList.size();
67 ++this is the increment operator, which increases the variable by 1firstUnsortedIndex) {open braces start code blocks and must be matched with a close brace
68 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);
69 swap(aList, firstUnsortedIndex, largestIndex);
70 }close braces end code blocks and must match an earlier open brace
71 }close braces end code blocks and must match an earlier open brace
72
73 /**
74 * 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
75 * 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).
76 * Does not dodo is part of the do-while looping structure (post condition loop) anything crafty when they are equal.
77 *
78 * @paramthis is the Javadoc tag for documenting the purpose of parameters aList list in which elements should be changed
79 * @paramthis is the Javadoc tag for documenting the purpose of parameters a an index into aList
80 * @paramthis is the Javadoc tag for documenting the purpose of parameters b an index into aList
81 */
82 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,
83 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
84 Integer temp =this assignment operator makes the left side equal to the right side aList.get(a);
85 aList.set(a, aList.get(b));
86 aList.set(b, temp);
87 }close braces end code blocks and must match an earlier open brace
88 }close braces end code blocks and must match an earlier open brace
89
90 //Uploaded on Mon Mar 29 21:40:27 EDT 2010
|