using
System;
using
System.Collections.Generic;
public
class
Pair
{
public
int
first, second;
public
Pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
public
class
GFG
{
static
void
swap(
int
[] a,
int
i,
int
j)
{
int
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static
void
perform_swap(
int
[] arr,
int
[] position,
List<Pair> answer,
int
x,
int
y)
{
swap(position, arr[x], arr[y]);
swap(arr, x, y);
answer.Add(
new
Pair(x, y));
}
static
void
sortBySwap(
int
N,
int
[] arr)
{
int
[] newArr =
new
int
[N + 1];
for
(
int
i = 0; i < N; i++)
newArr[i + 1] = arr[i];
int
[] position =
new
int
[N + 1];
List<Pair> answer =
new
List<Pair>();
for
(
int
i = 1; i <= N; i++)
position[newArr[i]] = i;
for
(
int
i = 1; i <= N; i++)
{
if
(i == position[i])
continue
;
if
(Math.Abs(i - position[i]) >= N / 2)
{
perform_swap(newArr, position, answer, i,
position[i]);
}
else
{
int
initial_position = position[i];
if
(Math.Abs(position[i] - 1) >= N / 2)
{
perform_swap(newArr, position, answer,
position[i], 1);
if
(Math.Abs(i - 1) >= N / 2)
{
perform_swap(newArr, position,
answer, i, 1);
perform_swap(newArr, position,
answer,
initial_position, 1);
}
else
{
perform_swap(newArr, position,
answer, 1, N);
perform_swap(newArr, position,
answer, N, i);
perform_swap(newArr, position,
answer, 1,
initial_position);
}
}
else
{
perform_swap(newArr, position, answer,
position[i], N);
perform_swap(newArr, position, answer,
N, i);
}
}
}
for
(
int
i = 0; i < answer.Count; i++)
Console.WriteLine(answer[i].first +
" "
+
answer[i].second);
}
static
public
void
Main ()
{
int
[] arr = { 3, 4, 1, 2 };
int
N = arr.Length;
sortBySwap(N, arr);
}
}