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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:52:48+00:00 2026-05-27T23:52:48+00:00

I wrote a short code that receives an array and sorts it. This was

  • 0

I wrote a short code that receives an array and sorts it.
This was my first time and I have so many error – please if you can
explain to me how to fix and where/what are my mistakes…
All I want to do is to sort the array and print it after sorting.

#include <stdio.h>

int 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 x[] = {1, 2, 3, 1, 3, 0, 6};
    int xsize= (sizeof x / sizeof x[0])
    merge_sort(x, 0, sizeof x / sizeof x[0]);

    for (int i = 0; i < xsize; i++) { printf("%d ", x[i]); } putchar('\n');
}
  • 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-27T23:52:49+00:00Added an answer on May 27, 2026 at 11:52 pm

    I could see multiple errors in your program

    1. int xsize= (sizeof x / sizeof x[0]) must have a semicolon ; at the end.
    2. for(int i=0;i<xsize;printf("%d ",x[i]), i++); is not the way to print the elements of the array.
    3. your merge() is missing – merge(a,first,middle,last);

    So I went ahead and wrote this sample mergesort for your reference. Hope this helps!!

    #include <stdio.h>
    
    /*
    
    NOTE: 
    The mergesort boils downs to this.. 
    Given two sorted array's how do we merge this?
    
    We need a new array to hold the result of merging
    otherwise it is not possible to do it using array, 
    so we may need a linked list
    
    */
    
    void merge(int a[], int i, int j)
    {
        int mid = (i+j)/2;
        int ai = i;
        int bi = mid+1;
    
        int newa[j-i+1], newai = 0;
    
        while(ai <= mid && bi <= j) {
            if (a[ai] > a[bi])
                newa[newai++] = a[bi++];
            else                    
                newa[newai++] = a[ai++];
        }
    
        while(ai <= mid) {
            newa[newai++] = a[ai++];
        }
    
        while(bi <= j) {
            newa[newai++] = a[bi++];
        }
    
        for (ai = 0; ai < (j-i+1) ; ai++)
            a[i+ai] = newa[ai];
    
    }
    
    void mergesort(int a[], int i, int j)
    {
        int mid = (i+j) / 2;
    
        if (i >= j) return;
    
        mergesort(a, i, mid);
        mergesort(a, mid+1, j);
        merge(a, i, j);
        return;
    }
    
    int main()
    {
        //int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int a[] = {9, 7, 2, 3, 5, 4, 1, 8, 6, 10};
        int i;
    
        mergesort(a, 0, 9);
    
        for (i = 0; i < 10; i++)
            printf ("%d ", a[i]);
    
        printf ("\n");
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To help me better understand lambda I wrote this short snippet that rotates and
I recently wrote some code that uses the same unsigned short to store two
For my first foray into python, I wrote some parsing code that works as
Basically i have a code that shows the user an error if the ID
I wrote a short bit of code to simply skip num_lines lines in an
I wrote a short bash script to complete a task that involves creating a
So I wrote this short script (correct word?) to download the comic images from
Ok, the short version is that I have a Linq entity from a LinqDataSourceUpdateEventArgs
I wrote a short test code in Java to upload a PDF file generated
I have an application that receives a pointer to JPEG data from a camera

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.