Running Time of Algorithms

  • + 0 comments
    public static int runningTime(List<Integer> list) {
        // Write your code here
            int count = 0;
            for (int i = 0; i < list.size() - 1; i++){
                for (int j = i + 1; j > 0; j--){
                    if (list.get(j - 1) > list.get(j)){
                        swap(list, j-1, j);
                        count++;
                    }
                    else break;
                }
            }
            return count;
        }
    
        private static void swap(List<Integer> list, int a, int b) {
            Collections.swap(list, a, b);
        }