using
System;
using
System.Collections.Generic;
using
System.Linq;
public
class
GFG
{
static
List<
int
> BlockSort(List<
int
> arr,
int
blockSize)
{
List<List<
int
>> blocks =
new
List<List<
int
>>();
for
(
int
i = 0; i < arr.Count; i += blockSize)
{
List<
int
> block =
new
List<
int
>();
for
(
int
j = i; j < i + blockSize && j < arr.Count; j++)
{
block.Add(arr[j]);
}
block.Sort();
blocks.Add(block);
}
List<
int
> result =
new
List<
int
>();
while
(blocks.Any())
{
int
minIdx = 0;
for
(
int
i = 1; i < blocks.Count; i++)
{
if
(blocks[i][0] < blocks[minIdx][0])
{
minIdx = i;
}
}
result.Add(blocks[minIdx][0]);
blocks[minIdx].RemoveAt(0);
if
(!blocks[minIdx].Any())
{
blocks.RemoveAt(minIdx);
}
}
return
result;
}
static
public
void
Main()
{
List<
int
> arr =
new
List<
int
> { 1, 7, 8, 2, 3, 5, 4, 6 };
Console.Write(
"Input: "
);
for
(
int
i = 0; i < arr.Count; i++)
{
Console.Write(arr[i] +
" "
);
}
Console.WriteLine();
int
blockSize = 3;
List<
int
> sortedArr = BlockSort(arr, blockSize);
Console.Write(
"Output: "
);
for
(
int
i = 0; i < sortedArr.Count; i++)
{
Console.Write(sortedArr[i] +
" "
);
}
Console.WriteLine();
}
}