AfterAcademy Tech
•
17 Dec 2019

Often in an algorithm, a group of statements needs to be executed again and again until a certain condition is met, this is where we find the need for iteration.
The repeated execution of some groups of code statements in a program is called iteration.
We will be exploring the following concepts in Iteration:
Loops are tools provided by programming languages to implement iteration. Generally, we use two different types of loops:
Note: Some languages there is an additional loop, i.e. a do-while loop.
for loop
This involves initializing the variable, defining the loop condition and implementing the increment/decrement statements.
for(loop initialization, loop condition, increment/decrement)
{
// loop body
}
Loop body: A single statement or a block of statements.
Loop condition: any expression, and true is any nonzero value. The loop iterates while the loop condition is true.
while loop
A while loop repeatedly executes a target statement as long as a given condition is true. This requires pre-initialization of loop variables (if any), defining loop condition, writing loop body and increment or decrement statements.
loop variable initialization
while (loop condition)
{
// loop body
// loop variable increment/decrement
}
Iteration Example: Increment the loop by 1
Find the maximum element in an array.
int largest(int A[], int n)
{
int i
int max = A[0]
for (i = 1; i < n; i++)
{
if (A[i] > max)
max = A[i]
}
return max
}
Iteration Example: Two pointer approach
Reverse an array.
void reveseArray(int A[], int start, int end)
{
while (start < end)
{
int temp = A[start]
A[start] = A[end]
A[end] = temp
start = start + 1
end = end - 1
}
}
Iteration Example: Increment the loop by 2
Sort an array in wave form
void sortWave(int A[], int n)
{
for (int i = 0; i < n; i=i+2)
{
if (i>0 && A[i-1] > A[i] )
swap(A[i], A[i-1])
if (i<n-1 && A[i] < A[i+1] )
swap(A[i], A[i+1])
}
}
Iteration Example: Exponential Increment
Print powers of 2 uptil 2^n
void printPowersOf2(int n)
{
for ( int i = 1; i <= pow(2,n); i = i*2)
print(i)
}
Iteration Example: Nested loops
Insertion sort in an array
void insertionSort(int A[], int n)
{
for(int i = 1; i <= n-1; ++i )
{
int key = A[i]
int j = i-1
while (j >= 0 and A[j] > key )
{
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
Iteration Example: Two pointer approach
Merge two sorted arrays
void mergeSortedArrays(int A[], int B[], int n1,
int n2, int C[])
{
int i = 0, j = 0, k = 0
while (i<n1 && j <n2)
{
if (A[i] < B[j])
{
C[k] = A[i]
k = k + 1
i = i + 1
}
else
{
C[k] = B[j]
k = k + 1
j = j + 1
}
}
while (i < n1)
{
C[k] = A[i]
k = k + 1
i = i + 1
}
while (j < n2)
{
C[k] = B[j]
k = k + 1
i = i + 1
}
}
Complexity Analysis in an iteration is relatively simpler compared to recursion. We basically need to calculate the total number of operations performed by the loop in the worst case with respect to the input size (conventionally denoted by n).
We use loop invariants to verify the correctness of the iterative algorithms. We must show three things about a loop invariants:
Example: Given a sorted array of n integers and given a number K, determines whether there is a pair of elements in the array that sums to exactly K.

Note: The choice of the loop is purely based on conventions and can be chosen either way according to the convenience of the programmer.
2. Sometimes we use a loop variable to count how many times the loop runs. We need to initialize and define this loop variable. Also, define the increment/decrement condition for the loop variable.
3. Define the condition which terminates the loop
Note: Using a loop variable is optional while coding iteration. Iteration can be terminated based on a predefined state of some existing variable too.
Iteration methodology has various applications in the programming world →
Happy Coding, Enjoy Algorithms!
AfterAcademy Tech
Iteration and Recursion are programming methodologies with similar objective and thus one might seem trivial compared to another, but that's not the case. We will be discussing the important differences between iteration and recursion and how both are useful.

AfterAcademy Tech
Dynamic Programming is one of the most commonly used approach of problem-solving during coding interviews. We will discuss how can we approach to a problem using Dynamic Programming and also discuss the concept of Time-Memory Trade-Off.

AfterAcademy Tech
In this blog, we will see how to get started with Competitive Programming. Competitive Programming is very important because in every interview you will be asked to write some code. So, let's learn together.

AfterAcademy Tech
Divide and Conquer and Dynamic Programming are two most used problem-solving skills in Data Structures. In this blog, we will see the similarities and difference between Dynamic Programming and Divide-and-Conquer approaches.
