[AlgoExpert] Sort K-Sorted Array

Sort K-Sorted Array

  • Time : O(nlogk)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
vector<int> sortKSortedArray(vector<int> array, int k) {
array.push_back(INT_MAX);
int n = array.size();
k = min(k, n - 1);
for(int i = 0, j = k + 1; j <= n; j++,i++) {
sort(begin(array) + i, begin(array) + j);
}
array.pop_back();
return array;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/10/PS/AlgoExpert/sort-k-sorted-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.