Print Inversions pairs of array in swift
Swift program for Print Inversions pairs of array. Here mentioned other language solution.
import Foundation
/*
Swift 4 program for
Display inversions pairs in array
*/
class InversionPairs
{
// Print the all inversion pairs in given array
func printInversionPair(_ arr: [Int], _ size: Int)
{
if (size <= 1)
{
return;
}
var i: Int = 0;
// Outer loop
while (i < size)
{
var j: Int = i + 1;
// Inner loop
while (j < size)
{
if (arr[i] > arr[j])
{
// When element of i is
// greater than element of j
// Print inversion pair
print("(" + String(arr[i]) + "," +
String(arr[j]) + ")");
}
j += 1;
}
i += 1;
}
}
static func main()
{
let task: InversionPairs = InversionPairs();
// Define array elements
let arr: [Int] = [1, 7, 6, 4, 5, 9, 8];
// Get the number of array elements
let size: Int = arr.count;
// Display inversion pair result
task.printInversionPair(arr, size);
}
}
InversionPairs.main();
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