Sort an almost-sorted, k-sorted or nearly-sorted array28 Aug 2024 | 10 min read In this article, we will learn to sort an almost sorted array in detail. What is meant by sorting an almost sorted array?When we can sort an array either by
Then it is considered as the sort of an almost-sorted, k-sorted or nearly-sorted array. For example: The above-given arrays are almost sorted, some of the elements in both of them are either misplaced by k positions, or a few elements are placed in reverse order. The simple idea would be to sort the array using the best sorting algorithm, like merge sort in O(n logn) time. But there are more efficient ways to sort an almost sorted array. The best possible approaches to sort the almost sorted array are discussed below: Approach 1:By using insertion sort, the time complexity of this approach is T(n) = O(n k), where n is the size of the problem or array, the outer for-loop runs up to n times, and the inner while-loop runs a maximum of k times. Space complexity = O(1) as there is no demand for extra space. The programs to sort the almost sorted array in different programming languages are given below: C++ CodeSample Output: Sorted Array = {3, 4, 5, 6, 7, 8, 9, 10, 13}
C code:Sample Output: Sorted Array = {21, 22, 23, 24, 32, 33, 34, 35, 36}
Python code:Sample Output: Sorted array = [5, 6, 7, 9, 10, 11, 12, 13, 15] Approach 2:By using a min-heap, the above problem can be solved with the help of a min-heap. In this approach, we use the idea of a min-heap. In step first step - we create a min-heap of size k+1 and insert the first k+1 elements in it. We have created a min-heap of size k+1 because the elements can be found at a k-distance apart from their actual position in the sorted array. This process takes O(k) time. In the second step - We remove the smallest element from the heap and insert it into the array. After each removal of an element from the heap, we insert the next element from the array into the heap and repeat the same process for the n-k elements. The method of addition and removal of elements takes O(log k) time. The actual time complexity of the program becomes equal to the T(n) = O(k) + O(n-k) * O(log k) = O(m * log k) where m = n-k. Sometimes, it is considered to be T(n) @ O(n * log k). C++ Code:Sample Output: Original array = 5 7 4 6 12 8 9 10 Sorted array = 4 5 6 7 8 9 10 12 Time Complexity: T(n) = O(k) + O(n-k) * O(log k) = O(m * log k) where m = n-k Space Complexity: = O(k) The same problem with the same time complexity can be solved using the AVL tree (Self-Balancing tree) as insertion and deletion can be performed in O(log k) time. Its overall cost becomes O(n logk). But min-heap is preferred over the AVL tree as it does not require any extra space for the left and right pointers and it is less complex than the AVL tree. Approach 3The above problem can be solved using the quick sort algorithm to achieve a better time complexity than the above approach. If you don't have the idea of working on the quick sort algorithm. Please go through the given link to learn the technicalities of the quick sort algorithm. < https://www.javatpoint.com/quick-sort> In this approach, we follow two ideas:
The idea of selecting the mid element as the pivot two divide the array into two halves for better logarithmic time complexity. The implementation of this approach is given below: C++ code:Sample Output: Given array = 2 5 2 4 4 6 7 7 12 12 10 9 8 Sorted array = 2 2 4 4 5 6 7 7 8 9 10 12 12 C Code:Sample Output: Given array = 6 5 5 3 1 3 1 11 14 12 12 13 Sorted array = 1 1 3 3 5 5 6 11 12 12 13 14 Time complexity: T(n) = O(k Logn) Space complexity: O(Logk) We can observe from the time and space complexity that this approach is better than the previous two approaches. Next TopicFibonacci Heap |
What is iteration in data structure
? Iteration refers to repeating a certain number of steps continuously until a particular condition is met successfully. The iterations can be an infinite number of times or an infinite number of times. It all depends on the program in which we are performing the iterations. Iterations play...
21 min read
Disjoint set data structure
The disjoint set data structure is also known as union-find data structure and merge-find set. It is a data structure that contains a collection of disjoint or non-overlapping sets. The disjoint set means that when the set is partitioned into the disjoint subsets. The various operations...
9 min read
Different Operation on Matrix Data Structure using Python
This article will delve into operations on matrices using Python and NumPy. It will cover matrix concepts their representations in Python well as operations like addition, subtraction, multiplication, transposition and more. Advanced topics such as matrix decompositions solving linear equations systems and other problems will also...
16 min read
What is a Non-Linear Data Structure
What is a non-linear data structure? Data Structure A data structure is a special way of organizing the data elements into a particular form. The arrangement of data in a particular order is very important to access the particular data element in less time easily without putting...
23 min read
Flatten Binary Tree to Sorted Linked List
A flattened binary tree is a changed version of the normal binary tree, as all the nodes present are rearranged to create a linear structure. All the nodes in the tree are organized so that when traversing the tree from left to right, we observe the...
6 min read
Cocktail Sort
Algorithm In this article, we will discuss the cocktail sort algorithm. Cocktail sort is the variation of Bubble Sort, which traverses the list in both directions alternatively. It is different from bubble sort in the sense that bubble sort traverses the list in the forward direction...
10 min read
What is the Balance Factor of AVL Tree
? AVL Tree In 1962, GM Adelson-Velsky and EM Landis created the AVL Tree. To honors the people who created it, the tree is known as AVL. The definition of an AVL tree is a height-balanced binary search tree in which each node has a balance factor that is...
4 min read
Implement Dynamic Deque using Templates Class and a Circular Array
A Deque, also known as a Double Ended Queue, is a type of Queue in which insertion and deletion are possible from both the front and back sides. A deque is a data structure that combines the capabilities of stacks and queues into a single data...
5 min read
Inorder Predecessor and Successor in a Binary Search Tree
Binary search trees (BSTs) are a famous data structure that stores data in a way that allows quick lookups, insertions, and deletions. An important concept when working with BSTs is finding the in-order predecessor and successor of a node. The inorder predecessor of a node is...
12 min read
How to efficiently implement k stacks in a single array
Introduction The ability to effectively manage and use memory is crucial when programming with multiple stacks. We will look at how to implement K stacks in a single array in this article, making sure to use the least amount of memory and perform operations as quickly as...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India