Print Inversions pairs in array in c++
C++ program for Print Inversions pairs in array. Here more solutions.
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Display inversions pairs in array
*/
class InversionPairs
{
public:
// Print the all inversion pairs in given array
void printInversionPair(int arr[], int size)
{
if (size <= 1)
{
return;
}
// Outer loop
for (int i = 0; i < size; ++i)
{
// Inner loop
for (int j = i + 1; j < size; ++j)
{
if (arr[i] > arr[j])
{
// When element of i is
// greater than element of j
// Print inversion pair
cout << "(" << arr[i] << ","
<< arr[j] << ")" << endl;
}
}
}
}
};
int main()
{
InversionPairs *task = new InversionPairs();
// Define array elements
int arr[] = {
1 , 7 , 6 , 4 , 5 , 9 , 8
};
// Get the number of array elements
int size = sizeof(arr) / sizeof(arr[0]);
// Display inversion pair result
task->printInversionPair(arr, size);
return 0;
}
Output
(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment