Sort a K-Sorted Array Python SolutionLast Updated : 17 Mar 2025 In this problem, we will be given an array of integers. The array will be k sorted. A k-sorted array is one in which every element of the array is at most k steps away from the final sorted array, which is the target sorted array. Let us see some examples to understand the problem Examples: Input: array = [5, 3, 2, 10, 9], K = 3 Output: array = {2, 3, 5, 9, 10} Input: array = [10, 9, 7, 4, 60, 50], k = 4 Output: array = [4, 7, 9, 10, 50, 60] Approach - 1We will use the insertion sort algorithm to solve the problem in this approach. In this approach, the insertion sorting will place each element of the array in the correct place, hence sorting the array. This is an efficient method to solve this problem because we can use the index of every element, which the maximum k number of indices can change. These are the steps that we will follow to solve this problem.
Below is the Python code showing how to use insertion to solve this problem. Code Output: [4, 7, 9, 10, 50, 60] Time Complexity: The time complexity of this approach is non-linear since we are using nested loops. The inner loop will traverse for a maximum of k times. The outer loop will run for every element of the array. Hence, it will run for N times. Therefore, the final time complexity of this approach will be O(N * k). Auxiliary Space: We have not used any extra space to solve this problem; therefore, the space complexity of this approach is O(1). Approach - 2In the second approach, we will consider the property of the k-sorted array. Earlier, we shifted every element to the right whenever the current element was not in the correct order. This is taking extra time because an element shifted x times to the left can return to the same position again if x elements are added on the left of the element. Therefore, in this approach, we will only shift the elements if the current element is more than k positions away from the correct position. This approach will optimize the time complexity of the program. This approach works well because the given array will be almost sorted; hence, we don't need to put every element in its correct order. Already, k operations of sorting are performed. Below is the implementation of this approach. Code Output: 4 7 9 10 50 60 Time complexity: The time complexity of this approach is better than the previous approach. However, the worst-case time complexities of both approaches are the same. The time complexities are the same because there can be a case where each element is k distance away from the correct position. In that case, the inner loop will run for at most k times. Hence, the worst time complexities of both approaches are the same, i.e., O(N * k). Auxiliary Space: We have not used any extra space to solve this problem; therefore, the space complexity of this approach is O(1). Approach - 3In this approach, we will use a heap to solve this problem. We will follow these steps to solve the problem.
Below is the implementation of this approach in Python. Code Output: 4 7 9 10 50 60 Time Complexity: The time complexity of this approach is O(K) + O(m * log(k)) Auxiliary Space: The space complexity of this approach is O(K), which is the space required to store the heap. Next TopicCelebrity-problem-in-python |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India