C Notes
Learn essential array operations in C including linear search, binary search, bubble sort, selection sort, insertion, and deletion with step-by-step examples.
Working with arrays goes far beyond simply storing and retrieving values. The real power comes from the operations you perform on them — searching for elements, sorting them into order, inserting new values, and removing unwanted ones. These operations form the foundation of almost every algorithm you'll encounter in computer science.
Searching Operations
Linear Search (Sequential Search)
The simplest search technique — examine each element one by one until you find the target or reach the end.
Time Complexity: O(n) — worst case checks every element.
96 found at index 4
Binary Search (Requires Sorted Array)
A much faster algorithm that repeatedly divides the search space in half. The array must be sorted first.
Time Complexity: O(log n) — dramatically faster for large arrays.
67 found at index 5
How Binary Search Works:
| Array | [12, 23, 34, 45, 56, 67, 78, 89, 96] |
| Target | 67 |
| Step 1: low=0, high=8, mid=4 | arr[4]=56 < 67 → low=5 |
| Step 2: low=5, high=8, mid=6 | arr[6]=78 > 67 → high=5 |
| Step 3: low=5, high=5, mid=5 | arr[5]=67 == 67 → FOUND at index 5! |
Search Comparison Table
| Feature | Linear Search | Binary Search |
|---|---|---|
| Time Complexity | O(n) | O(log n) |
| Requires Sorted? | No | Yes |
| Best for | Small/unsorted arrays | Large sorted arrays |
| Space Complexity | O(1) | O(1) iterative |
Sorting Operations
Bubble Sort
Repeatedly swap adjacent elements if they're in the wrong order. Simple but inefficient for large arrays.
Time Complexity: O(n²)
Before: 64 34 25 12 22 11 90 After: 11 12 22 25 34 64 90
Selection Sort
Find the minimum element and place it at the beginning, then repeat for the remaining portion.
Time Complexity: O(n²)
Sorted: 10 13 14 29 37
Insertion Sort
Build the sorted portion one element at a time by inserting each new element into its correct position.
Time Complexity: O(n²) worst case, O(n) best case (nearly sorted data)
Sorted: 5 6 11 12 13
Insertion into an Array
To insert an element at a specific position, shift existing elements to make room:
Before insertion: 10 20 30 40 50 After inserting 25 at index 2: 10 20 25 30 40 50
| Before | [10][20][30][40][50][ ][ ][ ][ ][ ] |
| Step 1 | Shift right from end |
| Step 2 | Place value |
Deletion from an Array
To delete an element, shift subsequent elements to fill the gap:
Before deletion: 10 20 30 40 50 After deleting index 2: 10 20 40 50
Operation Complexity Summary
| Operation | Time Complexity | Notes |
|---|---|---|
| Access by index | O(1) | Direct address calculation |
| Linear search | O(n) | Must check each element |
| Binary search | O(log n) | Requires sorted array |
| Insertion at end | O(1) | No shifting needed |
| Insertion at position | O(n) | Shift elements right |
| Deletion at end | O(1) | Just decrease size |
| Deletion at position | O(n) | Shift elements left |
| Bubble/Selection sort | O(n²) | Simple but slow |
Interview Questions
Q1: When is linear search preferred over binary search?
Linear search is preferred when: the array is unsorted, the array is very small (overhead of sorting outweighs benefit), you're searching only once, or the data structure doesn't support random access.
Q2: Why do we use mid = low + (high - low) / 2 instead of mid = (low + high) / 2?
To prevent integer overflow. If low and high are both large values near INT_MAX, their sum can overflow. The subtraction form avoids this by first computing the difference.
Q3: What is the best-case time complexity of bubble sort with the optimization flag?
O(n) — if the array is already sorted, the inner loop makes one pass without any swaps, the swapped flag remains 0, and the algorithm exits early.
Q4: How would you delete all occurrences of a value from an array?
Use a two-pointer technique: one pointer reads each element, the other writes only non-matching elements. This achieves O(n) time with O(1) extra space.
Summary
- Linear search is simple O(n); binary search is O(log n) but requires sorted data
- Bubble, selection, and insertion sorts are O(n²) but easy to implement
- Array insertion requires shifting elements right — O(n) worst case
- Array deletion requires shifting elements left — O(n) worst case
- Always pass array capacity to prevent buffer overflow during insertion
- Binary search uses the divide-and-conquer strategy for efficient lookup
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Array Operations in C - Searching, Sorting, Insert and Delete.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, arrays, array, operations, array operations in c - searching, sorting, insert and delete
Related C Programming Topics