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

The Archive Base Latest Questions

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

I have an object that has three variables in it: a source vertex, a

  • 0

I have an object that has three variables in it: a source vertex, a destination vertex, and a length. I have a Quicksort Algorithm that sorts an object by its length. However, I want it so that if I compare two objects and their lengths are equal, that my quicksort will sort the object with the smaller source vertex in front of the one with the larger source vertex. If those are also equal, I want to compare the destination vertexes. I want to sort the larger destination vertex in front of that with the smaller source vertex. This will all be done in an array. Below is my implementation of quicksort. Note that e[i] is my object which holds my vertexes and length.

void quickSort(edge *e, int left, int right)
{
  int i = left, j = right;
  int temp, temp1, temp2;
  while(i <= j)
  {
    while(e[i].getLength() < pivot)
      i++;
    while(e[j].getLength() > pivot)
      j--;
    if(i <= j)
    {
      temp = e[i].getLength();
      temp1 = e[i].getEdgeSrc();
      temp2 = e[i].getEdgeDes();
      e[i].setLength(e[j].getLength());
      e[i].setEdgeSrc(e[j].getEdgeSrc());
      e[i].setEdgeDes(e[j].getEdgeDes());
      e[j].setLength(temp);
      e[j].setEdgeSrc(temp1);
      e[j].setEdgeDes(temp2);
      i++;
      j--;
    } //if statement
  }///while loop
  if(left < j)
    quickSort(e, left, j);
  if(i < right)
    quickSort(e, i, right);

}

Might anyone know how / where to perform the sorting of the vertexes if the lengths are equal? 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-14T05:14:02+00:00Added an answer on June 14, 2026 at 5:14 am

    Best define a comparison operator for the edge objects that does what you need it to do:

    class edge
    {
      /* ... */
    public:
    
      bool operator<(const edge &other) const
      {
         /* Length is first criterion: */
         if (length < other.length)
            return true;
         /* Source vertex is second criterion: */
         else if ((length == other.length) && (src < other.src))
            return true;
         return false;
      }
    
      /* ... */
    };
    

    And then, inside your quick sort implementation, you use this operator to compare edge objects, rather than accessing getLength() etc. directly:

    while(e[i].getLength() < pivot)
      i++;
    

    becomes

    while(e[i] < pivot)
      i++;
    

    And

    while(e[j].getLength() > pivot)
      j--;
    

    becomes

    while(pivot < e[j])
      j--;
    

    Note that I’ve only defined operator<, not operator>, so I suggest using only that. Alternatively, you can define operator> as well.

    Note that above I assume that pivot is a reference to the current pivot element. If it’s actually an integer index to the current pivot, you’ll need to replace it with e[pivot].


    Separate note: You might want to use std::swap(e[i],e[j]); instead of the long (and error-prone) sequence of get- and set-statements and the temporary variables.

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

Sidebar

Related Questions

Another newbie question that has me stuck badly. I have three variables var state1
I have two tables: object that has object_id column and avalues that have object_id
I have an object that has pairs of replacement values used for simple encoding
I have an object that has properties, but when I try to access them,
I have an object that has a multi-part key and I am struggling to
I have an object that has a child collection: class Person { public string
I have an object that has 3 separate Dictionaries. The value parameter for each
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have a seat object that has a car object that has a owner
I have a domain object that has been auto generated for me by MyGeneration.

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.