Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:13:53+00:00 2026-05-23T22:13:53+00:00

Possible Duplicate: Interview Q: sorting an almost sorted array (elements misplaced by no more

  • 0

Possible Duplicate:
Interview Q: sorting an almost sorted array (elements misplaced by no more than k)

I have a partially sorted array with the property that every element is within d units of its properly sorted position. I am wondering if there is a way to sort this array — by exploiting this fact — in less than n log n time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:13:55+00:00Added an answer on May 23, 2026 at 10:13 pm

    Off the top of my head…

    Create a “sliding window” of size 2d.

    Start it at [0,2d). Sort the elements in the window; now elements 0 through d-1 are guaranteed to be correct.

    Slide the window forward by d, so now it’s [d,3d). Sort those elements, guaranteeing that elements d through 2d-1 are correct.

    Slide it forward another d, so now its [2d,4d). Sort those. And so on.

    Each sort is O(d log d), and it takes n/d steps to get to the end, so this is O(n/d * d log d) = O(n log d). If d is constant, that’s O(n).

    [edit]

    You make a good point in a comment; I did not prove that each iteration preserves the property that every element is within d units of its proper position.

    So…

    Lemma: If A is an array with the property that every element is within d units of its proper position, and you sort any contiguous subsequence within A to create an array A’, then every element in A’ is within d units of its proper position.

    Proof: Since this is a lemma about the properties of a sort (not performance), it does not matter what algorithm we use to sort. So use bubble sort. Find any two elements in the subsequence that are out of order, and swap them. There are only three cases: Both elements are before their proper positions in the array; both are after their proper positions in the array; or they are between their proper positions in the array.

    For example, suppose A[i] belongs at position i’ and A[j] belongs at position j’, i < j, but A[i] > A[j]. It follows that i’ > j’ (because that is where the elements “belong”, and A[i] > A[j]). Case 1: Suppose i’ and j’ are both greater than i and j; that is, the order goes i < j < j’ < i’. But by hypothesis, i’ is only d units from i, so this entire range is only d units wide at most. So j is also within d units of i’ and i is within d units of j’, so when we swap A[i] with A[j], both elements are still within d of where they belong. The analysis for Case 2 and Case 3 are similar.

    So each step of a bubble sort — on any subsequence of A — will preserve the desired property, from which it follows that the entire bubble sort will preserve the desired property, from which it follows that any sort will preserve the desired property. Q.E.D.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an array which might contain duplicate elements(more than two duplicates of an
Possible Duplicate: Interview Q: given an array of numbers, return array of products of
Possible Duplicate: Counting inversions in an array This is an phone interview question :
Possible Duplicate: Given a 2d array sorted in increasing order from left to right
Possible Duplicate: Counting inversions in an array This is a programming question that I
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Linked list interview question This is an interview question for which I
Possible Duplicate: What’s the “condition” in C interview question? void main() { if(CONDITION) printf(Hello
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
Possible Duplicate: Examples of GoF Design Patterns At an interview a while back I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.