Data Structures – Chapter 1: Arrays
(with Line-by-Line Explanation)
1. Traversal of Array
Algorithm Step Explanation
Start → Begin the algorithm
Set i = 0 → i is the index pointer (starts from 0)
Repeat step 3.1 while i < n → Loop through array until end
3.1) Print arr[i] → Show current element
3.2) i = i + 1 → Move to next index
end loop → End of loop
Exit → End of algorithm
2. Insertion at Position i
Algorithm Step Explanation
Start → Begin the algorithm
If n ≥ max then → If array is full
Print 'Array Full' → Cannot insert
Exit → Exit program
end if
If i < 0 or i > n then → Check valid index
Print 'Invalid Index' → Cannot insert at wrong position
Exit → Exit program
end if
Set j = n - 1 → Start shifting from last element
Repeat step 5.1 while j ≥ i → Keep shifting until position i
5.1) arr[j+1] = arr[j] → Shift element right
5.2) j = j - 1 → Move one step back
end loop
arr[i] = item → Place the new item at index i
n=n+1 → Increase array size
Exit → End of algorithm
3. Deletion at Position i
Algorithm Step Explanation
Start → Begin the algorithm
If n == 0 then → Check if array is empty
Print 'Array Empty' → Cannot delete
Exit → Exit program
end if
If i < 0 or i ≥ n then → Check valid index
Print 'Invalid Index' → Cannot delete
Exit → Exit program
end if
Set j = i → Start shifting from deleted index
Repeat step 5.1 while j < n - 1 → Shift all right elements to left
5.1) arr[j] = arr[j+1] → Replace with next value
5.2) j = j + 1 → Go to next index
end loop
n=n-1 → Decrease array size
Exit → End of algorithm
4. Linear Search
Algorithm Step Explanation
Start → Begin the search
Set i = 0, flag = 0 → i for index, flag for found/not
Repeat step 3.1 while i < n → Check all elements
3.1) If arr[i] == key then → If current element matches
Print 'Found at', i → Show position
flag = 1 → Mark as found
Exit → End search
end if
3.2) i = i + 1 → Go to next element
end loop
If flag == 0 then → If not found in full array
Print 'Not Found' → Show not found
end if
Exit → End algorithm
5. Binary Search (Sorted Array)
Algorithm Step Explanation
Start → Begin the algorithm
Set low = 0, high = n - 1 → Start with full array range
Repeat while low ≤ high → Continue until range is valid
3.1) mid = (low + high) // 2 → Find middle index
3.2) If arr[mid] == key then → If key is found
Print 'Found at', mid → Show position
Exit → End algorithm
end if
3.3) If key < arr[mid] then → If key is smaller
high = mid - 1 → Search left half
else → Otherwise
low = mid + 1 → Search right half
end if
end loop
Print 'Item Not Found' → If key not found
Exit → End algorithm
6. Bubble Sort
Algorithm Step Explanation
Start → Begin the sorting
Repeat for i = 0 to n - 1 → Outer loop for passes
Repeat for j = 0 to n - i - 2 → Inner loop to compare
If arr[j] > arr[j+1] then → If elements are out of order
Swap arr[j], arr[j+1] → Swap them
end if
end loop
end loop
Exit → End sorting
7. Selection Sort
Algorithm Step Explanation
Start → Begin sorting
Repeat for i = 0 to n - 2 → Outer loop for positions
Set min = i → Assume current index is minimum
Repeat for j = i + 1 to n - 1 → Search rest of array
If arr[j] < arr[min] then → If smaller found
min = j → Update min index
end if
end loop
Swap arr[i], arr[min] → Swap current with smallest
end loop
Exit → End sorting
8. Sum of Array Elements
Algorithm Step Explanation
Start → Begin summing
Set sum = 0 → Initialize total
Repeat for i = 0 to n - 1 → Loop through each element
sum = sum + arr[i] → Add element to total
end loop
Print sum → Display the total
Exit → End program