c / c-arrays-strings
22 mins
C Level 9: Searching, Sorting & Algorithmic Patterns
Why This Matters: Applying pointer iteration and arrays to solve core computer science algorithms.
## Linear & Binary Search in C
Algorithmic problem solving requires combining loops, array indexing, and conditional logic.
```c
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) return i;
}
return -1;
}
```
MENTAL MODEL & MEMORY LAYOUT
Linear Search Traversal: [ Index 0 ] ──► [ Index 1 ] ──► [ Index 2 ] ... ──► [ Target Found at Index i ]
COMMON PITFALLS TO AVOID
- Searching past size bounds `i <= size` instead of `i < size`.
- Forgetting to return -1 when element is missing.
Bubble Sort Implementation in C
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int nums[] = {5, 2, 8, 1, 9};
bubbleSort(nums, 5);
printf("Sorted first: %d\n", nums[0]);
return 0;
}Sorts array in ascending order by repeatedly swapping adjacent elements out of order.
CONCEPT MASTERY CHECKPOINT
What is the worst-case time complexity of Bubble Sort on an array of size N?
NEXT RECOMMENDED LESSON
C Level 10: Systems & Debugging Mastery
Challenge: Write a function `int findMax(int arr[], int n)` that returns the largest value in an array.