{"id":34190,"date":"2024-11-01T09:25:19","date_gmt":"2024-11-01T09:25:19","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34190"},"modified":"2024-11-01T10:58:15","modified_gmt":"2024-11-01T10:58:15","slug":"c-coding-test-course-bubble-sort-program-1-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34190\/","title":{"rendered":"C++ Coding Test Course, Bubble Sort Program 1"},"content":{"rendered":"<p><body><\/p>\n<p>In this course, we will cover how to implement the Bubble Sort algorithm using C++. Bubble Sort is one of the simplest sorting algorithms, which sorts by comparing two adjacent elements and moving the larger element backward. This algorithm will help you understand the basics of sorting algorithms and learn how to implement it in C++.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Write a program to sort a given integer array in ascending order.<\/p>\n<h3>Input<\/h3>\n<pre>\nThe input is an array consisting of multiple integers. For example, let's assume the following array is given:\n[64, 34, 25, 12, 22, 11, 90]\n<\/pre>\n<h3>Output<\/h3>\n<pre>\nOutput the result of the array sorted in ascending order. For example, the output for the above input would be:\n[11, 12, 22, 25, 34, 64, 90]\n<\/pre>\n<h2>Bubble Sort Algorithm<\/h2>\n<p>The basic idea of Bubble Sort is to compare two adjacent elements to sort them. If the first element is larger than the second element, the two elements are swapped. This process is repeated until the end of the array, and at the end of each iteration, the largest element moves to the last position. Below are simple steps to explain the Bubble Sort algorithm:<\/p>\n<ol>\n<li>Start from the first element of the array and compare two adjacent elements.<\/li>\n<li>If the first element is larger than the second element, swap the two elements.<\/li>\n<li>Repeat this process until the end of the array.<\/li>\n<li>Repeat this process for the length of the array &#8211; 1, so that if a sorted section is created, you can terminate the loop early.<\/li>\n<\/ol>\n<h2>C++ Code Implementation<\/h2>\n<p>Now, let&#8217;s implement Bubble Sort using C++. Below is the C++ code that implements Bubble Sort:<\/p>\n<pre><code> \n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n\nvoid bubbleSort(vector&lt;int&gt;&amp; arr) {\n    int n = arr.size();\n    bool swapped;\n    \n    \/\/ Iterate through all elements of the array\n    for (int i = 0; i &lt; n - 1; i++) {\n        swapped = false;\n        \/\/ The last i elements are already sorted, so iterate until n-i-1\n        for (int j = 0; j &lt; n - i - 1; j++) {\n            if (arr[j] &gt; arr[j + 1]) {\n                \/\/ Swap the two elements\n                swap(arr[j], arr[j + 1]);\n                swapped = true;\n            }\n        }\n        \/\/ If no elements were swapped, the array is already sorted, so we exit\n        if (!swapped) break;\n    }\n}\n\nint main() {\n    vector&lt;int&gt; data = {64, 34, 25, 12, 22, 11, 90};\n    \n    cout &lt;&lt; \"Array before sorting: \";\n    for (int num : data) {\n        cout &lt;&lt; num &lt;&lt; \" \";\n    }\n    \n    bubbleSort(data);\n    \n    cout &lt;&lt; \"\\nArray after sorting: \";\n    for (int num : data) {\n        cout &lt;&lt; num &lt;&lt; \" \";\n    }\n    \n    return 0;\n}\n<\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>The above code is a simple example of how to implement Bubble Sort in C++. First, we declare and initialize an integer array using the <code>vector<\/code> library. The <code>bubbleSort<\/code> function performs the sorting operation on the passed array.<\/p>\n<ul>\n<li><code>int n = arr.size();<\/code> &#8211; Gets the size of the array.<\/li>\n<li><code>for (int i = 0; i &lt; n - 1; i++)<\/code> &#8211; Iterates through all elements of the array.<\/li>\n<li><code>for (int j = 0; j &lt; n - i - 1; j++)<\/code> &#8211; Compares within the range of the last i sorted elements.<\/li>\n<li><code>if (arr[j] &gt; arr[j + 1])<\/code> &#8211; If the two elements are not in order, uses the <code>swap<\/code> function to exchange them.<\/li>\n<li><code>if (!swapped) break;<\/code> &#8211; If no swaps occurred, the array is already sorted, so it exits the loop early.<\/li>\n<\/ul>\n<h2>Testing and Result Verification<\/h2>\n<p>Now, let&#8217;s execute the code to check the results. The output when the above code is executed is as follows:<\/p>\n<pre><code>\nArray before sorting: 64 34 25 12 22 11 90 \nArray after sorting: 11 12 22 25 34 64 90 \n<\/code><\/pre>\n<h2>Algorithm Analysis<\/h2>\n<p>Bubble Sort is easy to understand, but it has issues with efficiency. The time complexity in the worst and average cases is O(n\u00b2). This means that performance significantly degrades as the size of the array increases. For this reason, Bubble Sort is more suitable for educational purposes than for practical applications.<\/p>\n<h3>Time Complexity<\/h3>\n<ul>\n<li>Worst case: O(n\u00b2)<\/li>\n<li>Average case: O(n\u00b2)<\/li>\n<li>Best case: O(n) (when already sorted)<\/li>\n<\/ul>\n<h3>Space Complexity<\/h3>\n<ul>\n<li>O(1) (as it directly modifies the given array)<\/li>\n<\/ul>\n<h2>Pros and Cons of Bubble Sort<\/h2>\n<h3>Pros<\/h3>\n<ul>\n<li>The algorithm is simple.<\/li>\n<li>It is easy for beginners to understand.<\/li>\n<li>It can identify whether it is already sorted on its own.<\/li>\n<\/ul>\n<h3>Cons<\/h3>\n<ul>\n<li>High time complexity makes it inefficient for real applications.<\/li>\n<li>Not recommended for large datasets.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this course, we learned how to implement sorting algorithms in C++ through Bubble Sort. We gained an understanding of the basic concepts of Bubble Sort and its algorithm implementation. In the future, I encourage you to study more efficient sorting algorithms (e.g., Quick Sort, Merge Sort, etc.) in depth.<\/p>\n<p>I hope this article was helpful for your C++ coding test preparation. If you have any further questions or would like to learn about more algorithms, please leave a comment!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this course, we will cover how to implement the Bubble Sort algorithm using C++. Bubble Sort is one of the simplest sorting algorithms, which sorts by comparing two adjacent elements and moving the larger element backward. This algorithm will help you understand the basics of sorting algorithms and learn how to implement it in &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34190\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Bubble Sort Program 1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34190","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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:\/\/atmokpo.com\/w\/34190\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this course, we will cover how to implement the Bubble Sort algorithm using C++. Bubble Sort is one of the simplest sorting algorithms, which sorts by comparing two adjacent elements and moving the larger element backward. This algorithm will help you understand the basics of sorting algorithms and learn how to implement it in &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Bubble Sort Program 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34190\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:15+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Bubble Sort Program 1\",\"datePublished\":\"2024-11-01T09:25:19+00:00\",\"dateModified\":\"2024-11-01T10:58:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/\"},\"wordCount\":554,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34190\/\",\"name\":\"C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:19+00:00\",\"dateModified\":\"2024-11-01T10:58:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34190\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34190\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Bubble Sort Program 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/34190\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this course, we will cover how to implement the Bubble Sort algorithm using C++. Bubble Sort is one of the simplest sorting algorithms, which sorts by comparing two adjacent elements and moving the larger element backward. This algorithm will help you understand the basics of sorting algorithms and learn how to implement it in &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Bubble Sort Program 1\"","og_url":"https:\/\/atmokpo.com\/w\/34190\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:19+00:00","article_modified_time":"2024-11-01T10:58:15+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34190\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34190\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Bubble Sort Program 1","datePublished":"2024-11-01T09:25:19+00:00","dateModified":"2024-11-01T10:58:15+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34190\/"},"wordCount":554,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34190\/","url":"https:\/\/atmokpo.com\/w\/34190\/","name":"C++ Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:19+00:00","dateModified":"2024-11-01T10:58:15+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34190\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34190\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34190\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Bubble Sort Program 1"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34190"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34190\/revisions"}],"predecessor-version":[{"id":34191,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34190\/revisions\/34191"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}