Array Manipulation

Lesson 5.2: Array Manipulation (Accessing, Sorting, Searching)


Now that you know how to create and initialize arrays, let's explore how to work with the data they contain. In this lesson, you'll learn essential techniques for accessing individual elements, sorting the contents of an array, and searching for specific values.


Accessing Array Elements


Indexing: The most common way to access an element in an array is by its index. Remember that array indices start at 0.

Syntax:

C

arrayName[index]

Example:

C

int numbers[5] = {10, 25, 3, 18, 7};

int thirdElement = numbers[2];  // Accessing the third element (value is 3)

numbers[4] = 22;        // Modifying the last element (now 22)


Sorting Arrays


Why Sort? Sorting data is a fundamental task in programming. It makes it easier to find specific values, organize information, and analyze data.

Bubble Sort (Simple, but Less Efficient):

C

void bubbleSort(int arr[], int size) {

    for (int i = 0; i < size - 1; i++) {

        for (int j = 0; j < size - i - 1; j++) {

            if (arr[j] > arr[j + 1]) { // Swap elements if out of order

                int temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

}


Other Sorting Algorithms: As you progress in C, you'll learn about more efficient sorting algorithms like insertion sort, selection sort, merge sort, and quicksort.

Searching Arrays


Linear Search (Simple, but Slower for Large Arrays):

C

int linearSearch(int arr[], int size, int target) {

    for (int i = 0; i < size; i++) {

        if (arr[i] == target) {

            return i;  // Return the index where the target is found

        }

    }

    return -1; // Target not found

}


Binary Search (Efficient for Sorted Arrays):

Requires the array to be sorted first.

Repeatedly divides the search interval in half. If the middle element is the target, it's found. If the target is smaller, the search continues in the lower half, otherwise in the upper half.

Example: Putting It All Together


C

#include <stdio.h>


// ... (bubbleSort and linearSearch functions)


int main() {

    int data[] = {64, 34, 25, 12, 22, 11, 90};

    int size = sizeof(data) / sizeof(data[0]);

    int target = 25;


    bubbleSort(data, size); // Sort the array

    int index = linearSearch(data, size, target);


    if (index != -1) {

        printf("Target found at index %d\n", index);

    } else {

        printf("Target not found.\n");

    }

    return 0;

}


Key Takeaways:


Arrays: Powerful for storing and manipulating collections of data.

Indexing: Use [] to access elements by their position.

Sorting: Makes data more organized and easier to search.

Searching: Linear search is simple, binary search is faster for sorted arrays.

Additional Tips:


Explore the C standard library functions in <string.h> and <stdlib.h> for additional array manipulation tools.

Practice writing your own array manipulation functions to solidify your understanding.

Experiment with different sorting and searching algorithms to compare their efficiency.

Course Syllabus