LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2024, 02:35 AM   #1
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 20

Rep: Reputation: 1
me


Hello All. It is my implementation of merge sort algorithm in c++. I need opinion it is correct example:

Code:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

template<typename T> std::vector<T>& merge(std::vector<T>& vec, typename std::vector<T>::size_type p, typename std::vector<T>::size_type q, typename std::vector<T>::size_type r) {

    std::vector<T> L(vec.begin()+p, vec.begin()+q);
    std::vector<T> R(vec.begin()+q, vec.begin()+r);
    typename std::vector<T>::size_type i = 0, j = 0, k = p;

    while(i < L.size() && j < R.size()) {
        if(L[i] < R[j]) {
            vec[k] = L[i];
            ++i;
        }
        else {
            vec[k] = R[j];
            ++j;
        }
        ++k;
    }

    while(R.begin()+j!=R.end()) {
        L.push_back(R[j]);
        ++j;
    }

    while(L.begin()+i!=L.end()) {
        vec[k] = L[i];
        ++k;
        ++i;
    }
    return vec;
}

template <typename T> std::vector<T>& merge_sort(std::vector<T>& vec, typename std::vector<T>::size_type p, typename std::vector<int>::size_type r) {

    if((r-p) < 2) {

        return vec;

    }
    auto q = ((p+r)/2);
    merge_sort(vec, p, q);
    merge_sort(vec, q, r);
    return merge(vec, p, q, r);

}
You can use them in main:

Code:
auto main() -> int {
        std::vector<int> vec({23432, 3454, 3, 33, 22, 1, 75});
        std::vector<int> result = merge_sort(vec, 0, 7);
        std::copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, " "));
        return (0);
}
Any comment welcome.
Thanks for answer.
 
Old 08-19-2024, 03:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,636

Rep: Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525Reputation: 7525
in that case would be nice to change the title to something more appropriate, "me" is not really informative.
From the other hand would be nice to know if you have any issues with it? Can you use it? does it work perfectly, or is it just slow or crashing or what?
How did you build/compile it? Did you get any warning or error message?

But anyway, did you test it at all? Do you know if it is correct, don't you? Why did you ask it?
 
3 members found this post helpful.
Old 08-19-2024, 04:50 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,293
Blog Entries: 24

Rep: Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254
As already noted by pan64, an appropriate title would help you attract the attention of those most willing to offer help.

Also, as noted, are you having difficulties or trying to solve some problem? Does it work for you?

The more contextual information you provide, the better you help others help you!

Please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.

Good luck!
 
1 members found this post helpful.
Old 08-22-2024, 06:17 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,820
Blog Entries: 4

Rep: Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984
It really doesn't make much sense, frankly, to "ask for affirmation" about an algorithm that you've written. "Either it works, or seems to work, or it doesn't."

Develop the skills, yourself, to create tests which demonstrate that "your algorithm [implementation] works." And then, if they "pass," move along.

"In the real world," there will be no one to verify or "approve" your work, other than your co-workers. Who will be very annoyed if they find themselves "cleaning up after you."
 
1 members found this post helpful.
Old 08-23-2024, 04:33 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,923
Blog Entries: 1

Rep: Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885
As a start, save a few bytes making your program more portable:
Code:
- auto main() -> int {
+ int main() {
 
Old 08-23-2024, 05:38 AM   #6
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 20

Original Poster
Rep: Reputation: 1
Im sorry I forget save topic title, I can change it now ?
 
Old 08-23-2024, 05:39 AM   #7
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 20

Original Poster
Rep: Reputation: 1
My question is about optimization merge sort algorithm in c++, based on my example.
 
1 members found this post helpful.
Old 08-23-2024, 07:56 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,820
Blog Entries: 4

Rep: Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984
Yes, I think that you can change the topic title in the original post. (Otherwise, "report" it, and thereby ask the site admins for help.) Please do try to do this, because your topic deserves to be discovered and right now it won't be.

Basically: create tests to determine if the algorithm appears to work. If it does, move along to something else. It is very unlikely that someone on a public forum is going to do a "code review" for you. Although you can certainly ask your co-workers (with the consent of your manager ...) to do such a thing. (On projects which I have managed, where "new algorithms" are being created, "code reviews" are mandated [by me].)

Finally: things like "merge sort" are usually handled by library routines of contributed code which have already been thoroughly tested by someone else. "Actum Ne Agas" – Do Not Do A Thing Already Done.

These days, no matter what it is you're doing, it is extremely likely that someone else has already done it, and has perfected it. Therefore, your task in nearly every case is not to code a brand-new solution, but to find what has already been well-done. Then, carefully incorporate it into your specific project. "We stand upon the shoulders of giants."

For instance, the Perl programming language's famous library, CPAN, today contains: "218,541 Perl modules in 45,287 distributions, written by 14,504 authors." Literally, it includes modules about "ancient alphabets." Every single one of these modules perform a long series of automatic tests, on your system, before they will allow themselves to be installed. Every programming language these days has a similar resource. And, it's quite astonishing how big they are. "Here. Let me help you up ..."

Last edited by sundialsvcs; 08-23-2024 at 08:04 AM.
 
Old 08-23-2024, 08:09 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707

Rep: Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627Reputation: 2627
Quote:
Originally Posted by Mac1ek View Post
Im sorry I forget save topic title, I can change it now ?
Use the edit button on the first post of the thread.

 
2 members found this post helpful.
Old 08-23-2024, 06:29 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,293
Blog Entries: 24

Rep: Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254
Quote:
Originally Posted by Mac1ek View Post
Im sorry I forget save topic title, I can change it now ?
Simply click the Edit button at bottom of the first post, click Go Advanced and change the title in the text edit box Title at top of the form.

Click Submit when done.
 
  


Reply

Tags
algorithms, c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration