Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to print inversions pairs in array in C#

1 Answer

0 votes
// Inversion for an array indicates how far (or close) the array is from being sorted. 
// If the array is sorted then the Inversion is 0.
 
using System;
 
class Program
{
    static void printInversionPair(int[] arr) {
        int size = arr.Length;
        if (size <= 1) {
            return;
        }
        bool sorted = true;
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (arr[i] > arr[j]) {
                    sorted = false;
                    Console.WriteLine(arr[i] + ", " + arr[j]);
                }
            }
        }
        if (sorted)
            Console.WriteLine("sorted");
    }
    static void Main() {
        int[] arr1 = {1, 7, 2, 5, 4, 3, 9, 8};
  
        printInversionPair(arr1);
          
        int[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
          
        printInversionPair(arr2);
    }
}
 
 
 
 
/*
run:
    
7, 2
7, 5
7, 4
7, 3
5, 4
5, 3
4, 3
9, 8
sorted
    
*/

 



answered Nov 10, 2021 by avibootz
edited Nov 10, 2021 by avibootz

Related questions

1 answer 158 views
1 answer 169 views
169 views asked Nov 10, 2021 by avibootz
1 answer 198 views
1 answer 143 views
1 answer 112 views
1 answer 102 views
1 answer 174 views
...