githubEdit

22. Nearly Sorted

βœ… GFG solution to the Nearly Sorted Array problem: efficiently sort an array where each element is at most k positions away from its target position using min heap. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given an array arr[], where each element is at most k positions away from its correct position in the sorted order. Your task is to restore the sorted order of arr[] by rearranging the elements in place.

Note: Don't use any sort() method.

A nearly sorted array is one where each element is displaced by at most k positions from where it would be in a fully sorted array. The goal is to efficiently sort this array by leveraging this property.

πŸ“˜ Examples

Example 1

Input: arr[] = [2, 3, 1, 4], k = 2
Output: [1, 2, 3, 4]
Explanation: All elements are at most k = 2 positions away from their correct positions.
Element 1 moves from index 2 to 0
Element 2 moves from index 0 to 1
Element 3 moves from index 1 to 2
Element 4 stays at index 3

Example 2

πŸ”’ Constraints

  • $1 \le \text{arr.size()} \le 10^6$

  • $0 \le k < \text{arr.size()}$

  • $1 \le \text{arr}[i] \le 10^6$

βœ… My Approach

The optimal approach uses a Min Heap (Priority Queue) to efficiently sort the nearly sorted array by maintaining a window of k+1 elements:

Min Heap Approach

  1. Initialize Min Heap:

    • Create a min heap and insert the first k+1 elements from the array.

    • These k+1 elements contain the smallest element that should be placed at index 0.

  2. Process Remaining Elements:

    • For each remaining element in the array:

      • Extract the minimum from the heap and place it at the current sorted position.

      • Insert the next element from the array into the heap.

      • This maintains a sliding window of k+1 elements.

  3. Empty the Heap:

    • After processing all elements, extract remaining elements from the heap.

    • Place them in their correct sorted positions.

  4. Key Insight:

    • Since each element is at most k positions away, the smallest element in any window of k+1 consecutive elements belongs at the start of that window.

    • Using a min heap ensures O(log k) insertion and extraction operations.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n log k), where n is the size of the array. We perform n insertions and n deletions on a heap of maximum size k+1, each operation taking O(log k) time.

  • Expected Auxiliary Space Complexity: O(k), as the min heap stores at most k+1 elements at any given time. This is constant relative to the array size when k is small.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Insertion Sort Optimized

πŸ’‘ Algorithm Steps:

  1. Use insertion sort with limited backward search up to k positions.

  2. Each element is compared with at most k previous elements.

  3. Insert element at correct position within its k-range window.

  4. Efficiently sorts the nearly sorted array in-place.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n*k) - Each element moves at most k positions

  • Auxiliary Space: πŸ’Ύ O(1) - In-place sorting

βœ… Why This Approach?

  • No extra space required for data structures

  • Simple implementation without heap operations

  • Works well when k is very small

πŸ“Š 3️⃣ STL Partial Sort

πŸ’‘ Algorithm Steps:

  1. Divide array into overlapping chunks of size 2k+1.

  2. Sort each chunk using STL sort function.

  3. Elements settle into their correct positions due to overlap.

  4. Handles the nearly sorted property efficiently.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(nklog(k)) - Sorting overlapping chunks

  • Auxiliary Space: πŸ’Ύ O(1) - In-place sorting

βœ… Why This Approach?

  • Leverages highly optimized STL sort

  • Easy to implement and understand

  • Good balance between simplicity and performance

πŸ“Š 4️⃣ Multiset Window Approach

πŸ’‘ Algorithm Steps:

  1. Maintain a sliding window of k+1 elements using multiset.

  2. Extract minimum from multiset and place in sorted position.

  3. Add new element and remove oldest from window.

  4. Balanced tree structure ensures efficient operations.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n*log(k)) - Balanced tree operations

  • Auxiliary Space: πŸ’Ύ O(k) - Multiset storage

βœ… Why This Approach?

  • Maintains sorted order automatically

  • Efficient insertion and deletion

  • Good alternative to priority queue

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🏷️ Min Heap

🟒 O(n*log(k))

🟑 O(k)

πŸš€ Optimal for large arrays

πŸ’Ύ Extra space for heap

πŸ”§ Insertion Sort

🟑 O(n*k)

🟒 O(1)

πŸ“¦ No extra space

🐌 Slower for large k

πŸ“Š Partial Sort

🟑 O(nklog(k))

🟒 O(1)

🎯 Simple implementation

πŸ”§ Not optimal complexity

🌲 Multiset

🟒 O(n*log(k))

🟑 O(k)

βš–οΈ Self-balancing structure

πŸ’Ύ Similar space as heap

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… General purpose optimal

πŸ₯‡ Min Heap

β˜…β˜…β˜…β˜…β˜…

πŸ“¦ Memory constrained

πŸ₯ˆ Insertion Sort

β˜…β˜…β˜…β˜…β˜†

🎯 Small k values

πŸ₯‰ Insertion Sort

β˜…β˜…β˜…β˜…β˜…

πŸ”„ Need sorted structure

πŸ… Multiset

β˜…β˜…β˜…β˜…β˜†

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated