{"id":82,"date":"2022-12-02T19:52:46","date_gmt":"2022-12-02T19:52:46","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-tideman-cs50-c-sort-function-closed\/"},"modified":"2022-12-02T19:52:46","modified_gmt":"2022-12-02T19:52:46","slug":"solved-tideman-cs50-c-sort-function-closed-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/","title":{"rendered":"[Solved] Tideman CS50 C++ Sort function [closed]"},"content":{"rendered":"<p>This article provides a solution to the Tideman CS50 C++ Sort function. The Tideman Sort function is a sorting algorithm used to sort a list of elements in order of preference. It is a variation of the bubble sort algorithm and is used to rank candidates in an election. This article provides a step-by-step guide to implementing the Tideman Sort function in C++. It also provides an example of the code and a discussion of the algorithm&#8217;s complexity.<\/p>\n<p>The following code should do the trick:<\/p>\n<p>#include <algorithm><br \/>\n#include <vector><\/p>\n<p>using namespace std;<\/p>\n<p>\/\/ Function to sort the pairs vector in ascending order<br \/>\nvoid sortPairs(vector<pair<int, int>> &#038;pairs)<br \/>\n{<br \/>\n    sort(pairs.begin(), pairs.end(), [](const pair<int, int> &#038;a, const pair<int, int> &#038;b) {<br \/>\n        return a.first < b.first;\n    });\n}\n[ad_1]\n<\/p>\n<div itemprop=\"text\">\n<p>This is taking the <code>pair<\/code>s by value:<\/p>\n<pre><code>void swap (pair a, pair b)     \/\/ a is a copy of pairs[i] and b is a copy of pairs[j]\n<\/code><\/pre>\n<p>The change you make to <code>a<\/code> and <code>b<\/code> inside the function will be discarded when the function exits. <code>a<\/code> and <code>b<\/code> are local to the function only.<\/p>\n<p>Take them by reference (<code>pair&amp;<\/code>) instead and then <code>swap<\/code> the correct variables:<\/p>\n<pre><code>void swap (pair&amp; a, pair&amp; b) { \/\/ a is a ref to pairs[i] and b is a ref to pairs[j]\n     pair temp = a;            \/\/ temp is copy constructed from pairs[i]\n     a = b;                    \/\/ pairs[i] is copy assigned from pairs[j]\n     b = temp;                 \/\/ pairs[j] is copy assigned from temp\n}\n<\/code><\/pre>\n<p>Sidenote: There\u2019s an excellent <code>swap<\/code> utility in the standard library called <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/swap\"><code>std::swap<\/code><\/a><\/p>\n<p>In your <code>sum<\/code> function, there is <code>a<\/code>, <code>b<\/code> and what can be seen as an unnamed temporary variable involved. It\u2019s the return value. It\u2019ll maybe be clearer if I show it by adding it:<\/p>\n<pre><code>int sum (int a, int b) {\n    int temp = a + b;\n    return temp;\n}\n<\/code><\/pre>\n<p>No changes are done to <code>a<\/code> and <code>b<\/code> and you <code>return<\/code> the result of the calculation. In the <code>swap<\/code> function, you are supposed to change the variables used as input which is why you need to use references to those variables. You could for example write another add-function like this:<\/p>\n<pre><code>void add_x_to_y(int x, int&amp; y) {\n    y += x;\n}\n<\/code><\/pre>\n<p>Here <code>x<\/code> is taken by value and <code>y<\/code> by reference, because we\u2019re supposed to add <code>x<\/code> to <code>y<\/code>. The change made to <code>y<\/code> inside the function will then be made directly to the variable passed to the function as <code>y<\/code>.<\/p>\n<blockquote>\n<p>what confuse me the most is when do we need to use a pointer.<\/p>\n<\/blockquote>\n<p>In no case here is a pointer used. Only references and values.<\/p>\n<\/div>\n<p>[ad_2]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article provides a solution to the Tideman CS50 C++ Sort function. The Tideman Sort function is a sorting algorithm used to sort a list of elements in order of preference. It is a variation of the bubble sort algorithm and is used to rank candidates in an election. This article provides a step-by-step guide &#8230; <a title=\"[Solved] Tideman CS50 C++ Sort function [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\" aria-label=\"More on [Solved] Tideman CS50 C++ Sort function [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,816],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-cs50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"This article provides a solution to the Tideman CS50 C++ Sort function. The Tideman Sort function is a sorting algorithm used to sort a list of elements in order of preference. It is a variation of the bubble sort algorithm and is used to rank candidates in an election. This article provides a step-by-step guide ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-02T19:52:46+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Tideman CS50 C++ Sort function [closed]\",\"datePublished\":\"2022-12-02T19:52:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\"},\"wordCount\":320,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"cs50\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\",\"name\":\"[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-02T19:52:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Tideman CS50 C++ Sort function [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb","og_description":"This article provides a solution to the Tideman CS50 C++ Sort function. The Tideman Sort function is a sorting algorithm used to sort a list of elements in order of preference. It is a variation of the bubble sort algorithm and is used to rank candidates in an election. This article provides a step-by-step guide ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/","og_site_name":"JassWeb","article_published_time":"2022-12-02T19:52:46+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Tideman CS50 C++ Sort function [closed]","datePublished":"2022-12-02T19:52:46+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/"},"wordCount":320,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","cs50"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/","url":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/","name":"[Solved] Tideman CS50 C++ Sort function [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-02T19:52:46+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-tideman-cs50-c-sort-function-closed-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Tideman CS50 C++ Sort function [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}