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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:32:15+00:00 2026-06-13T14:32:15+00:00

I am trying to write the following code for mergesort ,Can anybody please tell

  • 0

I am trying to write the following code for mergesort ,Can anybody please tell me what is wrong with the code:-

#include<stdio.h>
#include<stdlib.h>

int arr[100];

void merge(int low,int mid,int high)
{
    int temp[100],lowind = low,highind = mid+1,i=0;
    while(lowind <= mid && highind<=high)
    {
        if(arr[lowind]<arr[highind])
        {
            temp[i] = arr[lowind];
            i++;
            lowind++;
        }
        else
        {
            temp[i] = arr[highind];
            i++;
            highind++;
        }
    }

    while(lowind<=mid)
    {
        temp[i] = arr[lowind];
        i++;
        lowind++;
    }

    while(highind<=high)
    {
        temp[i] = arr[highind];
        i++;
        highind++;
    }

    for(i=low;i<=high;i++)
        arr[i] = temp[i-low];
}

void divider(int low,int high)
{
    int mid = (low+high)/2;
    if(low<mid)
    {
        divider(low,mid);
        divider(mid+1,high);
        merge(low,mid,high);
    }
}

int main()
{
    int i,j,temp;

    for(i=0;i<20;i++)
        arr[i] = rand()%21;

    for(i=0;i<20;i++)
        printf("%d ",arr[i]);
    printf("\n");

    divider(0,19);

    for(i=0;i<20;i++)
            printf("%d ",arr[i]);
    printf("\n");

    return 0;
}
  • 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-06-13T14:32:16+00:00Added an answer on June 13, 2026 at 2:32 pm

    In void divider(int low,int high), it should be if(low<high) instead of if(low<mid) as you have written.

    To know why, try to understand cases like low=2, high=3, here mid=(low+high)/2=5/2=2. Hence your condition fails.

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

Sidebar

Related Questions

I am trying to write the following code, though it is wrong it will
I am trying to write equivalent linq code for following query. SELECT A.* FROM
I'm trying to write productions ( LL ) for the following code a[i].b[a[p]].id.xyz.a[c].o =
Using Delphi 2007 I can write the following code: interface TTestType = (ttTest1, ttTest2);
In a C# forms project, I can write the following code to get something
I am trying to write the following code... static ArrayList<RssItem>[][] list = new ArrayList<RssItem>[][];
I have the following code which I'm trying to write a LRU Cache. I
I'm trying to write the following piece of code in python. g = networkx.Digraph()
I have the following code in PHP, I am trying to write the same
I'm trying to write the following code inside ASP.Net-MVC razor view but the page

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.