Running Time of Algorithms

  • [deleted]
    + 0 comments

    My solution :

    public static int runningTime(List<Integer> arr) {
        // Write your code here
            int n = arr.size();
            int noOfShifts = 0;
            
            for(int i=0; i<n-1; i++){
                int first = arr.get(i);
                int second = arr.get(i+1);
                if(first > second){
                    arr.set(i, second);
                    arr.set(i+1, first);
                    noOfShifts++;
                    if(i-1 > -1){ i-=2; }
                }
            }
            return noOfShifts;
        }