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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:11:53+00:00 2026-05-16T15:11:53+00:00

Suppose we have the following problem – we want to read a set of

  • 0

Suppose we have the following problem – we want to read a set of (x, y) coordinates and a name, then sort them in order, by increasing the distance from the origin (0, 0). Here is an algorithm which use simplest bubble sort:

 #include<iostream>
    #include <algorithm>
    using namespace std;
    struct point{
        float x;
        float y;
         char name[20];

         };
      float dist(point p){
          return p.x*p.x+p.y*p.y;
            }
       void sorting(point pt[],int n){
          bool doMore = true;
        while (doMore) {
            doMore = false;  // Assume no more passes unless exchange made.
            for (int i=0; i<n-1; i++) {
                if (dist(pt[i]) > dist(pt[i+1])) {
                    // Exchange elements
                    point temp = pt[i]; pt[i] = pt[i+1]; pt[i+1] = temp;
                    doMore = true;  // Exchange requires another pass.
                }
            }
        }

       }
       void display(point pt[],int n){
            for (int i=0;i<n;i++){
                cout<<pt[i].name<< " ";
                   }
       }
    int main(){
    point pts[1000];
    int n=0;

    while (cin>>pts[n].name>>pts[n].x>>pts[n].y){
        n++;

    }
     sorting(pts,n);
     display(pts,n);

    return 0;
    }

But I want to write STL sorting algorithm instead of bubble sort. How to do so?

I mean that, how should I use dist function in STL sort algorithm?

  • 1 1 Answer
  • 1 View
  • 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-16T15:11:53+00:00Added an answer on May 16, 2026 at 3:11 pm

    The STL sort function std::sort can take a user-defined comparison function (or function object) as an optional third argument. So if you have your items in e.g.:

    vector<point> points;
    

    You can sort them by calling:

    sort(points.begin(), points.end(), my_comp);
    

    where my_comp() is a function with the following prototype:

    bool my_comp(const point &a, const point &b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo friends, I want to figure out the following problem: Suppose,we have expression like,
Propose to consider the following problem. Suppose we have some composite object. Are there
I am solving the following problem: Suppose I have a list of software packages
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have the following problem: Suppose I have some basic counter class Counter .
suppose you are given the following problem. You have two index sets that have
I have the following problem: Suppose I have a table with the following fields:
Suppose you have the following string: Hello... how are you!!?? I'm bored!! I want
Suppose I have the following HTML snippet: <input type=text id=myinput /> Now I want
I have the following problem. Suppose I have to arrays X and Y whose

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.