Running Time of Algorithms

  • + 0 comments

    java8

        public static int runningTime(List<Integer> arr) {
            int shiftCounter = 0;
            for (int i=1; i < arr.size(); i++) {
                for (int j=0; j < i; j++) {
                    if (arr.get(i) < arr.get(j)) {
                        int toInsert = arr.remove(i);
                        arr.add(j, toInsert);
                        shiftCounter += i-j;
                    }
                }
            }
            return shiftCounter;
        }