{"id":35008,"date":"2024-11-01T09:34:33","date_gmt":"2024-11-01T09:34:33","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35008"},"modified":"2024-11-01T11:45:22","modified_gmt":"2024-11-01T11:45:22","slug":"kotlin-coding-test-course-bubble-sort-program-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35008\/","title":{"rendered":"kotlin coding test course, bubble sort program 2"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this tutorial, we will learn how to implement the Bubble Sort algorithm using the Kotlin language. Bubble Sort is one of the most basic sorting algorithms and is often featured as a fundamental problem in coding tests due to its simplicity. We will solve a problem together with detailed explanations.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Write a program to sort a given integer array in ascending order. The length of the array is N (1 \u2264 N \u2264 1000), and each element is M (-10<sup>4<\/sup> \u2264 M \u2264 10<sup>4<\/sup>).<\/p>\n<h3>Example<\/h3>\n<ul>\n<li>Input: [64, 34, 25, 12, 22, 11, 90]<\/li>\n<li>Output: [11, 12, 22, 25, 34, 64, 90]<\/li>\n<\/ul>\n<h2>Algorithm Explanation<\/h2>\n<p>Bubble Sort is an algorithm that sorts by comparing two adjacent elements and swapping them if they are in the wrong order. This process is repeated, and thus the largest value moves to the end of the array, which is why it is called &#8220;Bubble.&#8221; The average and worst-case time complexity of Bubble Sort is O(N<sup>2<\/sup>).<\/p>\n<h3>Bubble Sort Algorithm<\/h3>\n<ol>\n<li>Get the size N of the array.<\/li>\n<li>Repeat from 0 to N-1.<\/li>\n<li>Repeat from the first index of the array up to N-i-1, comparing the values at the two indices.<\/li>\n<li>If the value at the first index is greater than the value at the second index, swap the two values.<\/li>\n<li>After all iterations are complete, the array will be sorted.<\/li>\n<\/ol>\n<h2>Kotlin Implementation<\/h2>\n<p>Now let&#8217;s implement the algorithm in Kotlin:<\/p>\n<pre><code>\nfun bubbleSort(arr: IntArray): IntArray {\n    val n = arr.size\n    for (i in 0 until n - 1) { \n        \/\/ The last i elements are already sorted, so repeat up to n-i-1.\n        for (j in 0 until n - i - 1) {\n            \/\/ Compare two adjacent elements\n            if (arr[j] &gt; arr[j + 1]) {\n                \/\/ Swap\n                val temp = arr[j]\n                arr[j] = arr[j + 1]\n                arr[j + 1] = temp\n            }\n        }\n    }\n    return arr\n}\n\nfun main() {\n    val inputArray = intArrayOf(64, 34, 25, 12, 22, 11, 90)\n    val sortedArray = bubbleSort(inputArray)\n    println(\"Sorted array: ${sortedArray.joinToString(\", \")}\")\n}\n    <\/code><\/pre>\n<h2>Explanation<\/h2>\n<p>The code above shows a basic implementation of Bubble Sort. The main function <code>bubbleSort<\/code> takes an integer array as input and returns the sorted array. It operates by using two nested loops to compare adjacent elements and swap them when necessary.<\/p>\n<h3>Code Execution Process<\/h3>\n<p>When the code is executed, it will output the following result:<\/p>\n<pre><code>Sorted array: 11, 12, 22, 25, 34, 64, 90<\/code><\/pre>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of Bubble Sort is O(N<sup>2<\/sup>). This is because, in the worst case, all elements need to be compared. However, in the best case (an already sorted array), the time complexity reduces to O(N). The space complexity is O(1).<\/p>\n<h3>Advantages and Disadvantages of Bubble Sort<\/h3>\n<p>The advantages and disadvantages of Bubble Sort are as follows:<\/p>\n<ul>\n<li><strong>Advantages:<\/strong>\n<ul>\n<li>Simple to implement.<\/li>\n<li>Consumes minimal memory.<\/li>\n<li>Can check whether the array is already sorted during the sorting process.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Disadvantages:<\/strong>\n<ul>\n<li>Time complexity is too high, making it inefficient for large datasets.<\/li>\n<li>Performs worse compared to other efficient sorting algorithms.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Application Problem<\/h2>\n<p>Now let&#8217;s tackle a slightly more advanced problem based on the concepts we&#8217;ve covered. Remove duplicate elements from the given array and output the sorted result.<\/p>\n<h3>Problem Definition<\/h3>\n<p>Write a program that takes an integer array as input, removes duplicate elements, and outputs the result sorted in ascending order.<\/p>\n<h3>Example<\/h3>\n<ul>\n<li>Input: [64, 34, 25, 25, 12, 22, 11, 90, 90]<\/li>\n<li>Output: [11, 12, 22, 25, 34, 64, 90]<\/li>\n<\/ul>\n<h2>Code Implementation<\/h2>\n<pre><code>\nfun removeDuplicatesAndSort(arr: IntArray): IntArray {\n    return arr.distinct().toIntArray().let { bubbleSort(it) }\n}\n\nfun main() {\n    val inputArray = intArrayOf(64, 34, 25, 25, 12, 22, 11, 90, 90)\n    val resultArray = removeDuplicatesAndSort(inputArray)\n    println(\"Array after removing duplicates and sorting: ${resultArray.joinToString(\", \")}\")\n}\n    <\/code><\/pre>\n<h2>Result<\/h2>\n<p>When the above code is executed, the following result will be produced:<\/p>\n<pre><code>Array after removing duplicates and sorting: 11, 12, 22, 25, 34, 64, 90<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we explored the Bubble Sort algorithm in detail, and through this, we solved basic sorting and duplicate removal problems. Although Bubble Sort is a simple and easy-to-understand algorithm, familiarizing yourself with various sorting algorithms that appear in coding tests will allow you to solve problems more effectively. In the next tutorial, we will cover more efficient sorting algorithms. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this tutorial, we will learn how to implement the Bubble Sort algorithm using the Kotlin language. Bubble Sort is one of the most basic sorting algorithms and is often featured as a fundamental problem in coding tests due to its simplicity. We will solve a problem together with detailed explanations. Problem Definition Write &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35008\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, bubble sort program 2&#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":[106],"tags":[],"class_list":["post-35008","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin coding test course, bubble sort program 2 - \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\/35008\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, bubble sort program 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this tutorial, we will learn how to implement the Bubble Sort algorithm using the Kotlin language. Bubble Sort is one of the most basic sorting algorithms and is often featured as a fundamental problem in coding tests due to its simplicity. We will solve a problem together with detailed explanations. Problem Definition Write &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, bubble sort program 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35008\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:22+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, bubble sort program 2\",\"datePublished\":\"2024-11-01T09:34:33+00:00\",\"dateModified\":\"2024-11-01T11:45:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/\"},\"wordCount\":512,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35008\/\",\"name\":\"kotlin coding test course, bubble sort program 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:33+00:00\",\"dateModified\":\"2024-11-01T11:45:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35008\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35008\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, bubble sort program 2\"}]},{\"@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":"kotlin coding test course, bubble sort program 2 - \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\/35008\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, bubble sort program 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this tutorial, we will learn how to implement the Bubble Sort algorithm using the Kotlin language. Bubble Sort is one of the most basic sorting algorithms and is often featured as a fundamental problem in coding tests due to its simplicity. We will solve a problem together with detailed explanations. Problem Definition Write &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, bubble sort program 2\"","og_url":"https:\/\/atmokpo.com\/w\/35008\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:33+00:00","article_modified_time":"2024-11-01T11:45:22+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35008\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35008\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, bubble sort program 2","datePublished":"2024-11-01T09:34:33+00:00","dateModified":"2024-11-01T11:45:22+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35008\/"},"wordCount":512,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35008\/","url":"https:\/\/atmokpo.com\/w\/35008\/","name":"kotlin coding test course, bubble sort program 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:33+00:00","dateModified":"2024-11-01T11:45:22+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35008\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35008\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35008\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, bubble sort program 2"}]},{"@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\/35008","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=35008"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35008\/revisions"}],"predecessor-version":[{"id":35009,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35008\/revisions\/35009"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}