AP CSA Unit 1
Algorithm
An algorithm is a step by step process to follow when completing a task or solving a problem.
It is helpful to be very specific and detailed when writing algorithms.
Data Types
Integers are whole numbers, doubles are decimals, booleans are true/false.
Printing
Use System.out.print() to print in Java.
Comments
Use // for comments.
Documentation
Documentation helps explain how code works—APIs are instruction manuals for code.
AP CSA Unit 4 – Big Idea 4 (Data & Arrays)
4.1 Ethical and Social Issues Around Data Collection
- Data collection includes personal, behavioral, location, and biometric data.
- Privacy → users should know what data is being collected and how it is used.
- Bias in data can cause unfair or inaccurate program results.
- Programmers must consider fairness and avoid discriminatory algorithms.
- Data security protects against breaches and hacking.
- Ethical responsibility → collect only necessary data and protect it properly.
- Programs can have major societal impacts (positive or harmful).
4.2 Introduction to Using Data Sets
- Data set → collection of related values.
- In Java, data sets are commonly stored in arrays.
- Programs analyze data to find patterns or make decisions.
- Data sets allow efficient storage of multiple values.
- Examples: test scores, temperatures, survey results, statistics.
4.3 Array Creation and Access
- Array → fixed-size collection of same data type.
- Arrays are 0-indexed (first element is index 0).
- Declaration:
int[] numbers;
numbers = new int[5];
int[] numbers = new int[5];
int[] numbers = {1, 2, 3, 4};
- Access element →
numbers[index]
- Length →
numbers.length (no parentheses)
- Invalid index causes ArrayIndexOutOfBoundsException.
4.4 Array Traversals
- Traversal → visiting every element in an array.
- Standard for loop:
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
- Enhanced for loop (for-each):
for(int value : arr) {
System.out.println(value);
}
- Common patterns:
- Sum (accumulator)
- Find maximum/minimum
- Count values meeting a condition
- Off-by-one errors happen when using ≤ instead of <
4.5 Implementing Array Algorithms
- Algorithm → step-by-step solution.
- Linear search scans from index 0 to end.
public static int find(int[] arr, int target) {
for(int i = 0; i < arr.length; i++) {
if(arr[i] == target) {
return i;
}
}
return -1;
}
for(int i = 0; i < arr.length; i++) {
arr[i] *= 2;
}
for(int i = 0; i < arr.length; i++) {
if(arr[i] < 0) {
arr[i] = 0;
}
}
- Efficiency: linear search is O(n).
- Be able to trace and predict final array values on the AP exam.
4.6 Using Text Files
- Programs can read data stored in files.
- Useful when working with large data sets.
- Files often contain numbers, text, or structured data.
- Data from files is often stored in arrays or ArrayLists for processing.
- Programs must handle missing or invalid data safely.
4.7 Wrapper Classes
- Wrapper classes convert primitive types into objects.
- Needed when using collections like ArrayList.
- Examples:
int → Integer
double → Double
char → Character
boolean → Boolean
- Autoboxing → automatic conversion from primitive to wrapper object.
- Unboxing → converting wrapper object back to primitive.
4.8 ArrayList Methods
- ArrayList → resizable array structure.
- Unlike arrays, size can grow or shrink.
ArrayList nums = new ArrayList<>();
nums.add(5); // add to end
nums.add(0, 3); // insert at index
nums.get(0); // retrieve value
nums.set(1, 10); // replace value
nums.remove(0); // remove element
nums.size(); // number of elements
nums.clear(); // remove all elements
- ArrayLists store objects, not primitives.
4.9 ArrayList Traversals
- Traversal means visiting every element.
- Use
.size() instead of .length.
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for(int value : list) {
System.out.println(value);
}
- Enhanced loop cannot modify elements directly.
4.10 Implementing ArrayList Algorithms
- Same patterns used for arrays apply to ArrayLists.
- Common tasks:
- Searching for a value
- Counting occurrences
- Finding max/min
- Removing elements meeting a condition
- When removing elements during traversal → loop backwards.
for(int i = list.size() - 1; i >= 0; i--) {
if(list.get(i) < 0) {
list.remove(i);
}
}
4.11 2D Array Creation and Access
- 2D array → array of arrays.
- Used for tables, grids, or boards.
int[][] grid = new int[3][4];
grid[0][1] = 5;
grid.length → number of rows.
grid[0].length → number of columns.
4.12 2D Array Traversals
for(int r = 0; r < grid.length; r++) {
for(int c = 0; c < grid[r].length; c++) {
System.out.println(grid[r][c]);
}
}
- Outer loop → rows
- Inner loop → columns
4.13 Implementing 2D Array Algorithms
- Algorithms use nested traversals.
- Common problems:
- Find largest value
- Count occurrences
- Sum rows or columns
- Replace values
- Search grid patterns
4.14 Searching Algorithms
- Linear Search
- Checks each element one by one.
for(int i = 0; i < arr.length; i++) {
if(arr[i] == target)
return i;
}
- Binary Search
- Works only on sorted arrays.
- Repeatedly checks the middle element.
4.15 Sorting Algorithms
- Sorting → arranging elements in order.
- AP CSA focuses on two algorithms:
- Selection Sort
- Insertion Sort
- Selection Sort:
- Find smallest element and swap into correct position.
- Insertion Sort:
- Build sorted section one element at a time.
4.16 Recursion
- Recursion → method that calls itself.
- Requires two parts:
- Base case → stops recursion.
- Recursive call → smaller version of the problem.
public int factorial(int n) {
if(n == 0)
return 1;
return n * factorial(n - 1);
}
- Missing base case causes infinite recursion.
4.17 Recursive Searching and Sorting
- Binary search can be implemented recursively.
- Algorithm repeatedly searches smaller halves of the array.
- Recursion reduces the search range each call.
- Stops when element is found or range becomes empty.