# Loop Invariant Condition with Examples of Sorting Algorithms

**Loop Invariant Condition:**

Loop invariant condition is a condition about the relationship between the variables of our program which is definitely true immediately before and immediately after each iteration of the loop.

For example: Consider an array A{7, 5, 3, 10, 2, 6} with 6 elements and we have to find maximum element max in the array.

max = -INF (minus infinite) for (i = 0 to n-1) if (A[i] > max) max = A[i]

In the above example after 3rd iteration of the loop max value is 7, which holds true for first 3 elements of array A. Here, the loop invariant condition is that max is always maximum among the first i elements of array A.

**Loop Invariant condition of various algorithms:**

Prerequisite: insertion sort, selection sort, quick sort, bubblesort,

**Selection Sort:**

In selection sort algorithm we find the minimum element from the unsorted part and put it at the beginning.

min_idx = 0 for (i = 0; i < n-1; i++) { min_idx = i; for (j = i+1 to n-1) if (arr[j] < arr[min_idx]) min_idx = j; swap(&arr[min_idx], &arr[i]); }

In the above pseudo code there are two loop invariant condition:

1. In the outer loop, array is sorted for first i elements.

2. In the inner loop, min is always the minimum value in A[i to j].

**Insertion Sort:**

In insertion sort, loop invariant condition is that the subarray A[0 to i-1] is always sorted.

for (i = 1 to n-1) { key = arr[i]; j = i-1; while (j >= 0 and arr[j] > key) { arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; }

**Quicksort:**

In quicksort algorithm, after every partition call array is divided into 3 regions:

1. Pivot element is placed at its correct position.

2. Elements less than pivot element lie on the left side of pivot element.

3. Elements greater than pivot element lie on the right side of pivot element.

quickSort(arr[], low, high) { if (low < high) { pi = partition(arr, low, high); quickSort(arr, low, pi - 1); // Before pi quickSort(arr, pi + 1, high); // After pi } } partition (arr[], low, high) { pivot = arr[high]; i = (low - 1) for (j = low; j <= high- 1; j++) if (arr[j] <= pivot) i++; swap arr[i] and arr[j] swap arr[i + 1] and arr[high]) return (i + 1) }

**Bubble Sort:**

In bubble sort algorithm, after each iteration of the loop largest element of the array is always placed at right most position. Therefore, the loop invariant condition is that at the end of i iteration right most i elements are sorted and in place.

for (i = 0 to n-1) for (j = 0 to j arr[j+1]) swap(&arr[j], &arr[j+1]);

## Recommended Posts:

- Stability in sorting algorithms
- Time Complexities of all Sorting Algorithms
- Asymptotic Analysis and comparison of sorting algorithms
- Sorting Algorithms Visualization : Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Lower bound for comparison based sorting algorithms
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Time taken by Loop unrolling vs Normal loop
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Sorting objects using In-Place sorting algorithm
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sort 3 Integers without using if condition or using only max() function
- Regularity condition in the master theorem.
- Divide N segments into two non-empty groups such that given condition is satisfied
- Time Complexity of Loop with Powers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.