πŸš€ Arrays in C – Traversal, Updation, Sorting with Programs (DSA Complete Beginner Guide)

Previous Blog in This Series

πŸ‘‰ Array Operations in C – Insertion, Deletion, Searching (DSA Guide)

Next Blog in This Series

πŸ‘‰ 2D Arrays in C – Matrix Operations Complete Guide


πŸ“˜ Introduction

Arrays are one of the most fundamental and powerful data structures in C programming and Data Structures & Algorithms (DSA).

Before learning Linked Lists, Stacks, Queues, Trees, and Graphs, every student must first master arrays because arrays form the base of advanced problem solving.

In this complete beginner guide, we will learn:

✔ What is Array Traversal
✔ What is Array Updation
✔ Sorting Basics
✔ Bubble Sort with Full Program
✔ Time Complexity Analysis
✔ Real-Life Applications
✔ Common Mistakes
✔ Interview Questions
✔ FAQs

This blog is highly useful for:

  • B.Tech / BCA / MCA students

  • Placement preparation

  • Coding interviews

  • Competitive programming beginners

  • DSA learners


πŸ“ What is an Array?

An array is a collection of elements of the same data type stored at contiguous memory locations.

It helps us store multiple values using a single variable name instead of creating many separate variables.

Example

int marks[5] = {90, 85, 78, 92, 88};  // Array storing 5 students' marks in a single variable

// Instead of writing separate variables:
int m1, m2, m3, m4, m5;  // Individual variables for each mark (less efficient and harder to manage)

we use a single array.

This makes code cleaner, faster, and easier to manage.


🧠 Visual Understanding of Array

Index:    0    1    2    3    4
Value:   10   20   30   40   50

Each element has:

  • Value

  • Index position

Array indexing starts from 0 in C.


πŸ” Array Traversal

What is Traversal?

Traversal means visiting each element of the array one by one to perform operations like:

  • Printing elements

  • Calculating sum

  • Finding maximum

  • Finding minimum

  • Counting elements

Traversal is one of the most common array operations.


πŸ’» Program: Array Traversal in C

#include <stdio.h>   // Includes standard input-output functions like printf

int main()           // Main function where program execution starts
{
    int arr[5] = {10, 20, 30, 40, 50};  // Declare and initialize an array of 5 integers
    int i;                              // Loop variable for iteration

    printf("Array Elements are:\n");    // Print heading message

    for(i = 0; i < 5; i++)              // Loop from index 0 to 4 (total 5 elements)
    {
        printf("%d ", arr[i]);          // Print each array element using its index
    }

    return 0;                           // End of program (successful execution)
}

πŸ“€ Output

Array Elements are:
10 20 30 40 50

✏️ Array Updation

What is Updation?

Updation means changing the value of an existing element in the array.

For example:

arr[2] = 100;   // Assigns value 100 to the 3rd element of the array (index starts from 0)

This changes the third element from 30 to 100.


πŸ’» Program: Array Updation in C

#include <stdio.h>   // Includes standard input-output functions

int main()           // Main function starts execution
{
    int arr[5] = {10, 20, 30, 40, 50};  // Declare and initialize array with 5 elements

    printf("Before Updation:\n");        // Print message before updating array

    for(int i = 0; i < 5; i++)          // Loop to print all elements before update
    {
        printf("%d ", arr[i]);          // Print each element using index
    }

    arr[2] = 100;                       // Update 3rd element (index 2) to 100

    printf("\n\nAfter Updation:\n");     // Print message after updating array

    for(int i = 0; i < 5; i++)          // Loop to print all elements after update
    {
        printf("%d ", arr[i]);          // Print updated array elements
    }

    return 0;                           // End of program
}

πŸ“€ Output

Before Updation:
10 20 30 40 50

After Updation:
10 20 100 40 50

πŸ”„ Sorting Basics

What is Sorting?

Sorting means arranging data in a specific order.

Usually:

Ascending Order

Small → Large

Example:

10 20 30 40 50

Descending Order

Large → Small

Example:

50 40 30 20 10

Sorting helps in:

  • Faster searching

  • Better organization

  • Cleaner output

  • Efficient algorithms


🌟 Bubble Sort (Simple Sorting Method)

Bubble Sort is one of the easiest sorting algorithms for beginners.

It repeatedly compares adjacent elements and swaps them if they are in the wrong order.


🧠 Bubble Sort Visualization

Given Array:

50 20 40 10 30

Pass 1

50 > 20 → Swap
20 50 40 10 30

50 > 40 → Swap
20 40 50 10 30

50 > 10 → Swap
20 40 10 50 30

50 > 30 → Swap
20 40 10 30 50

Largest element moves to the end.

This repeats until fully sorted.


πŸ’» Program: Bubble Sort in C

#include <stdio.h>   // Includes standard input-output functions

int main()           // Main function starts execution
{
    int arr[5] = {50, 20, 40, 10, 30};  // Declare and initialize array with unsorted elements
    int i, j, temp;                     // i, j for loops and temp for swapping

    printf("Before Sorting:\n");        // Print message before sorting

    for(i = 0; i < 5; i++)              // Loop to print original array
    {
        printf("%d ", arr[i]);          // Print each element
    }

    for(i = 0; i < 5 - 1; i++)          // Outer loop for number of passes
    {
        for(j = 0; j < 5 - i - 1; j++)  // Inner loop for comparing adjacent elements
        {
            if(arr[j] > arr[j + 1])     // If current element is greater than next
            {
                temp = arr[j];          // Store current element in temp
                arr[j] = arr[j + 1];    // Move smaller element to left
                arr[j + 1] = temp;      // Place stored value to right
            }
        }
    }

    printf("\n\nAfter Sorting (Ascending Order):\n");  // Message after sorting

    for(i = 0; i < 5; i++)              // Loop to print sorted array
    {
        printf("%d ", arr[i]);          // Print each sorted element
    }

    return 0;                           // End of program
}

πŸ“€ Output

Before Sorting:
50 20 40 10 30

After Sorting (Ascending Order):
10 20 30 40 50

⚡ Time Complexity Analysis



Why?

Traversal checks all elements.

Updation directly accesses index.

Bubble Sort uses nested loops, so it becomes slower for large data.


πŸ”₯ Difference Between Traversal and Updation



This is a very common interview question.


❌ Common Mistakes Beginners Make

1. Index Out of Bound

Wrong:

arr[5]

Correct:

arr[4]

because indexing starts from 0.


2. Forgetting Loop Limits

Wrong loop conditions can cause runtime errors.

Always verify:

for(i = 0; i < n; i++)

3. Wrong Swap Logic in Sorting

Many beginners forget the temporary variable.

Correct:

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

🌍 Real-Life Applications of Arrays

Arrays are used in almost every software system.

Examples

  • Student marks management system

  • Banking transaction records

  • Employee salary management

  • Inventory systems

  • Mobile contact lists

  • Sensor data storage

  • Image processing

  • Matrix operations

  • Game score tracking

Arrays are the backbone of many advanced systems.


🎯 Interview Questions

Q1. What is array traversal?

Traversal means visiting each element one by one.


Q2. What is array updation?

Changing the value of an existing element.


Q3. Why is sorting important?

Sorting improves searching speed and organizes data efficiently.


Q4. Why is Bubble Sort slow?

Because it uses nested loops and takes O(n²) time.


Q5. Which is faster: traversal or updation?

Updation is faster because it takes O(1) time.


❓ Frequently Asked Questions (FAQs)

Is Bubble Sort used in real projects?

Rarely for large systems because it is slow, but it is excellent for learning DSA concepts.


Can arrays store different data types?

No. Arrays store only same data type elements.


Why does array index start from 0?

Because memory address calculations become simpler and faster.


Which comes after arrays in DSA?

Usually:

Linked List → Stack → Queue → Trees → Graphs


🏁 Conclusion

Arrays are the foundation of Data Structures and Algorithms.

In this blog, we learned:

✔ Traversal
✔ Updation
✔ Sorting
✔ Bubble Sort
✔ Time Complexity
✔ Common Mistakes
✔ Interview Questions
✔ FAQs

If your array concepts are strong, learning advanced DSA becomes much easier.


πŸ“Œ Keep Learning. Keep Coding. Keep Growing.

✨ Written by Krishna Popat
🌱 Founder – Learning Growth Hub

Comments

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”