{"id":34010,"date":"2024-11-01T09:23:02","date_gmt":"2024-11-01T09:23:02","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34010"},"modified":"2024-11-01T10:54:19","modified_gmt":"2024-11-01T10:54:19","slug":"c-coding-test-course-bubble-sort-program-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34010\/","title":{"rendered":"C# Coding Test Course, Bubble Sort Program 1"},"content":{"rendered":"<p><body><\/p>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n            Given an integer array, write a program to sort the array in ascending order.<br \/>\n            The program must be implemented using the &#8216;bubble sort&#8217; algorithm.<br \/>\n            Bubble sort works by comparing two adjacent elements and swapping them if they are in the wrong order.<br \/>\n            This process is repeated until the end of the array, and at the end of one complete pass, the last element will be in sorted order.<br \/>\n            Therefore, the number of sorted elements increases by one after each pass.\n        <\/p>\n<h2>Input Format<\/h2>\n<p>\n            The first line contains the integer N (1 \u2264 N \u2264 100).<br \/>\n            The second line contains N integers, with each integer in the range of -1000 \u2264 A[i] \u2264 1000.\n        <\/p>\n<h2>Output Format<\/h2>\n<p>\n            Print the sorted N integers, separated by spaces, on a single line.\n        <\/p>\n<h2>Example Input<\/h2>\n<pre>\n        5\n        5 3 2 4 1\n        <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n        1 2 3 4 5\n        <\/pre>\n<\/section>\n<section>\n<h2>Introduction to Bubble Sort Algorithm<\/h2>\n<p>\n            The bubble sort algorithm is one of the simplest sorting algorithms, which works by iteratively traversing the array<br \/>\n            and comparing adjacent elements to sort them. This algorithm is not efficient and has a time complexity of O(N\u00b2),<br \/>\n            but it is often used for educational purposes due to its simplicity in understanding and implementation.\n        <\/p>\n<p>\n            The basic operation of bubble sort is as follows:\n        <\/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 greater than the second, swap their positions.<\/li>\n<li>Repeat this process until the end of the array.<\/li>\n<li>After one complete pass, the last element is sorted, so decrease the size of the array by one and repeat.<\/li>\n<li>Continue this process until the entire array is sorted.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>C# Bubble Sort Implementation<\/h2>\n<p>\n            Now, let&#8217;s implement bubble sort in C# based on the algorithm above.<br \/>\n            Below is the C# code that implements bubble sort.\n        <\/p>\n<pre><code>\nusing System;\n\nclass Program\n{\n    static void BubbleSort(int[] arr)\n    {\n        int n = arr.Length;\n        for (int i = 0; i &lt; n - 1; i++)\n        {\n            for (int j = 0; j &lt; n - i - 1; j++)\n            {\n                if (arr[j] &gt; arr[j + 1])\n                {\n                    \/\/ Swap arr[j] and arr[j + 1]\n                    int temp = arr[j];\n                    arr[j] = arr[j + 1];\n                    arr[j + 1] = temp;\n                }\n            }\n        }\n    }\n\n    static void Main(string[] args)\n    {\n        int n = int.Parse(Console.ReadLine());\n        int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);\n        \n        BubbleSort(arr);\n        \n        Console.WriteLine(string.Join(\" \", arr));\n    }\n}\n        <\/code><\/pre>\n<p>\n            In the code above, we define the <code>BubbleSort<\/code> method which sorts the array.<br \/>\n            This method uses two nested loops to compare and swap adjacent elements to sort the array.<br \/>\n            The <code>Main<\/code> method takes the size and elements of the array as input from the user,<br \/>\n            calls the <code>BubbleSort<\/code> method to sort the array, and then prints the result.\n        <\/p>\n<\/section>\n<section>\n<h2>Code Execution and Testing<\/h2>\n<p>\n            To run the code, you need to use an IDE or editor that supports C#.<br \/>\n            You can use IDEs like Visual Studio or Visual Studio Code.<br \/>\n            After entering the code, click the <code>Run<\/code> button or press <code>Ctrl + F5<\/code> to execute.\n        <\/p>\n<p>\n            After execution, if you input integers as in the example, the sorted result in ascending order will be output.<br \/>\n            It is advisable to use various test cases to verify the accuracy of the algorithm.\n        <\/p>\n<h3>Test Cases<\/h3>\n<ul>\n<li>\n<strong>Input:<\/strong><\/p>\n<pre>3\n5 1 2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>1 2 5<\/pre>\n<\/li>\n<li>\n<strong>Input:<\/strong><\/p>\n<pre>4\n3 3 2 1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>1 2 3 3<\/pre>\n<\/li>\n<li>\n<strong>Input:<\/strong><\/p>\n<pre>5\n-1 -2 0 2 1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>-2 -1 0 1 2<\/pre>\n<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>Pros and Cons of the Bubble Sort Algorithm<\/h2>\n<p>\n            The bubble sort algorithm is simple and easy to understand, but it is inefficient for larger data sets.<br \/>\n            Below are the pros and cons of bubble sort.\n        <\/p>\n<h3>Pros<\/h3>\n<ul>\n<li>Implementation is simple and intuitive.<\/li>\n<li>It is easy to visually verify the sorting process.<\/li>\n<li>Does not require additional memory. (in-place sorting)<\/li>\n<\/ul>\n<h3>Cons<\/h3>\n<ul>\n<li>The time complexity is O(N\u00b2), making it inefficient. It is not advisable to use for large datasets.<\/li>\n<li>In the worst case, it may require comparing all N elements, which can take a long time.<\/li>\n<li>Since it continuously modifies the array while sorting, stability may decrease.<\/li>\n<\/ul>\n<p>\n            Considering the above pros and cons, bubble sort is mainly used for educational purposes, and in practice,<br \/>\n            it is common to use more efficient sorting algorithms like quick sort or merge sort.\n        <\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n            Today, we implemented a bubble sort program in C# and examined the process in detail.<br \/>\n            While it is a simple algorithm, it is very useful for understanding the basics of sorting algorithms.<br \/>\n            I hope it helps to solidify your understanding and improve your programming skills as you learn various sorting algorithms.\n        <\/p>\n<\/section>\n<footer>\n<p>\u00a9 2023 Coding Test Course. All rights reserved.<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Given an integer array, write a program to sort the array in ascending order. The program must be implemented using the &#8216;bubble sort&#8217; algorithm. Bubble sort works by comparing two adjacent elements and swapping them if they are in the wrong order. This process is repeated until the end of the array, and &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34010\/\" 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":[90],"tags":[],"class_list":["post-34010","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"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\/34010\/\" \/>\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=\"Problem Description Given an integer array, write a program to sort the array in ascending order. The program must be implemented using the &#8216;bubble sort&#8217; algorithm. Bubble sort works by comparing two adjacent elements and swapping them if they are in the wrong order. This process is repeated until the end of the array, and &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Bubble Sort Program 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34010\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:54:19+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\/34010\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34010\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Bubble Sort Program 1\",\"datePublished\":\"2024-11-01T09:23:02+00:00\",\"dateModified\":\"2024-11-01T10:54:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34010\/\"},\"wordCount\":622,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34010\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34010\/\",\"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:23:02+00:00\",\"dateModified\":\"2024-11-01T10:54:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34010\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34010\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34010\/#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\/34010\/","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":"Problem Description Given an integer array, write a program to sort the array in ascending order. The program must be implemented using the &#8216;bubble sort&#8217; algorithm. Bubble sort works by comparing two adjacent elements and swapping them if they are in the wrong order. This process is repeated until the end of the array, and &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Bubble Sort Program 1\"","og_url":"https:\/\/atmokpo.com\/w\/34010\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:02+00:00","article_modified_time":"2024-11-01T10:54:19+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\/34010\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34010\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Bubble Sort Program 1","datePublished":"2024-11-01T09:23:02+00:00","dateModified":"2024-11-01T10:54:19+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34010\/"},"wordCount":622,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34010\/","url":"https:\/\/atmokpo.com\/w\/34010\/","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:23:02+00:00","dateModified":"2024-11-01T10:54:19+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34010\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34010\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34010\/#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\/34010","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=34010"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34010\/revisions"}],"predecessor-version":[{"id":34011,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34010\/revisions\/34011"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}