Arrays
[Link] Kumar
Ap/DSBS
5 (a) : Find the Largest Element in an Array
Aim: To find the largest element in a given array of numbers.
Algorithm:
Start.
Read size n of the array.
Input n elements into the array.
Initialize max = arr[0].
Traverse the array:
If arr[i] > max, update max = arr[i].
Print max.
Stop.
5(b) : Reverse an Array
Aim:To reverse the elements of an array.
Algorithm:
Start.
Read size n of the array.
Input n elements into the array.
Initialize two variables i=0, j=n-1.
Swap arr[i] and arr[j] while i < j.
Print reversed array.
Stop.
Reverse Logic
arr = [10, 20, 30, 40, 50]
n=5
We reverse by swapping elements from both ends, moving toward the
middle.
Loop runs till n/2 = 5/2 = 2 (only 2 iterations needed).
Step:1
Iteration 1 (i = 0):
temp = arr[0]; → temp = 10
arr[0] = arr[n - 0 - 1]; → arr[0] = arr[4] = 50
arr[4] = temp; → arr[4] = 10
Array after step 1: [50, 20, 30, 40, 10]
…..
Step 2
Iteration 2 (i = 1):
temp = arr[1]; → temp = 20
arr[1] = arr[n - 1 - 1]; → arr[1] = arr[3] = 40
arr[3] = temp; → arr[3] = 20
Array after step 2: [50, 40, 30, 20, 10]
Iteration 3 (i = 2):
Condition check: i < n/2 → 2 < 2 → false, loop stops.
5 C: Sum of Elements in an Array
Aim:To calculate the sum of all elements in an array.
Algorithm:
Start.
Read size n of array.
Input array elements.
Initialize sum = 0.
For each element, add to sum.
Print sum.
Stop.
Logic explanation
n=4
arr = [5, 10, 15, 20]
sum = 0
Loop Execution:
Iteration 1 (i = 0):
arr[0] = 5
sum += arr[i]; → sum = sum + arr[0]
sum = 0 + 5 = 5
Now sum = 5
………
Iteration 2 (i = 1):
arr[1] = 10
sum = sum + arr[1] sum = 5 + 10 = 15
Now sum = 15
Iteration 3 (i = 2):
arr[2] = 15
sum = sum + arr[2]
sum = 15 + 15 = 30
Now sum = 30
………..
Iteration 4 (i = 3):
arr[3] = 20
sum = sum + arr[3] sum = 30 + 20 = 50
Now sum = 50
5d: Search an Element in an Array
Aim:To search for an element in an array using linear search.
Algorithm:
Start.
Read size n of array.
Input array elements.
Read search element key.
Traverse array:
If arr[i] == key, print position and stop.
If not found, print "Not Found".Stop.
Compares the current element arr[i] with the search element
key.
(// flag to indicate success).
Prints the value of the element and its position (using i + 1 because array indexes
start from 0, but users expect position starting from 1).
If found is still 0 (false), it means the element was never found
in the array.
5e: Sort an Array in Ascending Order
Aim:To sort the elements of an array in ascending order.
Algorithm:
Start.
Read size n of array.
Input elements.
Use bubble sort:
Repeat n-1 times.
Swap adjacent elements if arr[j] > arr[j+1].
Print sorted array.
Stop.
Logic explanation
Nested For loop
for (i = 1; i <= 2; i++) // runs for 2 times
{
for (j = 1; j <= 3; j++) // runs for 3 times
{
printf("i=%d, j=%d\n", i, j);
}
}
For each value of i, the entire inner loop (j) runs.
Then i increments, and the inner loop runs again
Step Outer i Inner j Printed Output
1 1 1 i=1 j=1
2 1 2 i=1 j=2
3 1 3 i=1 j=3
4 2 1 i=2 j=1
5 2 2 i=2 j=2
6 2 3 i=2 j=3
Let’s build a table for the bubble sort example
arr = [5, 3, 8, 4, 2]
n=5
Bubble Sort Idea
• In bubble sort, the largest element moves to the
end in the first pass.
• Second largest element moves to second last
position in the second pass.… and so on.
• After n - 1 passes, all elements are already sorted.
• So, there is no need for the nth pass
……..
for (i = 0; i < n - 1; i++) { // Outer loop → number of passes
for (j = 0; j < n - i - 1; j++) { // Inner loop → comparisons
if (arr[j] > arr[j + 1])
{
// swap
}
}
}
The outer loop controls the number of passes needed to fully sort the
array
i<n–1
Pass Concept
In pass 1, the largest element goes to last position.
In pass 2, the second largest goes to second last position.… and so on.
After n - 1 passes, the last remaining element is already in correct place
(no need for the nth pass).
Efficiency Saves one unnecessary pass.
If we wrote i < n, it would still work, but the last pass would do 0 useful
swaps.
Example: Array = [5, 3, 4, 1, 2] (n = 5)
Outer loop runs: i = 0, 1, 2, 3 → 4 passes (n-1)
Pass 1 (i=0)
Largest element 5 goes to last position → [3, 4, 1, 2, 5]
Pass 2 (i=1)
Next largest 4 goes to 2nd last → [3, 1, 2, 4, 5]
Pass 3 (i=2)
Next largest 3 goes to 3rd last → [1, 2, 3, 4, 5]
Pass 4 (i=3)
Confirms sorting → [1, 2, 3, 4, 5]
After 4 passes (n-1), array is sorted. No 5th pass needed.
………
• The inner loop condition in bubble sort
j<n-i–1
Example (n = 5, array = [5, 3, 4, 1, 2])
Pass 1 (i = 0):
j < n - 0 - 1 = 4 → j = 0,1,2,3
Comparisons: (arr[0] vs arr[1]), (arr[1] vs arr[2]), (arr[2] vs arr[3]), (arr[3]
vs arr[4])
Pass 2 (i = 1):
j < n - 1 - 1 = 3 → j = 0,1,2
Comparisons: only first 3 pairs checked. Last element already sorted.
………
Pass 3 (i = 2):
j < n - 2 - 1 = 2 → j = 0,1
Comparisons: only first 2 pairs checked. Last 2 elements sorted.
Pass 4 (i = 3):
j<n-3-1=1→j=0
Only 1 comparison needed.