📓
CS 010C: Study Guide
UCR’s CS010C - Data Structures & Algorithms
Professor Pat Miller [Winter 2024 Final Exam: Study Guide]
HEAPS
heaps: a complete binary tree that satisfies the heap property
completely filled at all levels
height of O(logn), where n =number of elements
efficient for priority queue
implementations
max-heap: the max element is
at the root
min-heap: the min element is at
the root
CS 010C: Study Guide 1
, complete binary tree: every level, except possibly the last, is completely filled & all
nodes are as far left as possible
this allows for efficient storage & retrieval of elements without using explicit
pointers/references as in linked lists
insertion, deletion, heapification, and other heap operations are easier to
implement because the structure of complete binary trees make it so that the
child nodes are in predictable positions
less storage because no memory is occupied by pointers
File I/O
string filename("bar.txt");
int x;
// open file
input.open(filename);
// check if file was opened
if (!input.is_open()) {
throw runtime_error("can't open file");
}
// attempt to read int x to file
input >> x;
// check if anything was read (if not, it's an empty file)
if(input.eof()) {
throw runtime_error("empty file");
}
// check if integer was read (if not, it wasn't an int)
if(input.fail()) {
CS 010C: Study Guide 2
, throw runtime_error("not an integer");
}
Sorting Algorithms
SELECTION SORT
selection sort: repeatedly finds the minimum element from the unsorted section &
moves it to the sorted section until the whole array is sorted.
has O(n2 )runtime complexity
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
}
}
BUBBLE SORT
bubble sort: repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order. The pass through the list is repeated
until the list is sorted; the algorithm gets its name because smaller elements
"bubble" to the top of the list (beginning) with each iteration.
has O(n2 )runtime complexity
CS 010C: Study Guide 3
CS 010C: Study Guide
UCR’s CS010C - Data Structures & Algorithms
Professor Pat Miller [Winter 2024 Final Exam: Study Guide]
HEAPS
heaps: a complete binary tree that satisfies the heap property
completely filled at all levels
height of O(logn), where n =number of elements
efficient for priority queue
implementations
max-heap: the max element is
at the root
min-heap: the min element is at
the root
CS 010C: Study Guide 1
, complete binary tree: every level, except possibly the last, is completely filled & all
nodes are as far left as possible
this allows for efficient storage & retrieval of elements without using explicit
pointers/references as in linked lists
insertion, deletion, heapification, and other heap operations are easier to
implement because the structure of complete binary trees make it so that the
child nodes are in predictable positions
less storage because no memory is occupied by pointers
File I/O
string filename("bar.txt");
int x;
// open file
input.open(filename);
// check if file was opened
if (!input.is_open()) {
throw runtime_error("can't open file");
}
// attempt to read int x to file
input >> x;
// check if anything was read (if not, it's an empty file)
if(input.eof()) {
throw runtime_error("empty file");
}
// check if integer was read (if not, it wasn't an int)
if(input.fail()) {
CS 010C: Study Guide 2
, throw runtime_error("not an integer");
}
Sorting Algorithms
SELECTION SORT
selection sort: repeatedly finds the minimum element from the unsorted section &
moves it to the sorted section until the whole array is sorted.
has O(n2 )runtime complexity
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
}
}
BUBBLE SORT
bubble sort: repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order. The pass through the list is repeated
until the list is sorted; the algorithm gets its name because smaller elements
"bubble" to the top of the list (beginning) with each iteration.
has O(n2 )runtime complexity
CS 010C: Study Guide 3