{"id":34538,"date":"2024-11-01T09:29:09","date_gmt":"2024-11-01T09:29:09","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34538"},"modified":"2024-11-01T11:40:51","modified_gmt":"2024-11-01T11:40:51","slug":"javascript-coding-test-course-bubble-sort-program-1","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34538\/","title":{"rendered":"JavaScript Coding Test Course, Bubble Sort Program 1"},"content":{"rendered":"<p><body><\/p>\n<header>\n<p>Let&#8217;s learn about the essential algorithm for coding tests, Bubble Sort.<\/p>\n<\/header>\n<section>\n<h2>1. Problem Definition<\/h2>\n<p>\n            Implement the Bubble Sort algorithm that takes an array as input and sorts it in ascending order.<br \/>\n            Bubble Sort operates by comparing two adjacent elements and repeatedly moving the largest element to the end of the array.\n        <\/p>\n<h3>Input Format<\/h3>\n<p>\n            The input is an integer array with a length between 1 and 1000. Each element can have a value between -10,000 and 10,000.\n        <\/p>\n<h3>Output Format<\/h3>\n<p>\n            Return the array sorted in ascending order.\n        <\/p>\n<h2>2. Problem Approach<\/h2>\n<p>\n            Bubble Sort is a very intuitive sorting algorithm. The basic approach is to compare two adjacent elements,<br \/>\n            and if they are not in order, swap them repeatedly until the entire array is sorted. This process is repeated for the size of the array,<br \/>\n            and continues until no more swaps occur. This way, the largest value moves to the end of the array in each step.\n        <\/p>\n<h3>2.1. Algorithm Steps<\/h3>\n<ol>\n<li>Obtain the length of the array.<\/li>\n<li>Use two indices to compare the elements of the array.<\/li>\n<li>If adjacent elements are not sorted, swap them.<\/li>\n<li>Consider the sorting complete if no swaps occur during a full pass.<\/li>\n<li>Repeat the above process and ultimately return the array sorted in ascending order.<\/li>\n<\/ol>\n<h2>3. Bubble Sort Code Implementation<\/h2>\n<p>\n            Now let&#8217;s implement the above algorithm in JavaScript. The basic Bubble Sort function is as follows.\n        <\/p>\n<pre>\n<code>\n\/\/ Bubble Sort function implementation\nfunction bubbleSort(arr) {\n    let n = arr.length;  \/\/ Store the length of the array\n\n    \/\/ Repeat to sort the array\n    for (let i = 0; i &lt; n - 1; i++) {\n        let swapped = false;  \/\/ Variable to check if a swap has occurred\n\n        \/\/ Compare and swap adjacent elements\n        for (let j = 0; j &lt; n - i - 1; j++) {\n            if (arr[j] &gt; arr[j + 1]) {\n                \/\/ Swap\n                let temp = arr[j];\n                arr[j] = arr[j + 1];\n                arr[j + 1] = temp;\n                swapped = true;  \/\/ Record that a swap has occurred\n            }\n        }\n\n        \/\/ Exit if no swaps have occurred\n        if (!swapped) break;\n    }\n\n    return arr;  \/\/ Return the sorted array\n}\n\n\/\/ Test\nlet testArray = [64, 34, 25, 12, 22, 11, 90];\nconsole.log(bubbleSort(testArray));  \/\/ [11, 12, 22, 25, 34, 64, 90]\n<\/code>\n        <\/pre>\n<h2>4. Time Complexity Analysis<\/h2>\n<p>\n            The time complexity of the Bubble Sort algorithm is O(n\u00b2) in the worst case. This is due to the presence of two nested loops, each proportional to the length of the array.<br \/>\n            The best case (when the array is already sorted) is O(n). In this case, no swaps occur, and the process terminates after the first step.<br \/>\n            Bubble Sort is generally inefficient, and it is advisable to use other algorithms (e.g., Quick Sort, Merge Sort) when sorting large datasets in practice.\n        <\/p>\n<h3>4.1. Space Complexity<\/h3>\n<p>\n            The space complexity of Bubble Sort is O(1). It does not use any unnecessary additional memory,<br \/>\n            as sorting is performed within the given array.\n        <\/p>\n<h2>5. Advantages and Disadvantages of Bubble Sort<\/h2>\n<h3>Advantages<\/h3>\n<ul>\n<li>The algorithm is simple and easy to understand.<\/li>\n<li>It does not have any specific requirements, so no additional memory management is necessary.<\/li>\n<li>It works effectively with small datasets.<\/li>\n<\/ul>\n<h3>Disadvantages<\/h3>\n<ul>\n<li>The time complexity is inefficient (O(n\u00b2)).<\/li>\n<li>Even when the array is well sorted, it must perform a complete pass, reducing efficiency.<\/li>\n<li>It is inefficient for sorting large datasets.<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>\n            In this lecture, we implemented the Bubble Sort algorithm using JavaScript.<br \/>\n            Due to its structural simplicity, Bubble Sort is useful for educational purposes, but in real production environments, it is advisable to use more efficient algorithms.<br \/>\n            I hope you can further develop your coding skills as you explore more complex algorithms and data structures in the future.\n        <\/p>\n<h2>7. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Bubble_sort\">Bubble Sort &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\">JavaScript Guide &#8211; MDN Web Docs<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/bubble-sort\/\">Bubble Sort &#8211; GeeksforGeeks<\/a><\/li>\n<\/ul>\n<\/section>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s learn about the essential algorithm for coding tests, Bubble Sort. 1. Problem Definition Implement the Bubble Sort algorithm that takes an array as input and sorts it in ascending order. Bubble Sort operates by comparing two adjacent elements and repeatedly moving the largest element to the end of the array. Input Format The input &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34538\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript 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":[141],"tags":[],"class_list":["post-34538","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript 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\/34538\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Let&#8217;s learn about the essential algorithm for coding tests, Bubble Sort. 1. Problem Definition Implement the Bubble Sort algorithm that takes an array as input and sorts it in ascending order. Bubble Sort operates by comparing two adjacent elements and repeatedly moving the largest element to the end of the array. Input Format The input &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Bubble Sort Program 1&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34538\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:29:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:40:51+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\/34538\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34538\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Bubble Sort Program 1\",\"datePublished\":\"2024-11-01T09:29:09+00:00\",\"dateModified\":\"2024-11-01T11:40:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34538\/\"},\"wordCount\":479,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34538\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34538\/\",\"name\":\"JavaScript Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:29:09+00:00\",\"dateModified\":\"2024-11-01T11:40:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34538\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34538\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34538\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript 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":"JavaScript 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\/34538\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Let&#8217;s learn about the essential algorithm for coding tests, Bubble Sort. 1. Problem Definition Implement the Bubble Sort algorithm that takes an array as input and sorts it in ascending order. Bubble Sort operates by comparing two adjacent elements and repeatedly moving the largest element to the end of the array. Input Format The input &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Bubble Sort Program 1\"","og_url":"https:\/\/atmokpo.com\/w\/34538\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:29:09+00:00","article_modified_time":"2024-11-01T11:40:51+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\/34538\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34538\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Bubble Sort Program 1","datePublished":"2024-11-01T09:29:09+00:00","dateModified":"2024-11-01T11:40:51+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34538\/"},"wordCount":479,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34538\/","url":"https:\/\/atmokpo.com\/w\/34538\/","name":"JavaScript Coding Test Course, Bubble Sort Program 1 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:29:09+00:00","dateModified":"2024-11-01T11:40:51+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34538\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34538\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34538\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript 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\/34538","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=34538"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34538\/revisions"}],"predecessor-version":[{"id":34539,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34538\/revisions\/34539"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}