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 7000935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:44:16+00:00 2026-05-27T20:44:16+00:00

i wrote a regular merge sort array code and all i want to do

  • 0

i wrote a regular merge sort array code and all i want to do is to call to this
function with ‘asize’ and not with a number but instead of recieving [1 2 3 4 5 6 7 8 9 10]
a regular sorted array i get
[-858993460 1 2 3 4 5 6 7 8 9]

please help me to find the reason for this

void merge_sort(int *a,int first, int last)
{
     int middle;
            if(first < last)
           {
                middle=(first+last)/2;
                merge_sort(a,first,middle);
                merge_sort(a,middle+1,last);
                merge(a,first,middle,last);
            }
}






void main()
{

    int a[] = {9, 7, 2, 3, 5, 4, 1, 8, 6, 10};
    int asize= (sizeof a / sizeof a[0]);
  merge_sort(a, 0, asize);
For (i = 0; i < 10; i++)
        printf ("%d ", a[i]);
  • 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-27T20:44:17+00:00Added an answer on May 27, 2026 at 8:44 pm

    Because your resultant array has one strange value and one missing value, the most likely case is an off-by-one error, likely to be passing asize as the index of the last element rather than asize - 1.

    The parameters needed by your merge sort are the first and last indexes (zero-based) so, for an array of size 10, that would be 0 and 9. You’re passing 0 and 10 with your current code.

    Because of this, you’re actually sorting the “array”:

    {9, 7, 2, 3, 5, 4, 1, 8, 6, 10, ?}
     \___________________________/  |
              your array            +-- not your array
    

    and, because ? is -858993460 (in this particular case), you end up with:

    {-858993460, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
     \___________________________________/  \/ not your
                  your array                 \  array
    

    Then, printing the first ten elements gives you the output you’re seeing. Unfortunately, whatever variable (or stack control information or anything really) that was holding that large negative value has now been overwritten with the value ten, not something that you really want.

    What you’re doing is undefined behaviour. Stop it immediately. Don’t make me come over there 🙂

    The easy fix, by the way, is to change:

    merge_sort (a, 0, asize);
    

    into:

    merge_sort (a, 0, asize - 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this PHP code to make some substitutions: function cambio($txt){ $from=array( '/\+\>([^\+\>]+)\<\+/', //finds
In rails I want to wrote some code to change this url string https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg
I wrote a regular expression which I expect should work but it doesn't. var
Here's a little bit of code I wrote for a FileFilter using a regular
Hello i wrote a regular expression to validate URL jQuery.validator.addMethod(url_validation, function (value, element) {
I wrote the following Regular Expression m = re.match('.+SPELL_DAMAGE,.+,([A-Z][a-z]+),.+([A-Z][a-z]+\s[A-Z][a-z]+!?),\d+x\d+,(\d+)',line) I want the second group
I wrote up this regular expression: p = re.compile(r''' \[\[ #the first [[ [^:]*?
i need a Regular Expression to convert a a string to a link.i wrote
How would you write a regular expression to define all strings of 0's and
I am trying to write a regular expression to strip all HTML with the

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.