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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:40:44+00:00 2026-06-01T05:40:44+00:00

I have a (150×150)2d array of structs that i’d like to sort in a

  • 0

I have a (150×150)2d array of structs that i’d like to sort in a row independent fashion. Because it is a struct, i do not believe (correct me if i’m wrong here) i cannot use qsort() or at least do not know how to due to the fact i’m paring structs and the element i am comparing is a double which goes against the compare prototype requirement of qsort(). At any rate i’d like to apply quicksort on

  struct my_struct {
    int x;
    int y;
    double d;
 };
void quicksort(struct my_struct* array,int start, int end)
{

struct my_struct key, Pivot;
int i,j,PivotPoint;
if(start< end)
{
    PivotPoint = (start+end)/2;
    theswap(&array[start], &array[PivotPoint]);
    key = array[start];
    i= start+1;
    j = end;
    while (i<=j)
    {
        while((i<=end) && (array[i].d <= key.d))
            ++i;
        while ((j>=start) && array[j].d> key.d) {
            --j;
            if (i<j) {
                theswap(&array[i], &array[j]);
            }
        }
    }
    theswap(&array[start], &array[j]);
    quicksort(array, start, j-1);
    quicksort(array, j+1, end);
    }
}
void theswap(struct my_struct *a, struct TourElement *b)
{
struct my_struct t;
t=*a; 
*a=*b;
*b=t;
}

in my main function
i have something
like this:

 for (i=0;i<150;++i)
   {
   for (j=0;j<150;++j)
   { 
    My_array[i][j].x = somethingUseful;
    My_Array[i][j].y = somethingEquallyUseful;
    My_Array[i][j].d = CalcD(somethingUseful,somethingEquallyUseful);
    }
    qsort(My_Array[i],150,sizeof(my_struct),compare);
   }


       int compare(struct my_struct a , struct my_struct b)
     {
          return a.d -b.d;
     }

When i execute quicksort, the application hangs, upon further investigation, there doesn’t seem to be any elements in array within the quicksort function. (I added a for loop printf at the beginning of quicksort to itemize the struct’s d values and nothing was printed)

Can anyone identify what i am doing wrong here? I’m getting no compile errors. And The “D”s are calculated correctly.

  • 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-01T05:40:45+00:00Added an answer on June 1, 2026 at 5:40 am

    you can use std c qsort

    void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
    

    the compare function is something like this:

    int my_struct_comp(const void *p1, const void *p2){
      my_struct *mp1 = (my_Struct*)p1;
      my_struct *mp2 = (my_Struct*)p2;
    
      return mp1->d - mp2->d;
    }
    

    than you can call qsort (where len is the length of the array)

    qsort(myarray, len, sizeof(my_struct), &my_struct_cmp);
    

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

Sidebar

Related Questions

I have a database with 150 million products, I would like index these using
I have a large database with over 150 tables that I've recently been handed.
I have an array: string myArray={15,56,17-75,78,100-150,130} I want to filter myList by arrayElements .
I have an array of part lengths, for examples sake:- array(150, 180, 270); I
I've got a WCF intranet application I'm working on that will have 150 clients
I have a search app that only has 150 files. So if somebody searches
I have table with articles and there is around 150.000 of records, in that
I have 150 32 by 48 matrices of values that represent color intensities from
I have a large (150,000x7) dataframe that I intend to use for back-testing and
I have a dataset of around 150 million records that's generated daily it contains:

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.