Sort in O(n*k) - C++
Mon Feb 17 2025 03:10:16 GMT+0000 (Coordinated Universal Time)
Saved by
@Rohan@99
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n, k;
cin >> n >> k;
int a[n];
for(int i = 0; i < n; ++i)
{
cin >> a[i];
}
for(int i = 1; i < n; ++i)
{
int key = a[i];
int j = i-1;
while(j >= max(0, i-k) && a[j] > key)
{
a[j+1] = a[j];
--j;
}
a[j+1] = key;
}
for(int i = 0; i < n; ++i)
{
cout << a[i] << " ";
}
return 0;
}
content_copyCOPY
Comments