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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:00:26+00:00 2026-06-16T02:00:26+00:00

Here’s what I’m trying to do. There are 3 arrays, cost[] node1[] and node2[].

  • 0

Here’s what I’m trying to do.
There are 3 arrays, cost[] node1[] and node2[].
These entires correspond to edges of a graph with node1[i],node2[i] and cost[i] specifying that there is an edge going from vertex node1[i] to node2[i] with an edge weight of cost[i].

I’m trying to sort these edges with respect to their weights, i.e sort the cost[] array using merge-sort. However whenever I’m, changing an entry in the cost[] array I also want to change the corresponding entries in the node1 and node2 array since even the nodes of the graph have to be modified. Ie if node1[]=1,2,3 and node2[]=2,3,1 cost[]={7 4 8} then after sorting the cost array the node1 and node2 should look like node1[]=2,1,3 node2[]=3,2,1. and cost[]=4,7,8

Here’s my code.

        #include<stdio.h>
        #include<stdlib.h>
        int merge_sort(int arr[],int low,int high,int node1[],int node2[])

        {

        int mid;

        if(low<high) {

        mid=(low+high)/2;

        // Divide and Conquer

        merge_sort(arr,low,mid,node1,node2);

        merge_sort(arr,mid+1,high,node1,node2);

        // Combine

        merge(arr,low,mid,high,node1,node2);

        }



        return 0;

        }



        int merge(int arr[],int l,int m,int h,int node1[],int node2[])

        {

        int arr1[80000],arr2[80000]; // Two temporary arrays to
        int arr3[70000],arr4[70000];
        int arr5[70000],arr6[70000];
        int n1,n2,i,j,k;

        n1=m-l+1;

        n2=h-m;



        for(i=0; i<n1; i++)
        {

        arr1[i]=arr[l+i];
        arr3[i]=node1[l+i];
        arr5[i]=node2[l+i];

        }
        for(j=0; j<n2; j++)
        {
        arr2[j]=arr[m+j+1];
        arr4[i]=node1[m+j+1];
        arr6[i]=node2[m+j+1];
        }


        arr1[i]=99999; // To mark the end of each temporary array
        arr2[j]=99999;
        arr3[i]=99999;
        arr4[j]=99999;
        arr5[i]=99999;
        arr6[j]=99999;


        i=0;

        j=0;

        for(k=l; k<=h; k++) { //process of combining two sorted arrays

        if(arr1[i]<=arr2[j])
        {

        arr[k]=arr1[i++];
        //node1[k]=arr3[i++]; COMMENTED LINES!!!!!!!!!!!
        //node2[k]=arr5[i++];

        }
        else
        {
        arr[k]=arr2[j++];
        //node1[k]=arr4[j++]; COMMENTED LINES!!!!!!!!~!
        //node2[k]=arr6[j++];
        }
        }
        return(0);
        }

        int main(void)
        {
            int i,j,n,vert1,vert2,weight;
            scanf("%d",&n);
            int adjmat[n+1][n+1],cluster[n+1][n+1];
            int *cost,*node1,*node2;
            node1=malloc(sizeof(int)*1000000);
            node2=malloc(sizeof(int)*1000000);
            cost=malloc(sizeof(int)*1000000);
            for(i=0;i<n+1;i++)
                for(j=0;j<n+1;j++)
                {
                    adjmat[i][j]=0;
                    cluster[i][j]=0;
                }
            for(i=1;i<n+1;i++)
                cluster[i][0]=i;
            for(i=1;i<(n+1)*(n+1);i++)
            {
                scanf("%d %d %d",&vert1,&vert2,&weight);

                node1[i]=vert1;
                node2[i]=vert2;
                cost[i]=weight;
                if(node1[i]==node1[i-1] && node2[i]==node2[i-1] && cost[i]==cost[i-1])
                    break;
                //  printf("%d %d %d\n",node1[i],node2[i],cost[i]);
                adjmat[vert1][vert2]=weight;
                adjmat[vert2][vert1]=weight;
            }
            printf("\n%d\n",i);
            merge_sort(cost,1,124751,node1,node2);
            for(j=1;j<i;j++)
                printf("%d %d %d\n",node1[j],node2[j],cost[j]);
            return(0);
        }

Whenever I comment the lines in the merge function the code manages to sort the cost array. However whenever I un comment these lines somehow everything gets equated to 0. i.e all entires of the node1 node2 and cost arrays are 0. Could anyone tell me why this is happening? Thanks!

  • 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-16T02:00:27+00:00Added an answer on June 16, 2026 at 2:00 am

    You probably have forgotten to take care of the side effect of the i++ operation. There is no need at all at that place to work with side effects, don’t do that.

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

Sidebar

Related Questions

Here's what I'm trying to do... It's quite simple and obviously there must be
Here is the code in a function I'm trying to revise. This example works
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here's a sample of a SpinBox that writes its changes to underlying variables. The
Here is my text file forms. S1,F2 title including several white spaces (abbr) single,Here<->There,reply
I know there's a lot of other questions out there that deal with this
Here is my scenario. I have a website running under AppPool1 and that works
Here's the code I have. It works. The only problem is that the first
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.