Counting cross lines in an array | Java program to count cross line in array ; function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver Code
class GFG { static int countCrossLine ( int arr [ ] , int n ) { int count_crossline = 0 ; int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } public static void main ( String args [ ] ) { int arr [ ] = new int [ ] { 4 , 3 , 1 , 2 } ; int n = arr . length ; System . out . print ( countCrossLine ( arr , n ) ) ; } }