{"id":11449,"date":"2022-12-28T10:27:17","date_gmt":"2022-12-28T10:27:17","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11449"},"modified":"2023-07-01T07:45:53","modified_gmt":"2023-07-01T07:45:53","slug":"bubble-sort-program-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/","title":{"rendered":"Bubble Sort Program in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<p>In this article, we will delve into the bubble sort algorithm and its implementation in Java. We will explore the intricacies of the algorithm, examine its time and space complexity, analyze the best and worst-case scenarios, and evaluate its stability. Without further ado, let us commence with our exploration.<\/p>\n<h2>What is Bubble Sort in Java?<\/h2>\n<p>Bubble Sort is an algorithm used to arrange the elements of an array or a list in either ascending or descending order. It is a comparison-based sorting algorithm, meaning that it relies on comparing the elements with each other to perform the sorting operation. You might be curious about how sorting can occur without directly comparing the elements.<\/p>\n<p>Alternatively, there exists a different type of sorting known as Linear sorting, which does not involve comparisons. Examples of linear sorting include Count Sort and Radix Sort. However, these methods are beyond the scope of our current discussion.<\/p>\n<p>In the bubble sort algorithm, during each iteration, the heaviest element &quot;bubbles&quot; its way to the end of the array. This implies that the first iteration identifies the maximum element, the second iteration finds the second maximum element, and so on (assuming the sorting is performed in ascending order). The steps of the bubble sort algorithm are as follows.<\/p>\n<h2>Bubble Sort Algorithm:<\/h2>\n<ol>\n<li>There will be N-1 iterations i.e. the iteration variable i varies as 0&lt;= i &lt; N-1. In each iteration, follow these steps:\n<ul>\n<li>Run a loop from 0 &lt;= j &lt; N-i+1. <\/li>\n<li>In this loop, compare the adjacent elements i.e. array[j] and array[j+1]. If array[j] &gt; array[j+1], the bigger element is before the smaller one in the array. So, swap them. Else, move further.<\/li>\n<\/ul>\n<\/li>\n<li>After these N-1 iterations, the array will be sorted.<\/li>\n<\/ol>\n<p>Let us demonstrate a dry run of these steps. So, consider the array shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498688-Bubble%20Sort%20Program%20in%20Java1.png\" alt=\"\" \/><\/p>\n<p>We will now sort this array using bubble sort. So, the first iteration and its steps are shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498688-Bubble%20Sort%20Program%20in%20Java2.png\" alt=\"\" \/><\/p>\n<p>So, in the first pass of the first iteration, we compare the first 2 elements. Since 5 is smaller than 9 i.e. the smaller element is on the left and larger on the right, the elements are in sorted positions with respect to each other. Hence, we don\u2019t swap them.<\/p>\n<p>Next, we compare 9 and 8. Since 9 is greater than 8, we will swap these elements and now 8 will come before 9.<\/p>\n<p>Next, 9 and 2 are compared and again 9 gets swapped with 2. Then we compare 9 and 1. Since 9 is greater than 1, we swap them. Now, we can see that the largest element of the array i.e. 9 is at its sorted position i.e. at the last of the array. This will happen in every iteration.<\/p>\n<p><strong>So, we have established a fact that in the ith iteration of bubble sort, the ith largest element will move to the ith position from the back of the array i.e. its sorted position.<\/strong><\/p>\n<p>This was just the 1st iteration. Let us do one more iteration. The second iteration swaps are shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498699-Bubble%20Sort%20Program%20in%20Java3.png\" alt=\"\" \/><\/p>\n<p>So, the box around the last element i.e. 9 shows that it is already in its sorted position and no element will be compared to 9. So, the first 2 elements i.e. 5 and 8 are compared. Since they are in sorted positions with respect to each other, no swaps take place.<\/p>\n<p>Next, we compare elements 8 and 2. Since 8 is larger than 2, the swap takes place. Then, 8 and 1 are compared and since 8 is greater than 1, swapping takes place and 8 goes to the second last position of the array.<\/p>\n<p>So, we got the second largest element at the second last position of the array in the second iteration of bubble sort.<\/p>\n<p>This is how the complete bubble sort algorithm will continue for 4 iterations (as there are 5 elements and the number of iterations = N-1 = 5-1=4). The rest of the iterations are shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498699-Bubble%20Sort%20Program%20in%20Java4.png\" alt=\"\" \/><\/p>\n<p>So, we can see that after 4 iterations, the array got sorted. So, now that we have understood the logic, let us write the bubble sort program in Java.<\/p>\n<p><strong>Bubble Sort Program in Java<\/strong><br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11382 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11382 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11382 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11382 .wpsm_nav-tabs > li.active > a, #tab_container_11382 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11382 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11382 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11382 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11382 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11382 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11382 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11382 .wpsm_nav-tabs > li > a:hover , #tab_container_11382 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11382 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11382 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11382 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11382 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11382 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11382 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11382 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11382 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11382 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11382 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11382 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11382\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11382\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11382_1\" aria-controls=\"tabs_desc_11382_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11382\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11382_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\n\r\npublic class Main {\r\n\r\n    public static void bubbleSort(int arr[]) {\r\n        int n = arr.length;\r\n        for (int i = 0; i &lt; n - 1; i++)\r\n            for (int j = 0; j &lt; n - i - 1; j++)\r\n                if (arr[j] &gt; arr[j + 1]) {\r\n                    int temp = arr[j];\r\n                    arr[j] = arr[j + 1];\r\n                    arr[j + 1] = temp;\r\n                }\r\n    }\r\n    \r\n    public static void printArray(int arr[]) {\r\n        int n = arr.length;\r\n        for (int i = 0; i &lt; n; ++i)\r\n            System.out.print(arr[i] + \" \");\r\n        System.out.println();\r\n    }\r\n    \r\n    public static void main(String[] args) {\r\n        int[] arr = {5,9,8,2,1};\r\n        bubbleSort(arr);\r\n        for(int i=0;i&lt;arr.length;i++) {\r\n            System.out.print(arr[i] + \" \");\r\n        }\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11382 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11382 a\"),jQuery(\"#tab-content_11382\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<\/p>\n<h3>Time Complexity of Bubble Sort Program in Java:<\/h3>\n<p>The time complexity of the above program is O(n2) where n is the number of elements. This is because we are using a nested loop. The outer loop runs for n-1 iterations and the inner loop runs for n-i+1 iterations.<\/p>\n<h3>Space Complexity of Bubble Sort Program in Java:<\/h3>\n<p>Since we have not used any extra space, the auxiliary space used will be O(1).<\/p>\n<h2>Bubble Sort Program in Java &#8211; Optimized Version<\/h2>\n<p>The time complexity of the bubble sort algorithm, O(n^2), cannot be improved. However, there is a way to enhance the algorithm. In the program provided above, the bubble sort algorithm completes n-1 iterations even if the array is already sorted. Nevertheless, we can modify it to perform only one iteration. If no swaps are made during this iteration, it indicates that the array is already sorted. Consequently, we can avoid further iterations. The modified version of the program, incorporating this change, is presented below.<\/p>\n<p><strong>Bubble Sort Program in Java &#8211; Optimized Version<\/strong><br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11383 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11383 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11383 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11383 .wpsm_nav-tabs > li.active > a, #tab_container_11383 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11383 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11383 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11383 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11383 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11383 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11383 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11383 .wpsm_nav-tabs > li > a:hover , #tab_container_11383 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11383 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11383 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11383 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11383 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11383 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11383 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11383 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11383 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11383 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11383 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11383 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11383\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11383\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11383_1\" aria-controls=\"tabs_desc_11383_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11383\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11383_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\n\r\npublic class Main {\r\n\r\n    public static void bubbleSort(int arr[]) {\r\n        int n = arr.length;\r\n        boolean flag = false;\r\n        for (int i = 0; i &lt; n - 1; i++) {\r\n            for (int j = 0; j &lt; n - i - 1; j++)\r\n                if (arr[j] &gt; arr[j + 1]) {\r\n                    int temp = arr[j];\r\n                    arr[j] = arr[j + 1];\r\n                    arr[j + 1] = temp;\r\n                    flag = true;\r\n                }\r\n            if(flag == false) break;\r\n        }\r\n    }\r\n    \r\n    public static void printArray(int arr[]) {\r\n        int n = arr.length;\r\n        for (int i = 0; i &lt; n; ++i)\r\n            System.out.print(arr[i] + \" \");\r\n        System.out.println();\r\n    }\r\n    \r\n    public static void main(String[] args) {\r\n        int[] arr = {5,9,8,2,1};\r\n        bubbleSort(arr);\r\n        for(int i=0;i&lt;arr.length;i++) {\r\n            System.out.print(arr[i] + \" \");\r\n        }\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11383 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11383 a\"),jQuery(\"#tab-content_11383\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<\/p>\n<h3>Time Complexity of Optimized Bubble Sort Program in Java:<\/h3>\n<p>The time complexity is still the same. However, the best-case time complexity has improved, but the overall time complexity is still O(n2). <\/p>\n<h3>Space Complexity of Optimized Bubble Sort Program in Java:<\/h3>\n<p>The space complexity is also the same i.e. O(1).<\/p>\n<h2>Best Case of Bubble Sort Program in Java<\/h2>\n<p>From the optimized version of bubble sort, we can say that the best case of the bubble sort is when the array is already sorted as then we only have to perform 1 iteration on the array. Hence, the best case of bubble sort is when the input array is already sorted and the best case time complexity is O(N) where N is the number of elements.<\/p>\n<h2>Worst Case of Bubble Sort Program in Java<\/h2>\n<p>The worst case of the Bubble Sort algorithm is when the array is reverse sorted. This is because if the array is reverse sorted i.e. if we want to sort in ascending order and the array is in descending order or vice versa, then the bubble sort algorithm will perform the maximum number of comparisons and swaps. So, the worst case of the bubble sort algorithm is if the array is reverse sorted and the worst case time complexity is O(n2).<\/p>\n<h2>Is Bubble Sort Stable?<\/h2>\n<p>Yes, Bubble sort is a stable algorithm. What do we mean by stability? Consider the array shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498712-Bubble%20Sort%20Program%20in%20Java5.png\" alt=\"\" \/><\/p>\n<p>There are two 2s in the array. One is named 2a and the other as 2b. Before sorting, element 2a is before element 2b. After sorting also, we want 2a to be before 2b. If a sorting algorithm is able to maintain the order between the equal elements as it was before sorting, then that algorithm is a stable sorting algorithm.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, bubble sort is a simple yet inefficient sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. Despite its simplicity, it has a time complexity of O(n^2), making it less suitable for large data sets. However, it can still be useful for small arrays or in situations where simplicity is prioritized over efficiency.<\/p>\n<h2>FAQs related to bubble sort in Java:<\/h2>\n<p><strong>Q1. Can bubble sort be used for sorting objects other than integers?<\/strong><br \/>\nYes, bubble sort can be used to sort objects of any comparable type. You need to implement the comparison logic by overriding the compareTo method or by providing a custom comparator.<\/p>\n<p><strong>Q2. How does bubble sort compare to other sorting algorithms like merge sort or quicksort?<\/strong><br \/>\nBubble sort has a higher time complexity compared to more efficient algorithms like merge sort or quicksort. It is generally considered as a simple algorithm for educational purposes or when dealing with small data sets.<\/p>\n<p><strong>Q3. Is bubble sort stable?<\/strong><br \/>\nYes, bubble sort is a stable sorting algorithm. It preserves the relative order of elements with equal values. If two elements are equal, their original order will be maintained after sorting.<\/p>\n<p><strong>Q4. Can bubble sort be used for sorting a linked list?<\/strong><br \/>\nBubble sort can be applied to sort a linked list by swapping the nodes instead of array elements. However, due to its time complexity, it is not recommended for large linked lists.<\/p>\n<p><strong>Q5. Are there any optimized versions of bubble sort?<\/strong><br \/>\nYes, there are variations of bubble sort such as the optimized bubble sort or the cocktail shaker sort. These variations introduce enhancements to reduce the number of iterations or optimize the swapping process.<\/p>\n<p><strong>Other Java Programs<\/strong><\/p>\n<p><a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-add-two-numbers\/\" title=\"Java Program to Add Two Numbers\">Java Program to Add Two Numbers<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-prime-number\/\" title=\"Java Program to Check Prime Number\">Java Program to Check Prime Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-whether-a-number-is-a-palindrome-or-not\/\" title=\"Java Program to Check Whether a Number is a Palindrome or Not\">Java Program to Check Whether a Number is a Palindrome or Not<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-find-the-factorial-of-a-number\/\" title=\"Java Program to Find the Factorial of a Number\">Java Program to Find the Factorial of a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-reverse-a-number\/\" title=\"Java Program to Reverse a Number\">Java Program to Reverse a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/program-to-convert-arraylist-to-linkedlist-in-java\/\" title=\"Program to convert ArrayList to LinkedList in Java\">Program to convert ArrayList to LinkedList in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-reverse-a-linked-list\/\" title=\"Java Program to Reverse a linked list\">Java Program to Reverse a linked list<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/anagram-program-in-java\/\" title=\"Anagram Program in Java\">Anagram Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/inheritance-program-in-java\/\" title=\"Inheritance Program in Java\">Inheritance Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/even-odd-program-in-java\/\" title=\"Even Odd Program in Java\">Even Odd Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/hello-world-program-in-java\/\" title=\"Hello World Program in Java\">Hello World Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/if-else-program-in-java\/\" title=\"If else Program in Java\">If else Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/binary-search-program-in-java\/\" title=\"Binary Search Program in Java\">Binary Search Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/linear-search-program-in-java\/\" title=\"Linear Search Program in Java\">Linear Search Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/menu-driven-program-in-java\/\" title=\"Menu Driven Program in Java\">Menu Driven Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/package-program-in-java\/\" title=\"Package Program in Java\">Package Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/leap-year-program-in-java\/\" title=\"Leap Year Program in Java\">Leap Year Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/array-programs-in-java\/\" title=\"Array Programs in Java\">Array Programs in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/linked-list-program-in-java\/\" title=\"Linked List Program in Java\">Linked List Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/string-programs-in-java\/\" title=\"String Programs in Java\">String Programs in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/star-program-in-java\/\" title=\"Star Program in Java\">Star Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/number-pattern-program-in-java\/\" title=\"Number Pattern Program in Java\">Number Pattern Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/for-loop-program-in-java\/\" title=\"For Loop Program In Java\">For Loop Program In Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/pattern-program-in-java\/\" title=\"Pattern Program in Java\">Pattern Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/string-palindrome-program-in-java\/\" title=\"String Palindrome Program in Java\">String Palindrome Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/thread-program-in-java\/\" title=\"Thread Program in JAVA\">Thread Program in JAVA<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-scanner-program\/\" title=\"Java Scanner Program\">Java Scanner Program<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/while-loop-program-in-java\/\" title=\"While Loop Program in Java\">While Loop Program in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will delve into the bubble sort algorithm and its implementation in Java. We will explore the intricacies of the algorithm, examine its time and space complexity, analyze the best and worst-case scenarios, and evaluate its stability. Without further ado, let us commence with our exploration. What is Bubble Sort in Java? [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-11449","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bubble Sort Program in Java<\/title>\n<meta name=\"description\" content=\"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort Program in Java\" \/>\n<meta property=\"og:description\" content=\"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-28T10:27:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-01T07:45:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Bubble Sort Program in Java\",\"datePublished\":\"2022-12-28T10:27:17+00:00\",\"dateModified\":\"2023-07-01T07:45:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\"},\"wordCount\":1610,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\",\"name\":\"Bubble Sort Program in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\",\"datePublished\":\"2022-12-28T10:27:17+00:00\",\"dateModified\":\"2023-07-01T07:45:53+00:00\",\"description\":\"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bubble Sort Program in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort Program in Java","description":"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis","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:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort Program in Java","og_description":"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis","og_url":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-12-28T10:27:17+00:00","article_modified_time":"2023-07-01T07:45:53+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Bubble Sort Program in Java","datePublished":"2022-12-28T10:27:17+00:00","dateModified":"2023-07-01T07:45:53+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/"},"wordCount":1610,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/","url":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/","name":"Bubble Sort Program in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg","datePublished":"2022-12-28T10:27:17+00:00","dateModified":"2023-07-01T07:45:53+00:00","description":"Write the bubble sort program in Java, and will also cover various aspects of the bubble sort algorithm like its time and space complexity analysis","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672221498556-Bubble%20Sort%20Program%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-program-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Bubble Sort Program in Java"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11449"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11449\/revisions"}],"predecessor-version":[{"id":17033,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11449\/revisions\/17033"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}