COMPSCI SG FINAL

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/41

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

42 Terms

1
New cards

Algorithm Efficiency

// Algorithm Efficiency: Linear vs Constant time
void example(int[] arr) {
System.out.println(arr[0]); // O(1)
for (int i : arr) { // O(n)
System.out.println(i);
}
}

2
New cards

Growth Functions of a program

// Growth Function Example
void growthExample(int n) {
for (int i = 0; i < n; i++) { // O(n)
for (int j = 0; j < n; j++) { // O(n)
System.out.println(i + j); // Total O(n^2)
}
}
}

3
New cards

Asymptotic Complexity of an algorithm

// Asymptotic Complexity: ignores lower order terms
void example(int n) {
int count = 0;
for (int i = 0; i < n; i++) count++; // O(n)
for (int j = 0; j < 1000; j++) count++; // O(1)
// Overall: O(n)
}

4
New cards

Big-O Notation

// Big-O: Upper bound for growth
void logExample(int n) {
while (n > 1) {
n /= 2;
System.out.println(n);
} // O(log n)
}

5
New cards

Big-O/order Categories

// Big-O Categories
// O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n)

6
New cards

Comparing Growth Functions and orders

// Comparing growths
// Linear: O(n), Quadratic: O(n^2)
// n = 1000 → n = 1000, n^2 = 1,000,000

7
New cards

Analyzing Loop/Nested loops Execution

// Loop Analysis
void nestedLoops(int n) {
for (int i = 0; i < n; i++) { // O(n)
for (int j = 0; j < n; j++) { // O(n)
System.out.println(i * j); // O(n^2)
}
}
}

8
New cards

ADT

// Abstract Data Type (ADT) Example: Stack
interface StackADT {
void push(T item);
T pop();
boolean isEmpty();
}

9
New cards

Stack

// Stack with Java
Stack stack = new Stack<>();
stack.push(10);
System.out.println(stack.pop()); // 10

10
New cards

Linear and Non-Linear Collections

// Linear: ArrayList, Stack, Queue
// Non-Linear: Tree, Graph

11
New cards

Stack Operations

// Stack Operations
Stack stack = new Stack<>();
stack.push(1); // add
stack.peek(); // top
stack.pop(); // remove
stack.isEmpty(); // check empty

12
New cards

The concept of Implementation of Stacks

// Stack using array
class ArrayStack {
int[] data = new int[100];
int top = -1;

void push(int x) { data[++top] = x; }
int pop() { return data[top--]; }

}

13
New cards

Idea of applications of stack

// Applications of Stack
// Expression evaluation, Undo feature, DFS in graphs

14
New cards

Linked Structures

// Linked Node
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}

15
New cards

Linked Lists

// Singly Linked List insert
Node head = new Node(1);
head.next = new Node(2);

16
New cards

What is a queue? Queue’s features.

// Queue: FIFO
Queue q = new LinkedList<>();
q.add(1); q.add(2);
System.out.println(q.remove()); // 1

17
New cards

Operations of queue

// Queue Operations
Queue q = new LinkedList<>();
q.add(10); // enqueue
q.remove(); // dequeue
q.peek(); // front

18
New cards

Implementation of queue

// Queue using array
class Queue {
int[] arr = new int[100];
int front = 0, rear = 0;

void enqueue(int x) { arr[rear++] = x; }
int dequeue() { return arr[front++]; }

}

19
New cards

What is a list?

// List: ordered collection
List list = new ArrayList<>();
list.add(5); list.add(10);

20
New cards

Three types of list collections

// Types of Lists
// ArrayList, LinkedList, Vector

21
New cards

Standard Linkedlist in Java

// LinkedList in Java
LinkedList list = new LinkedList<>();
list.add("A");
list.add("B");

22
New cards

Recursion

// Recursion Example
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

23
New cards

Recursive Definitions

// Fibonacci recursively
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}

24
New cards

Infinite Recursion

// Infinite Recursion (no base case)
void infinite() {
infinite();
}

25
New cards

Recursion in Math

// Recursion: factorial
// n! = n * (n-1)!

26
New cards

Recursive Programming

// Print 1 to n
void print(int n) {
if (n == 0) return;
print(n - 1);
System.out.println(n);
}

27
New cards

Recursion vs. Iteration

// Compare Recursion vs Iteration
// Recursion: concise, more memory
// Iteration: efficient, less stack usage

28
New cards

Linear Search

// Linear Search
int search(int[] arr, int x) {
for (int i = 0; i < arr.length; i++)
if (arr[i] == x) return i;
return -1;
}

29
New cards

Binary Search

// Binary Search (sorted array)
int binarySearch(int[] a, int x) {
int l = 0, r = a.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == x) return m;
if (a[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}

30
New cards

Comparing Search Algorithms

// Linear: O(n), Binary: O(log n) (sorted only)

31
New cards

Selection Sort

// Selection Sort
void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[min]) min = j;
int t = arr[i]; arr[i] = arr[min]; arr[min] = t;
}
}

32
New cards

Insertion Sort

// Insertion Sort
void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) { int key = arr[i], j = i - 1; while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
}

33
New cards

Merge Sort

// Merge Sort
void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

34
New cards

Comparing Sorts

// Time Complexity:
// Selection & Insertion: O(n^2)
// Merge Sort: O(n log n)

35
New cards

Trees

// Binary Tree Node
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}

36
New cards

Binary search in a sorted array

// Binary search - see earlier card

37
New cards

Tree Traversals

// Inorder Traversal
void inorder(TreeNode root) {
if (root != null) {
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
}

38
New cards

Create a binary with given numbers

// Tree with numbers 5, 3, 7
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(7);

39
New cards

What is a Graph?

// Graph: set of nodes and edges
// Directed/Undirected, Weighted/Unweighted

40
New cards

Terminologies of graph

// Terms: vertex, edge, degree, path, cycle

41
New cards

Graph Representations

// Adjacency List
Map

42
New cards

Graph Traversal

// DFS
void dfs(int v, boolean[] visited, List