Insertion Sort Algorithm

Yasir
1 min readMay 9, 2020

--

Line by Line Explanation for Insertion Sorting Algorithm in C++

Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e, the position to which it belongs in a sorted array.

Line 6 :- Declaring an array of size 5 with their elements .

Line 8 :- selecting every element from the array using for loop .

Line 9 :- Inserting the ith element to temp variable .

Line 10 :- Putting ith -1 index to j variable .

Line 12 :- We are checking for two condition in while loop , first one is that wether j is > = zero and second thing is wether jth element of array is greater then temp or not .

Line 13 :- Swapping and decrementing the j value .

--

--

Yasir
Yasir

Written by Yasir

Exploring Swift, SwiftUI, and Apple Universe.

Responses (1)