Docsity

Array Operations in Data Structures and Algorithms (DSA) – Complete Notes with C Code,, Study notes of Computer Science

Partial preview of the text

Download Array Operations in Data Structures and Algorithms (DSA) – Complete Notes with C Code, and more Study notes Computer Science in PDF only on Docsity!

DATA STRUCTURES

Array Operations — Complete Notes

(Language used for code: C)

Traversal • Insertion • Deletion • Linear Search • Updating • Time Complexity • Solved Problems

A Complete Study Guide with Diagrams and Solved Examples for CS Students

Contents

1. Overview of Array Operations

2. Traversal

3. Insertion

3.1 Insertion at Beginning 3.2 Insertion at End 3.3 Insertion at Specific Position

4. Deletion

4.1 Deletion at Beginning 4.2 Deletion at End 4.3 Deletion at Specific Position

5. Searching (Linear Search)

6. Updating Elements

7. Time Complexity Summary Table

8. Solved Examples (5 Fully Worked Problems)

3. Insertion

Insertion means adding a new element to the array. Depending on where the new element is placed,

the operation behaves very differently in terms of speed.

3.1 Insertion at the Beginning

To insert a new element at index 0, every existing element must first be shifted one position to the right

to make room. This is the most expensive insertion case because the maximum number of elements

must move.

void insertAtBeginning(int arr[], int *n, int value) { for (int i = n; i > 0; i--) { arr[i] = arr[i - 1]; // shift every element right } arr[0] = value; (n)++; }

Time Complexity: O(n) — worst case, since all n elements shift.

3.2 Insertion at the End

If there is free space available at the end of the array (i.e., the array is not already full), inserting a new

element there simply means placing the value at index n and increasing the count — no shifting is

required at all.

void insertAtEnd(int arr[], int n, int value) { arr[n] = value; // place directly, no shifting needed (*n)++; }

Time Complexity: O(1) — assuming space is already available at the end.

3.3 Insertion at a Specific Position

To insert at any arbitrary position (index k), all elements from index k to the end must shift one step to

the right before placing the new value at index k.

Diagram: Insertion at a specific position

Before: insert 99 at index 2

0

1

2

3

4 5 After: elements shifted right, 99 placed at index 2

0

1

2

3

4

5 Figure 2: Inserting 99 at index 2 shifts elements 7, 23, 9 one position to the right.

void insertAtPosition(int arr[], int *n, int pos, int value) { for (int i = *n; i > pos; i--) { arr[i] = arr[i - 1];

arr[pos] = value; (*n)++; }

Time Complexity: O(n) in the worst case (inserting near the beginning); O(1) best case (inserting at the

very end).

Time Complexity: O(n) worst case (deleting near the beginning); O(1) best case (deleting the last

element).

5. Searching (Linear Search)

Linear Search (also called Sequential Search) is the most basic searching technique. It checks every

element of the array one by one, starting from index 0, until either the target value is found or the entire

array has been scanned.

Algorithm:

  • Step 1: Start from index 0.
  • Step 2: Compare the current element with the target (key) value.
  • Step 3: If it matches, return the current index (search successful).
  • Step 4: If not, move to the next index and repeat.
  • Step 5: If the end of the array is reached without a match, return -1 (search unsuccessful).

Diagram: Linear Search in progress

0

1

2

3

4

match found!

Figure 4: Searching for value 23 — the search scans index 0, 1, 2 before finding a match at index 3.

int linearSearch(int arr[], int n, int key) { for (int i = 0; i < n; i++) { if (arr[i] == key) { return i; // found, return index } } return -1; // not found }

Best, Average, and Worst Case:

Case Condition Time Complexity

Best Case Target is the very first element O(1)

Average Case Target is somewhere in the middle O(n)

Worst Case Target is the last element, or not present at all O(n)

6. Updating Elements

Updating means changing the value stored at a known index. Because array elements support direct,

random access via the address formula (Base Address + index × element size), updating any element

takes exactly the same amount of time, no matter which index is chosen.

void updateElement(int arr[], int index, int newValue) { arr[index] = newValue; }

7. Time Complexity Summary Table

Operation Best Case Worst Case Notes

Traversal O(n) O(n) Every element must be visited

Access / Update by index O(1) O(1) Direct address calculation

Insertion at end O(1) O(1) No shifting needed (space available)

Insertion at beginning O(n) O(n) All elements shift right

Insertion at position k O(1) O(n) Depends on how many elements are after k

Deletion at end O(1) O(1) No shifting needed

Deletion at beginning O(n) O(n) All elements shift left

Deletion at position k O(1) O(n) Depends on how many elements are after k

Linear Search O(1) O(n) Depends on position of target element

8. Solved Examples

This section contains fully worked, step-by-step solved problems — the most valuable part of these

notes for exam preparation.

Solved Example 1: Insert an element at a given position

Problem: Given the array [10, 20, 30, 40] with n = 4, insert the value 25 at index 2.

Step-by-step solution:

  • Original array: [10, 20, 30, 40], indices 0 1 2 3.
  • Shift arr[3] (value 40) to arr[4]: array becomes [10, 20, 30, 40, 40].
  • Shift arr[2] (value 30) to arr[3]: array becomes [10, 20, 30, 30, 40].
  • Place 25 at arr[2]: array becomes [10, 20, 25, 30, 40].
  • Increase n to 5.

Final Answer: [10, 20, 25, 30, 40]

Solved Example 2: Delete an element from a given position

Problem: Given the array [5, 8, 12, 19, 21] with n = 5, delete the element at index 1.

Step-by-step solution:

  • Original array: [5, 8, 12, 19, 21].
  • Shift arr[2] (12) to arr[1]: array becomes [5, 12, 12, 19, 21].
  • Shift arr[3] (19) to arr[2]: array becomes [5, 12, 19, 19, 21].
  • Shift arr[4] (21) to arr[3]: array becomes [5, 12, 19, 21, 21].
  • Decrease n to 4, so the valid array is [5, 12, 19, 21].

Final Answer: [5, 12, 19, 21]

Solved Example 3: Linear Search — count comparisons

Problem: In the array [4, 17, 9, 23, 6, 15], search for the value 23 using Linear Search. How many

comparisons are made?

Step-by-step solution:

  • Compare arr[0]=4 with 23^ →^ No match (comparison 1).
  • Compare arr[1]=17 with 23^ →^ No match (comparison 2).
  • Compare arr[2]=9 with 23^ →^ No match (comparison 3).
  • Compare arr[3]=23 with 23^ →^ Match found! (comparison 4).

Final Answer: Element found at index 3, after 4 comparisons.