{"id":572,"date":"2025-01-11T22:55:45","date_gmt":"2025-01-11T22:55:45","guid":{"rendered":"https:\/\/funwithdev.com\/?page_id=572"},"modified":"2025-02-23T16:27:43","modified_gmt":"2025-02-23T16:27:43","slug":"bubble-sort","status":"publish","type":"page","link":"https:\/\/funwithdev.com\/?page_id=572","title":{"rendered":"Bubble Sort"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Fun With Bubble Sort v1.0\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/4KJQXkuV69o?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding Bubble Sort: A Beginner\u2019s Guide<\/h3>\n\n\n\n<p>Bubble Sort is one of the simplest sorting algorithms. It\u2019s a great starting point for anyone new to programming and algorithm design. In this article, we\u2019ll explore the basics of Bubble Sort and provide pseudocode to help you understand how it works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What is Bubble Sort?<\/h4>\n\n\n\n<p>Bubble Sort is a comparison-based algorithm that sorts a list of elements by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. This process is repeated until the list is sorted.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Does Bubble Sort Work?<\/h4>\n\n\n\n<p>The algorithm works by repeatedly swapping adjacent elements if they are in the wrong order. The largest element \u201cbubbles\u201d to the top (end of the list) in each pass through the list. This process continues until no more swaps are needed, indicating that the list is sorted.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Pseudocode for Bubble Sort<\/h4>\n\n\n\n<p>Here\u2019s the pseudocode for Bubble Sort:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pseudocode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>procedure bubbleSort(A: list of sortable items)\n    n = length(A)\n    repeat\n        swapped = false\n        for i = 1 to n-1 inclusive do\n            if A[i-1] > A[i] then\n                swap(A[i-1], A[i])\n                swapped = true\n            end if\n        end for\n        n = n - 1\n    until not swapped\nend procedure<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Python<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def bubble_sort(arr):\n    n = len(arr)\n    for i in range(n):\n        # Last i elements are already sorted\n        for j in range(0, n-i-1):           \n              if arr[j] > arr[j+1]:\n                   arr[j], arr[j+1] = arr[j+1], arr[j]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Test the python method<\/h3>\n\n\n\n<p>arr = [10,9,8,7,6,5]<br>bubble_sort(arr)<br>print(arr)<br>[5, 6, 7, 8, 9, 10]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step-by-Step Explanation<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize Variables<\/strong>: The algorithm starts by initializing the variable <code>n<\/code> to the length of the list <code>A<\/code>. The <code>swapped<\/code> variable is set to <code>false<\/code> to keep track of whether any elements were swapped during the current pass.<\/li>\n\n\n\n<li><strong>Outer Loop<\/strong>: The outer loop continues until no elements are swapped in a pass, indicating that the list is sorted.<\/li>\n\n\n\n<li><strong>Inner Loop<\/strong>: The inner loop iterates through the list from the first element to the second-to-last element. For each pair of adjacent elements, it checks if they are in the wrong order.<\/li>\n\n\n\n<li><strong>Swap Elements<\/strong>: If the elements are in the wrong order, they are swapped, and the <code>swapped<\/code> variable is set to <code>true<\/code>.<\/li>\n\n\n\n<li><strong>Reduce Range<\/strong>: After each pass, the range of the inner loop is reduced by one, as the largest element is already in its correct position.<\/li>\n\n\n\n<li><strong>Repeat<\/strong>: The process repeats until no elements are swapped in a pass.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Time Complexity<\/h4>\n\n\n\n<p>The time complexity of Bubble Sort is O(n^2) in the worst and average cases, where <code>n<\/code> is the number of elements in the list. This makes it inefficient for large lists. However, it has a best-case time complexity of O(n) when the list is already sorted.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>Bubble Sort is a simple and intuitive sorting algorithm that is easy to understand and implement. While it may not be the most efficient for large datasets, it serves as a great introduction to sorting algorithms and algorithm design principles.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>I hope you find this article helpful! If you have any questions or need further clarification, feel free to ask.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Bubble Sort: A Beginner\u2019s Guide Bubble Sort is one of the simplest sorting algorithms. It\u2019s a great starting point for anyone new to programming and algorithm design. In this article, we\u2019ll explore the basics of Bubble Sort and provide pseudocode to help you understand how it works. What is Bubble Sort? Bubble Sort is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-572","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=572"}],"version-history":[{"count":8,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/572\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/572\/revisions\/631"}],"wp:attachment":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}