C Array Algorithms: Linear Search, Bubble Sort, Min/Max & Manipulation

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 17 ๐Ÿ“‚ Phase 07: Arrays & Memory Organization ๐Ÿ“… 2026 Comprehensive Master Edition
๐Ÿ“Œ Covered in this in-depth guide: Sum & Average ยท Min & Max in O(N) ยท Linear Search Algorithm ยท Bubble Sort Optimization ยท In-Place Array Reversal ยท Merging Arrays

Welcome to Phase 7 (Chapter 17): Classical Array Algorithms, Searching, Sorting & Memory Manipulation Masterclass! Data structures exist to enable efficient algorithms. In this comprehensive guide, you will master the algorithmic architecture, step-by-step memory trace walkthroughs, and time complexities of 6 foundational algorithmic operations: calculating Sum & Average without integer truncation bugs, single-pass Min/Max searching, Linear Searching, optimized Bubble Sorting with early-termination flags, two-pointer in-place array reversal, and merging arrays into unified memory buffers.

1Sum, Average & Single-Pass Min/Max Search Algorithms

1. Sum & Average ($O(N)$ Time, $O(1)$ Space)

Loop through the array with an accumulator variable. To avoid integer truncation bug during division, cast count to (double):
double avg = (double)sum / size;

2. Minimum & Maximum Search ($O(N)$ Time)

Initialize min = arr[0] and max = arr[0]. Compare each subsequent element from index 1 to $N-1$ in a single sequential linear scan.

2Searching & Sorting: Linear Search vs Optimized Bubble Sort
AlgorithmTime Complexity (Worst / Best)Auxiliary SpaceCore Mechanism
Linear Search $O(N)$ / $O(1)$ $O(1)$ Target element dorike varaku elements ni sequentially index 0 nunchi compare chesthundhi.
Optimized Bubble Sort $O(N^2)$ / $O(N)$ (with flag) $O(1)$ In-Place Pakkana pakkana unna elements ni compare chesi larger value ni right side ki bubble chesthundhi.
C โ€” Comprehensive Algorithmic Suite โ–ถ Run Algorithms
#include <stdio.h>
#include <stdbool.h>

// 1. Linear Search: returns index if found, -1 if not found
int linearSearch(const int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) return i; // Found!
    }
    return -1; // Not found
}

// 2. Optimized Bubble Sort (Early exit if already sorted)
void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        bool swapped = false;
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }
        if (!swapped) break; // Optimized: Array is already sorted!
    }
}

// 3. In-Place Array Reversal (Two-Pointer Technique)
void reverseArray(int arr[], int size) {
    int start = 0, end = size - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

int main(void) {
    int numbers[] = {64, 25, 12, 22, 11};
    int size = 5;

    // Linear Search Demo
    int target = 22;
    int foundIdx = linearSearch(numbers, size, target);
    printf("1. Linear Search: Element %d found at index %d\n", target, foundIdx);

    // Bubble Sort Demo
    bubbleSort(numbers, size);
    printf("2. Sorted Array: ");
    for (int i = 0; i < size; i++) printf("%d ", numbers[i]);
    printf("\n");

    // Reverse Array Demo
    reverseArray(numbers, size);
    printf("3. Reversed Array: ");
    for (int i = 0; i < size; i++) printf("%d ", numbers[i]);
    printf("\n");

    return 0;
}
3Merging Two Arrays into a Unified Array

๐Ÿ“ฆ Merging Logic Explained:

Rendu arrays (Size $N_1$ and $N_2$) ni kalipi third array (Size $N_1 + N_2$) create cheyyadaniki:
1. First array elements ni 0 to $N_1-1$ copy chesthamu.
2. Second array elements ni index $N_1$ nunchi start chesi $N_1 + N_2 - 1$ daka append chesthamu.

๐Ÿ’ป Try It Yourself โ€” Test Array Algorithms in Online C Compiler

Run this Min/Max single pass search in our live GCC compiler:

C (GCC Standard) โ–ถ Open C Compiler
#include <stdio.h>

int main(void) {
    int data[] = {45, 12, 89, 34, 99, 23};
    int size = sizeof(data) / sizeof(data[0]);

    int min = data[0], max = data[0];
    for (int i = 1; i < size; i++) {
        if (data[i] < min) min = data[i];
        if (data[i] > max) max = data[i];
    }
    printf("Min = %d | Max = %d\n", min, max);
    return 0;
}
Open in Online C Compiler โ†’