Search This Blog




running time of algorithms


Please refer this link for problem statement click here




Javascript program for running time of algorithms


function runningTime(a) {
    let shifts = 0, pos;

    for (let i = 1; i < a.length; i++) {
        pos = i,
        j = i - 1,
        key = a[pos];
        
        while (j >= 0 && a[j] > key) {
            shifts += 1;
            a[j + 1] = a[j];
            j--;
        }

        a[j + 1] = key;
    }

    return shifts;
}





Time Complexity : O(n^2)

Space Complexity : O(1)



Post a Comment

0 Comments