Apcompscinotes
Apcompscinotes
3. Control Structures
● Conditional Statements:
○ if, else if, else: Used for decision-making (e.g., if (x > 5) { } else { }).
○ switch: Allows multiple cases for decision-making.
● Loops:
○
4. Methods and Functions
● Method: A block of code that performs a specific task, defined with a return type and
parameters.
○ Syntax: returnType methodName(parameters) { }
Example:
java
Copy code
public int add(int a, int b) { return a + b; }
○
● Parameters: Variables passed into methods to perform operations.
Example:
java
Copy code
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
○
7. Sorting and Searching Algorithms
● Sorting Algorithms:
○ Selection Sort: Repeatedly selects the smallest element and moves it to the
front.
○ Bubble Sort: Compares adjacent elements and swaps them if they’re in the
wrong order.
● Searching Algorithms:
○ Linear Search: Checks each element in a list for a match.
○ Binary Search: Efficient search on a sorted list by repeatedly dividing the range
in half.
8. Recursion
Example:
java
Copy code
public int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
● Big O: Describes the efficiency of an algorithm in terms of time or space as the input
size grows.
○ O(1): Constant time.
○ O(n): Linear time.
○ O(n^2): Quadratic time.