Running Time of Algorithms

  • + 0 comments

    This is my Java 8 solution reused the previous challenge method. Feel free to ask me any questions.

    public static int runningTime(List<Integer> arr) {
        int n = arr.size();
        int shifts = 0;
        
        for(int i = 1; i < n; i++) {
            int value = arr.get(i);
            int pos = i - 1;
            
            while(pos >= 0 && value < arr.get(pos)) {
                arr.set(pos + 1, arr.get(pos));
                
                pos--;
                shifts++;
            }
            arr.set(pos + 1, value);
        }
        return shifts;
    }