Latest LQ Deal: Latest LQ Deals
 LinuxQuestions.org me
 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.

 08-19-2024, 02:35 AM #1 Mac1ek LQ Newbie   Registered: Jan 2024 Distribution: Debian Linux / Red Hat Enterprise Posts: 20 Rep: me Hello All. It is my implementation of merge sort algorithm in c++. I need opinion it is correct example: Code: ```#include #include #include #include template std::vector& merge(std::vector& vec, typename std::vector::size_type p, typename std::vector::size_type q, typename std::vector::size_type r) { std::vector L(vec.begin()+p, vec.begin()+q); std::vector R(vec.begin()+q, vec.begin()+r); typename std::vector::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 std::vector& merge_sort(std::vector& vec, typename std::vector::size_type p, typename std::vector::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 vec({23432, 3454, 3, 33, 22, 1, 75}); std::vector result = merge_sort(vec, 0, 7); std::copy(result.begin(), result.end(), std::ostream_iterator(std::cout, " ")); return (0); }``` Any comment welcome. Thanks for answer.
 08-19-2024, 03:42 AM #2 pan64 LQ Addict   Registered: Mar 2012 Location: Hungary Distribution: debian/ubuntu/suse ... Posts: 22,636 Rep: 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.
 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: 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.
 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: As a start, save a few bytes making your program more portable: Code: ```- auto main() -> int { + int main() {```
 08-23-2024, 05:38 AM #6 Mac1ek LQ Newbie   Registered: Jan 2024 Distribution: Debian Linux / Red Hat Enterprise Posts: 20 Original Poster Rep: Im sorry I forget save topic title, I can change it now ?
 08-23-2024, 05:39 AM #7 Mac1ek LQ Newbie   Registered: Jan 2024 Distribution: Debian Linux / Red Hat Enterprise Posts: 20 Original Poster Rep: My question is about optimization merge sort algorithm in c++, based on my example. 1 members found this post helpful.
08-23-2024, 08:09 AM   #9
boughtonp
Senior Member

Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707

Rep:
Quote:
 Originally Posted by Mac1ek 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.
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:
Quote:
 Originally Posted by Mac1ek 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.

 Tags algorithms, c++

 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 Forum Rules

LinuxQuestions.org

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

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -