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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:25:21+00:00 2026-05-26T14:25:21+00:00

This is based on the algorithm given in the Cormen’s book. What am I

  • 0

This is based on the algorithm given in the Cormen’s book. What am I doing wrong?

#include <stdio.h>
#include <conio.h>

void mergesort(int a[],int,int);
void merge(int a[],int,int,int);

int main()
{
   int i,num,a[50];

   printf("Enter the number of elements : ");
   scanf("%d",&num);

   for(i=1;i<=num;i++)
   {
      printf("\n%d) ",i);
      scanf("%d",&a[i]);
   }

   mergesort(a,1,num);

   for(i=1;i<=num;i++)
   {
      printf("SoRTED ArrAy \n");
      printf("\n%d) ",a[i]);
   }

   getch();

   return 0;
}

void mergesort(int a[],int i, int k)
{
   int j;
   j=(i+k)/2;

   while(i<k)
   {
      mergesort(a,i,j);
      mergesort(a,j+1,k);
      merge(a,i,j,k);
   }
}

void merge(int a[],int p,int q,int r)
{
   int i,j,k,n1,n2;

   n1=q-p+1;
   n2=r-q;

   int l[n1],s[n2];

   for(i=1;i<=n1;i++)
   {
      l[i]=a[p+i-1];
   }

   for(j=1;i<=n2;j++)
   {
      s[j]=a[q+j];
   }

   l[n1+1]=1000;
   s[n2+1]=1000;

   i=1;
   j=1;

   for(k=p;k<=r;k++)
   {
      if(l[k]<s[k])
      {
         a[k]=l[i];
         i++;
      }
      else
      {
         a[k]=s[j];
         j++;
      }
   }
}
  • 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-26T14:25:22+00:00Added an answer on May 26, 2026 at 2:25 pm
    void mergesort(int a[],int i, int k)
    {
     int j;
     j=(i+k)/2;
     while(i<k)
     {
      mergesort(a,i,j);
      mergesort(a,j+1,k);
      merge(a,i,j,k);
     }
    }
    

    Nothing inside the while changes the value of i or k. So if i<k, the loop will repeat forever.

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

Sidebar

Related Questions

I've written a QuickSort algorithm based off pseudo-code that I had been given. I've
I am trying to implement password based encryption algorithm, but I get this exception:
Given a string like this: a,string, with,various,values, and some,quoted What is a good algorithm
I am coding this Sudoku Solver with the following algorithm: Given a grid which
This is based on a similar question How to Replace Multiple Characters in Access
This is based on my full question . I decided to take it in
Some time ago I created this LAMP based web. At the time I wrote
This question is based on another question of mine (thankfully answered). So if in
This request is based in MS Access VBA. I would like to know what
This question is based on the thread . I have the shell function function

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.