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

  • SEARCH
  • Home
  • 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 3785192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:29:27+00:00 2026-05-19T11:29:27+00:00

I am working on this question . My function prototype is static void Sort(byte[]

  • 0

I am working on this question. My function prototype is

static void Sort(byte[] arr, int leftPos, int rightPos)

In the 2nd part of the function i know leftPos to leftPos + (rightPos-leftPos)/2 and (rightPos-leftPos)/2 to rightPos are sorted in order.

I tried thinking of how i could do an in place sort knowing the two parts are in order. I couldnt think of any. I looked at the merge function on merge sort but it uses an output array rather than in place.

How do i sort it in place knowing both slices are in order?

Note: I was thinking i could pass in a extra array that is the same length as the main array to use as temp memory but the way i thought of would require me to do Array.Copy after each merge.

  • 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-19T11:29:28+00:00Added an answer on May 19, 2026 at 11:29 am

    In-place merge is possible, but it’s complicated and doesn’t give much performance gain. Below is some sample code from here. from is your leftPos, to is your rightPos, pivot is your (rightPos-leftPos)/2 and the lengths are the lengths of each half.

    void merge(int from, int pivot, int to, int len1, int len2) {
      if (len1 == 0 || len2==0) return;
      if (len1+len2 == 2) {
       if (compare(pivot, from) < 0)
        exchange(pivot, from);
       return;
      }
      int first_cut, second_cut;
      int len11, len22;
      if (len1 > len2) {
       len11=len1/2;
       first_cut = from + len11;
       second_cut = lower(pivot, to, first_cut);
       len22 = second_cut - pivot;
      } else {
       len22 = len2/2;
       second_cut = pivot + len22;
       first_cut = upper(from, pivot, second_cut);
       len11=first_cut - from;
      }
      rotate(first_cut, pivot, second_cut);
      int new_mid=first_cut+len22;
      merge(from, first_cut, new_mid, len11, len22);
      merge(new_mid, second_cut, to, len1 - len11, len2 - len22);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has spawned out of this one. Working with lists of structs in
Following up on this question, I'm working on a large Delphi 7 codebase which
As kind of a followup to this question I've gotten a solution working on
All, I'm working on a SharePoint project, but this isn't a SharePoint-specific question per-se.
I have a simple code fragment in JS working with prototype inheritance. function object(o)
See also: Why is my image rotation algorithm not working? This question isn't language
This question came to my mind while working on 2 projects in AI and
This question is probably language-agnostic, but I'll focus on the specified languages. While working
(there is a follow up to this question here ) I am working on
I think questions like this are the reason why I don't like working with

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.