Counting cross lines in an array

Given an unsorted array of distinct elements. Task is to count number of cross lines formed in an array elements after sorting the array elements.
Note: Draw a line between same array elements before sorting and after sorting the array elements.

Examples :

Input :  arr[] = { 3, 2, 1, 4, 5 }
Output : 3
      before sort: 3  2  1  4  5
                    \ | /   |  | 
                     \|/    |  |
                    / | \   |  |
      After sort : 1  2  3  4  5 
      line (1 to 1) cross line (2 to 2)
      line (1 to 1) cross line (3 to 3)
      line (2 to 2) cross line (3 to 3)
Note: the line between two 4s and the line 
between two 5s don't cross any other lines; 

Input : arr[] = { 5, 4, 3, 1 }
Output : 6

Simple solution of this problem is based on the insertion sort. we simply pick each array elements one-by-one and try to find it’s proper position in the sorted array.during finding it’s appropriate position of an element we have to cross all the element_line whose value is greater than current element.

Below is the implementation of above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// c++ program to count cross line in array
#include <bits/stdc++.h>
using namespace std;
  
// function return count of cross line in an array
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;
  
        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
  
            // increment cross line by one
            count_crossline++;
        }
        arr[j + 1] = key;
    }
    return count_crossline;
}
  
// driver program to test above function
int main()
{
    int arr[] = { 4, 3, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countCrossLine(arr, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count 
// cross line in array
  
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;
      
            // Move elements of arr[0..i-1], 
            // that are greater than key, 
            // to one position ahead of 
            // their current position 
            while (j >= 0 && arr[j] > key) 
            {
                arr[j + 1] = arr[j];
                j = j - 1;
      
                // increment cross
                // line by one
                count_crossline++;
            }
            arr[j + 1] = key;
        }
          
        return count_crossline;
    
      
    // Driver Code
    public static void main(String args[]) 
    {
        int arr[] = new int[]{ 4, 3, 1, 2 };
        int n = arr.length;
        System.out.print(countCrossLine(arr, n));
    }
}
  
// This code is contributed by Sam007

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count cross line in array
using System;
  
class GFG {
  
    // function return count of cross line
    // in an array
    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;
      
            /* Move elements of arr[0..i-1], 
            that are greater than key, to one
            position ahead of their current
            position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
      
                // increment cross line by one
                count_crossline++;
            }
            arr[j + 1] = key;
        }
          
        return count_crossline;
    
      
    // Driver code
    public static void Main()
    {
        int []arr = new int[]{ 4, 3, 1, 2 };
        int n = arr.Length;
        Console.Write(countCrossLine(arr, n));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count 
// cross line in array
  
// Function return count 
// of cross line in an array
function countCrossLine($arr, $n)
{
      
    $count_crossline = 0;
    $i; $key; $j;
    for ($i = 1; $i < $n; $i++)
    {
        $key = $arr[$i];
        $j = $i - 1;
  
        /* Move elements of arr[0..i-1], that are
           greater than key, to one position ahead
           of their current position */
        while ($j >= 0 and $arr[$j] > $key
        {
            $arr[$j + 1] = $arr[$j];
            $j = $j - 1;
  
            // increment cross line by one
            $count_crossline++;
        }
        $arr[$j + 1] = $key;
    }
    return $count_crossline;
}
  
    // Driver Code
    $arr = array( 4, 3, 1, 2 );
    $n = count($arr);
    echo countCrossLine($arr, $n);
      
// This code is contributed by anuj_67.
?>

chevron_right