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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:46:40+00:00 2026-06-10T06:46:40+00:00

I am working on a code I want to optimize. The code is all

  • 0

I am working on a code I want to optimize. The code is all about sorting a double array by ascending order of its second .
The first input is an integer N, and the second is a 2D array of size N*2 (call it c), then we sort the array by ascending order of c[.][1], and when there are equal elements, say if c[i][1]==c[j][1], for two integers i and j, we sort those elements by ascending of the elements of the first columns, so c[i][1] is below c[j][1] if c[i][1]<c[j][1]. Let’s take an example :

input 
3
2 3
1 3
4 2

output 
4 2
1 3
2 3

The fact is that the code needs to run in less than 0.5s and mine is really too slow. Here it is

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

//determining the index of the max elments in an array
int max(int i, int N, int **c)
{
    int j=0;
    int M=0;

    for(j=0;j<i;j++)
    {if(c[j][1]>c[M][1]){M=j;}else{}}
    return M;
}

int main ()
{
    //integers used for the loops
    int i;
    int j;

    //the size of the 2D array is N*2
    int N;
    scanf("%d",&N);
    int **c;
    int mx;
    int maxi;

    //this array is the output, thath is the 2D array sorted
    int e[N][2];

    //2D array we want to sort
    c = malloc(N*sizeof(int*));

    for (i=0;i<N;i++)
    {
        c[i] = malloc(2*sizeof(int));
        for (j=0;j<2;j++)
        {
            scanf("%d",&c[i][j]);
        }
    }

    //at the first step, we have initialized the value of the max
    maxi=max(N,N,c);


    for(i=0;i<N;i++)
    {
        //we sort the c[.][1], and we take the max (called 'mx') of the array. At each step of the loop, we throw away the max from the array c[.][1] (we mean the max found at the precedent step of the loop)

        mx=max(N-i,N,c);

        //Here, we look at the multiple occurence of the max, if there are, we sort the c[.][0] for which c[.][1]=c[mx][1] by ascending order
        if(maxi==mx){int k;
            for(k=0;k <N;k++){if(c[k][1]==c[mx][1]){if(c[k][0]>c[mx][0]){mx=k;}}else{}}
        }else{}

        //we keep the value of the max in order to verify that the same value of the max has another occurence in following steps
        maxi=mx;

        //e is the double array for the output
        e[i][0]=c[mx][0];
        e[i][1]=c[mx][1];
        int j;

        //here we throw away the max from the array
        for(j=mx;j< N-i-1;j++){c[j][1]=c[j+1][1];c[j][0]=c[j+1][0];}
    }

    for(i=0;i<N;i++)
    {printf("%d %d",e[N-1-i][0],e[N-1-i][1]);
        printf("\n");}

}

Could anyone help ?

  • 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-10T06:46:41+00:00Added an answer on June 10, 2026 at 6:46 am

    Your code has way too high computational complexity. Also, int e[N][2] only works under GCC extension since N is a variable determined at run time instead of compile time.
    An easy (in terms of coding) way to improve it is utilizing qsort() in C standard library.

    struct IntPair
    {
        int n[2];
    };
    
    int compareIntPair(const void* lhs, const void* rhs)
    {
        const struct IntPair *l = (const struct IntPair*)lhs;
        const struct IntPair *r = (const struct IntPair*)rhs;
    
        if(l->n[1] < r->n[1])
            return -1;
        if(l->n[1] > r->n[1])
            return 1;
        if(l->n[0] < r->n[0])
            return -1;
        if(l->n[0] > r->n[0])
            return 1;
        return 0;
    }
    
    int main(void)
    {
        //integers used for the loops
        int i;
        //the size of the 2D array is N*2
        int N;
        struct IntPair* c;
        scanf("%d",&N);
        c = (struct IntPair*)calloc(N, sizeof(struct IntPair));
    
        for(i = 0; i < N; ++i)
        {
            scanf("%d%d", &c[i].n[0], &c[i].n[1]);
        }
    
        qsort(c, N, sizeof(struct IntPair), compareIntPair);
    
        for(i = 0; i < N; ++i)
            printf("%d %d\n", c[i].n[0], c[i].n[1]);
    
        free(c);
        return 0;
    }
    

    Edit: to answer the question in comment “could each element of the N-dynamic allocation points to 1D array of size 2?”

    int compareElem(const void* lhs, const void* rhs)
    {
        const int *l = *(const int**)lhs;
        const int *r = *(const int**)rhs;
    
        if(l[1] < r[1])
            return -1;
        if(l[1] > r[1])
            return 1;
        if(l[0] < r[0])
            return -1;
        if(l[0] > r[0])
            return 1;
        return 0;
    }
    
    int main(void)
    {
        //integers used for the loops
        int i;
        //the size of the 2D array is N*2
        int N;
        int ** c;
        scanf("%d",&N);
        c = (int**)calloc(N, sizeof(int*));
    
        for(i = 0; i < N; ++i)
        {
            c[i] = (int*)calloc(2, sizeof(int));
            scanf("%d%d", &c[i][0], &c[i][1]);
        }
    
        qsort(c, N, sizeof(int*), compareElem);
    
        for(i = 0; i < N; ++i)
        {
            printf("%d %d\n", c[i][0], c[i][1]);
            free(c[i]);
        }
    
        free(c);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code and its working but i want it to return dailyTotals
I want to display data from database in DataGridView...This is my code...Its not working...Can
This is my Working Code DriftMul:=99; WriteProcessMemory(HandleWindow, ptr($4E709C), @DriftMul, 2, Write); I want to
I want to know i can do something similar to this (not working) code
I'm working with code I'm converting to Pgsql working with .NET. I want to
The following awk code is working as expected. I want to check if the
Before I start, I want to make it clear that my code is working
The code that I am working with has tons of style that I want
I am working on sql server where i want to save html code into
I want to rotate imageview with continuously looping on android.my code rotate is working

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.