I just read the concept of insertion sort from CLRS and before proceeding to the pseudo code I tried to implement insertion sort myself :Could you please review and point out the time complexity of this code.    public static void main(String[] args) {    int[] arr = { 7, 6, 5, 4, 3, 2, 1, 32 };    for (int i = 1; i < arr.length; i++) {        int key = arr[i];        for (int k = 0; k < i; k++) {            if (arr[k] >= arr[i]) {                int j = i;                while (j != k) {                    arr[j] = arr[j - 1];                    j--;                }                arr[j] = key;            }        }    }    for (int s : arr) {        System.out.print(s + -);    }}It works fine but-How can I shape my approach while implementation of an algorithm so that I can do justice to time-complexity of the algorithm?I am afraid this might happen during competitive programming, although I have the correct algorithm in mind, I may end up with higher complexity, here its well aware that insertion-sort is O(n^2), but while designing algorithms this may not be the case.Also,please point out if my calculation of time complexity is not up to the mark. Any guidance will be helpful.
Why did I end up with o(n^3) while writing implementation of insertion sort?
java;algorithm;insertion sort