{"id":33640,"date":"2024-11-01T09:18:51","date_gmt":"2024-11-01T09:18:51","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33640"},"modified":"2024-11-01T11:47:18","modified_gmt":"2024-11-01T11:47:18","slug":"python-coding-test-course-bubble-sort-program-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33640\/","title":{"rendered":"Python Coding Test Course, Bubble Sort Program 1"},"content":{"rendered":"<p><body><\/p>\n<p>\n    This lecture explains the process of solving basic algorithm problems using Python.<br \/>\n    In this session, we will take a closer look at one of the most fundamental sorting algorithms, <strong>Bubble Sort<\/strong>.<br \/>\n    Bubble Sort is a simple yet easy-to-understand sorting algorithm that helps in understanding the basics of algorithms.\n<\/p>\n<h2>1. What is Bubble Sort?<\/h2>\n<p>\n    Bubble Sort is an algorithm that sorts by comparing two adjacent elements.<br \/>\n    It goes through each element of the list sequentially, swapping them when two adjacent elements are in the wrong order.<br \/>\n    This process is repeated, which is why it is named Bubble Sort, as the largest element &#8220;bubbles&#8221; up to the end of the list.\n<\/p>\n<h3>1.1. The working process of Bubble Sort<\/h3>\n<p>\n    The basic working process of Bubble Sort is as follows:\n<\/p>\n<ol>\n<li>Start from the first element of the list and compare two adjacent elements.<\/li>\n<li>If the first element is greater than the second element, swap their positions.<\/li>\n<li>Repeat until the end of the list to send the largest element to the very end.<\/li>\n<li>Excluding the last element of the list, go back to step 1 and repeat for the remaining part.<\/li>\n<li>Continue this process until all elements in the list are sorted.<\/li>\n<\/ol>\n<h2>2. Implementing the Bubble Sort Algorithm<\/h2>\n<p>\n    Now, let&#8217;s implement Bubble Sort in Python code. Here is the basic code for the Bubble Sort algorithm.\n<\/p>\n<pre><code>def bubble_sort(arr):\n    n = len(arr)\n    # Repeat for the length of the list\n    for i in range(n):\n        # In each pass, the last i elements are already sorted, so repeat until n-i-1\n        for j in range(0, n-i-1):\n            # Compare and swap adjacent elements\n            if arr[j] &gt; arr[j+1]:\n                arr[j], arr[j+1] = arr[j+1], arr[j]\n    return arr<\/code><\/pre>\n<h3>2.1. Code Explanation<\/h3>\n<p>\n    The above code defines a function called <code>bubble_sort<\/code>, which takes a list to be sorted as an argument. <\/p>\n<ul>\n<li><code>n = len(arr)<\/code>: Stores the length of the given list.<\/li>\n<li>The outer <code>for<\/code> loop repeats for all elements of the list.<\/li>\n<li>The inner <code>for<\/code> loop compares adjacent elements in the currently unsorted part and swaps them if necessary.<\/li>\n<li>Returns the sorted list after all passes are completed.<\/li>\n<\/ul>\n<h2>3. Bubble Sort with an Example<\/h2>\n<p>\n    Let&#8217;s actually use Bubble Sort to see it in action with an example.\n<\/p>\n<h3>3.1. Example List<\/h3>\n<p>\n    Let&#8217;s sort the following list: <code>[64, 34, 25, 12, 22, 11, 90]<\/code>\n<\/p>\n<h3>3.2. Step-by-Step Explanation of the Sorting Process<\/h3>\n<p>\n    1. First Pass:<\/p>\n<ul>\n<li>Compare 64 and 34 \u2192 Swap \u2192 <code>[34, 64, 25, 12, 22, 11, 90]<\/code><\/li>\n<li>Compare 64 and 25 \u2192 Swap \u2192 <code>[34, 25, 64, 12, 22, 11, 90]<\/code><\/li>\n<li>Compare 64 and 12 \u2192 Swap \u2192 <code>[34, 25, 12, 64, 22, 11, 90]<\/code><\/li>\n<li>Compare 64 and 22 \u2192 Swap \u2192 <code>[34, 25, 12, 22, 64, 11, 90]<\/code><\/li>\n<li>Compare 64 and 11 \u2192 Swap \u2192 <code>[34, 25, 12, 22, 11, 64, 90]<\/code><\/li>\n<li>Compare 64 and 90 \u2192 No Swap \u2192 <code>[34, 25, 12, 22, 11, 64, 90]<\/code><\/li>\n<\/ul>\n<p>    The largest number, 90, has bubbled up to the end.\n<\/p>\n<p>\n    2. Second Pass:<\/p>\n<ul>\n<li>Compare 34 and 25 \u2192 Swap \u2192 <code>[25, 34, 12, 22, 11, 64, 90]<\/code><\/li>\n<li>Compare 34 and 12 \u2192 Swap \u2192 <code>[25, 12, 34, 22, 11, 64, 90]<\/code><\/li>\n<li>Compare 34 and 22 \u2192 Swap \u2192 <code>[25, 12, 22, 34, 11, 64, 90]<\/code><\/li>\n<li>Compare 34 and 11 \u2192 Swap \u2192 <code>[25, 12, 22, 11, 34, 64, 90]<\/code><\/li>\n<li>Compare 34 and 64 \u2192 No Swap \u2192 <code>[25, 12, 22, 11, 34, 64, 90]<\/code><\/li>\n<\/ul>\n<p>    The second largest number, 64, has moved to the second last position.\n<\/p>\n<p>\n    By repeating this process, we eventually obtain the sorted list <code>[11, 12, 22, 25, 34, 64, 90]<\/code>.\n<\/p>\n<h2>4. Time Complexity<\/h2>\n<p>\n    The time complexity of Bubble Sort is O(n\u00b2) in the worst case. This means that the time taken grows proportionally to the square of the length \\( n \\) of the list.<br \/>\n    However, in the best case scenario of dealing with an already sorted list, it can be reduced to O(n).<br \/>\n    This occurs because no swaps happen during the first pass.\n<\/p>\n<h2>5. Improvements on Bubble Sort<\/h2>\n<p>\n    While Bubble Sort has the advantage of being simple to implement, it has the drawback of being inefficient.<br \/>\n    In practical use, the following improvements can be applied:\n<\/p>\n<ul>\n<li>You can terminate the sort immediately if no swaps occur, reducing unnecessary iterations.<\/li>\n<li>During the maximum pass, you can avoid comparing the already sorted part and reduce the range of comparisons.<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>\n    In this session, we learned the basic concept of Bubble Sort and how to implement it in Python.<br \/>\n    Bubble Sort may be a simple algorithm, but it is very useful for understanding sorting concepts.<br \/>\n    If you want to build a foundation in algorithms, start by understanding Bubble Sort!\n<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lecture explains the process of solving basic algorithm problems using Python. In this session, we will take a closer look at one of the most fundamental sorting algorithms, Bubble Sort. Bubble Sort is a simple yet easy-to-understand sorting algorithm that helps in understanding the basics of algorithms. 1. What is Bubble Sort? Bubble Sort &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33640\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python 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":[145],"tags":[],"class_list":["post-33640","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 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\/33640\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This lecture explains the process of solving basic algorithm problems using Python. In this session, we will take a closer look at one of the most fundamental sorting algorithms, Bubble Sort. Bubble Sort is a simple yet easy-to-understand sorting algorithm that helps in understanding the basics of algorithms. 1. What is Bubble Sort? Bubble Sort &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Bubble Sort Program 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33640\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:18+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\/33640\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33640\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Bubble Sort Program 1\",\"datePublished\":\"2024-11-01T09:18:51+00:00\",\"dateModified\":\"2024-11-01T11:47:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33640\/\"},\"wordCount\":562,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33640\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33640\/\",\"name\":\"Python Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:51+00:00\",\"dateModified\":\"2024-11-01T11:47:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33640\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33640\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33640\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 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":"Python 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\/33640\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This lecture explains the process of solving basic algorithm problems using Python. In this session, we will take a closer look at one of the most fundamental sorting algorithms, Bubble Sort. Bubble Sort is a simple yet easy-to-understand sorting algorithm that helps in understanding the basics of algorithms. 1. What is Bubble Sort? Bubble Sort &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Bubble Sort Program 1\"","og_url":"https:\/\/atmokpo.com\/w\/33640\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:51+00:00","article_modified_time":"2024-11-01T11:47:18+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\/33640\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33640\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Bubble Sort Program 1","datePublished":"2024-11-01T09:18:51+00:00","dateModified":"2024-11-01T11:47:18+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33640\/"},"wordCount":562,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33640\/","url":"https:\/\/atmokpo.com\/w\/33640\/","name":"Python Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:51+00:00","dateModified":"2024-11-01T11:47:18+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33640\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33640\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33640\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python 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\/33640","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=33640"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33640\/revisions"}],"predecessor-version":[{"id":33641,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33640\/revisions\/33641"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}