{"id":11610,"date":"2023-01-09T07:00:54","date_gmt":"2023-01-09T07:00:54","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11610"},"modified":"2023-09-22T08:38:03","modified_gmt":"2023-09-22T08:38:03","slug":"bubble-sort-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/","title":{"rendered":"Bubble Sort in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<p>Bubble sort is a sorting algorithm that uses the swapping of elements to sort the array in either ascending or descending order. Bubble sort is the simplest and easy-to-understand sorting algorithm. In this article, we will learn more about bubble sort, the bubble sort algorithm java, and the time and space complexity of the bubble sort program in java.<\/p>\n<h2>What is Bubble Sort Java?<\/h2>\n<p>The bubble sort algorithm compares adjacent elements and swaps them to sort the array. The bubble sort uses two for loops to sort the array. The first for loop (outer for loop) is used to traverse the array n times. The Second for loop (inner for loop) is used to traverse the array and swap elements if necessary.<\/p>\n<h2>Bubble Sort Algorithm Java<\/h2>\n<p>Let\u2019s understand how the bubble sort algorithm java works using an example and visualizing the algorithm. The process of sorting the array in ascending using the bubble sort algorithm java is given below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671607224884-Bubble%20Sort%20in%20Python1.png\" alt=\"\" \/><\/p>\n<p>Now Let\u2019s see how n-1 (n=size of an array) array passes work on an array.<\/p>\n<p><strong>First pass ( i=0)<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671607224884-Bubble%20Sort%20in%20Python2.png\" alt=\"\" \/><\/p>\n<ul>\n<li>First, we will run the inner loop for n-i-1 (5-0-1 = 4) times and we will compare each adjacent pair.<\/li>\n<li>For j=0, compare j=0 (array[0]=4) and j+1=1 (array[1]=3) here array[j]&gt;array[j+1] so we will swap two elements because we want higher elements on the right side to sort an array.<\/li>\n<li>For j=1, array[1] (4) &lt; array[2] (9) so we will not swap elements because the higher element (9) is already on the right side.<\/li>\n<li>For j=2, array[2] (9) &gt; array[3] (1) so we will swap elements.<\/li>\n<li>For j=3, array[3] (9) &gt; array[4] (5) so we will swap elements.<\/li>\n<li>After performing the first pass we can see that the last element of an array (9) is already at its right position so in further passes we will not touch that element.<\/li>\n<\/ul>\n<p><strong>Second pass ( i=1)<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671607224890-Bubble%20Sort%20in%20Python3.png\" alt=\"\" \/><\/p>\n<ul>\n<li>In the second pass (i=1), we will run inner loop n-i-1 time ( 5-1-1 =3) which one less than the first pass it is because the last element is already sorted in the second pass so we will not consider the last element.<\/li>\n<li>For j=0, array[0] (3) &lt; array[1] (4) so we will not swap elements.<\/li>\n<li>For j=1, array[1] (4) &gt; array[2] (1) so we will swap elements.<\/li>\n<li>For j=2, array[2] (4) &lt; array[3] (5) so we will not swap elements<\/li>\n<li>After the completion of the second pass, we can see that the last two elements of an array ( [5,9] ) are in their right position.<\/li>\n<\/ul>\n<p><strong>Third pass ( i=2)<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671607224890-Bubble%20Sort%20in%20Python4.png\" alt=\"\" \/><\/p>\n<ul>\n<li>In the third pass ( i=2 ), we will run the inner loop n-i-1 (5-2-1 = 2) times.<\/li>\n<li>For j=0, array[0] (3) &gt; array[1] (1) so we will swap elements.<\/li>\n<li>For j=1, array[1] (3) &lt; array[2] (4) so we will not swap elements.<\/li>\n<li>After performing the third pass we can see that the last three elements ( [4,5,9] ) are in their right positions.<\/li>\n<\/ul>\n<p><strong>Fourth pass ( i=3)<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671607224890-Bubble%20Sort%20in%20Python5.png\" alt=\"\" \/><\/p>\n<ul>\n<li>In the fourth pass (i=3 ), we will run the inner loop n-i-1 (5-3-1=1) times.<\/li>\n<li>For j=0, array[0] (1) &lt; array[1] (3) so we will not swap elements.<\/li>\n<li>After performing the fourth pass we can see that our array is sorted.<\/li>\n<\/ul>\n<p>Sorting happens in place in bubble sort java which means if there are two elements with the same values so after performing the bubble sort program in java the order of that two elements will be the same.  Let\u2019s understand this with an example array= [4,6,3,6,5] here two same elements are 6 at index 2 and 4 now after performing bubble sort array= [3,4,5,6,6] now two elements which is same are 6 at index 4 and 5 so element  6 at index 4 is element 6 at index 2 before performing sorting and element 6 at index 5 is element at index 4 before performing sorting.<\/p>\n<h2>Bubble Sort Program in Java:-<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11595 {\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_11595 .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_11595 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11595 .wpsm_nav-tabs > li.active > a, #tab_container_11595 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11595 .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_11595 .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_11595 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11595 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11595 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11595 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11595 .wpsm_nav-tabs > li > a:hover , #tab_container_11595 .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_11595 .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_11595 .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_11595 .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_11595 .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_11595 .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_11595 .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_11595 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11595 .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_11595 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11595 .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_11595 .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_11595\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11595\">\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_11595_1\" aria-controls=\"tabs_desc_11595_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_11595\">\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_11595_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Prepbytes{\r\n    \/\/ method to implement bubble sort program in java\r\n    public static void BubbleSort(int[] arr){\r\n        \/\/ find the length of an array\r\n        int n=arr.length;\r\n        \/\/ run outer for loop n-1 times\r\n        for(int i=0;i&lt;n-1;i++){\r\n            \/\/ run inner for loop n-i-1 times\r\n            for(int j=0;j&lt;n-i-1;j++){\r\n                \/\/ check if j th element is greater than j+1 th element\r\n                if(arr[j]&gt;arr[j+1]){\r\n                    \/\/ swap j and j+1 th elements\r\n                    int tmp=arr[j];\r\n                    arr[j]=arr[j+1];\r\n                    arr[j+1]=tmp;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    \r\n    \/\/ method to print an array\r\n    public static void DisplayArray(int[] arr,boolean before){\r\n        if(before==true){\r\n            System.out.print(\"Array Before Sorting: \");\r\n        }\r\n        else{\r\n            System.out.print(\"Array After Sorting: \");\r\n        }\r\n        for(int i=0;i&lt;5;i++){\r\n            System.out.print(arr[i]+\" \");\r\n        }\r\n        System.out.println();\r\n    }\r\n    \r\n    public static void main(String[] args) {\r\n        \/\/ initialize an array\r\n        int[] arr=new int[]{4,3,9,1,5};\r\n        \/\/ print array before performing bubble sort program in java\r\n        DisplayArray(arr,true);\r\n        \/\/ perform bubble sort program in java\r\n        BubbleSort(arr);\r\n        \/\/ print array after performing bubble sort program in java\r\n        DisplayArray(arr,false);\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_11595 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_11595 a\"),jQuery(\"#tab-content_11595\"));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\n<p><strong>Output:-<\/strong><\/p>\n<pre><code>Array Before Sorting: 4 3 9 1 5 \nArray After Sorting: 1 3 4 5 9 <\/code><\/pre>\n<p>There is one problem with the above bubble sort program in java when a given array is already sorted the above program will still finish all the two loops so the time complexity becomes O(n^2) though the array is already sorted. Let\u2019s see how can we optimize the above bubble sort program in java.<\/p>\n<h3>Optimized Bubble Sort Program in Java:-<\/h3>\n<p>We can observe one thing if our array is already sorted then no swap will be performed so we can use this observation to write an optimized bubble sort program in java.<br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11596 {\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_11596 .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_11596 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11596 .wpsm_nav-tabs > li.active > a, #tab_container_11596 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11596 .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_11596 .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_11596 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11596 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11596 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11596 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11596 .wpsm_nav-tabs > li > a:hover , #tab_container_11596 .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_11596 .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_11596 .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_11596 .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_11596 .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_11596 .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_11596 .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_11596 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11596 .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_11596 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11596 .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_11596 .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_11596\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11596\">\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_11596_1\" aria-controls=\"tabs_desc_11596_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_11596\">\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_11596_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Prepbytes{\r\n    \/\/ method to implement bubble sort program in java\r\n    public static void BubbleSort(int[] arr){\r\n        \/\/ find the length of an array\r\n        int n=arr.length;\r\n        \/\/ run outer for loop n-1 times\r\n        for(int i=0;i&lt;n-1;i++){\r\n            \/\/ variable to keep track of swap is performed or not\r\n            boolean is_swap=false;\r\n            \/\/ run inner for loop n-i-1 times\r\n            for(int j=0;j&lt;n-i-1;j++){\r\n                \/\/ check if j th element is greater than j+1 th element\r\n                if(arr[j]&gt;arr[j+1]){\r\n                    \/\/ swap is performed so make is_swap true\r\n                    is_swap=true;\r\n                    \/\/ swap j and j+1 th elements\r\n                    int tmp=arr[j];\r\n                    arr[j]=arr[j+1];\r\n                    arr[j+1]=tmp;\r\n                }\r\n            }\r\n            \/\/ if no swap is performed then break the loop\r\n            if(is_swap==false){\r\n                break;\r\n            }\r\n        }\r\n    }\r\n    \r\n    \/\/ method to print an array\r\n    public static void DisplayArray(int[] arr,boolean before){\r\n        if(before==true){\r\n            System.out.print(\"Array Before Sorting: \");\r\n        }\r\n        else{\r\n            System.out.print(\"Array After Sorting: \");\r\n        }\r\n        for(int i=0;i&lt;5;i++){\r\n            System.out.print(arr[i]+\" \");\r\n        }\r\n        System.out.println();\r\n    }\r\n    \r\n    public static void main(String[] args) {\r\n        \/\/ initialize an array\r\n        int[] arr=new int[]{4,3,9,1,5};\r\n        \/\/ print array before performing bubble sort program in java\r\n        DisplayArray(arr,true);\r\n        \/\/ perform bubble sort program in java\r\n        BubbleSort(arr);\r\n        \/\/ print array after performing bubble sort program in java\r\n        DisplayArray(arr,false);\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_11596 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_11596 a\"),jQuery(\"#tab-content_11596\"));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<h2>Time complexity analysis of bubble sort java :-<\/h2>\n<ul>\n<li><strong>Best:- O(n)<\/strong> when the given array is already sorted at that time program using only one loop.<\/li>\n<li><strong>Average:- O(n^2)<\/strong>when the given array is not sorted.<\/li>\n<li><strong>Worst:- O(n^2)<\/strong> when the given array is reverse sorted at that time we have to perform a swap for each comparison. So the total swaps performed will be n*(n-1)\/2<\/li>\n<\/ul>\n<h2>Auxiliary Space complexity of bubble sort java:-<\/h2>\n<ul>\n<li><strong>Auxiliary space complexity:- O(1)<\/strong> it\u2019s because we are not using any extra arrays to perform bubble sort java. We are only using variables so the space complexity is O(1) to perform the bubble sort algorithm java.<\/li>\n<\/ul>\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><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/bubble-sort-program-in-java\/\" title=\"Bubble Sort Program in Java\">Bubble Sort Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/fibonacci-series-program-in-java\/\" title=\"Fibonacci Series Program in Java\">Fibonacci Series Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/calculator-program-in-java\/\" title=\"Calculator Program in Java\">Calculator Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/fizzbuzz-program-in-java\/\" title=\"Fizzbuzz Program in Java\">Fizzbuzz Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/matrix-program-in-java\/\" title=\"Matrix Program in Java\">Matrix Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/stack-program-in-java\/\" title=\"Stack Program in Java\">Stack Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/recursion-program-in-java\/\" title=\"Recursion Program in Java\">Recursion Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/multiple-inheritance-program-in-java\/\" title=\"Multiple Inheritance Program in Java\">Multiple Inheritance Program in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bubble sort is a sorting algorithm that uses the swapping of elements to sort the array in either ascending or descending order. Bubble sort is the simplest and easy-to-understand sorting algorithm. In this article, we will learn more about bubble sort, the bubble sort algorithm java, and the time and space complexity of the bubble [&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-11610","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 in Java<\/title>\n<meta name=\"description\" content=\"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.\" \/>\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-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 in Java\" \/>\n<meta property=\"og:description\" content=\"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/bubble-sort-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=\"2023-01-09T07:00:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-22T08:38:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%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=\"6 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-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Bubble Sort in Java\",\"datePublished\":\"2023-01-09T07:00:54+00:00\",\"dateModified\":\"2023-09-22T08:38:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/\"},\"wordCount\":1031,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/\",\"name\":\"Bubble Sort in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg\",\"datePublished\":\"2023-01-09T07:00:54+00:00\",\"dateModified\":\"2023-09-22T08:38:03+00:00\",\"description\":\"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-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 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 in Java","description":"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.","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-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort in Java","og_description":"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.","og_url":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-09T07:00:54+00:00","article_modified_time":"2023-09-22T08:38:03+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Bubble Sort in Java","datePublished":"2023-01-09T07:00:54+00:00","dateModified":"2023-09-22T08:38:03+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/"},"wordCount":1031,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/","url":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/","name":"Bubble Sort in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg","datePublished":"2023-01-09T07:00:54+00:00","dateModified":"2023-09-22T08:38:03+00:00","description":"We will learn about the working of bubble sort, the bubble sort program and algorithm in java with the time and space complexity of the bubble sort program.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673243518886-Bubble%20Sort%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-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 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\/11610","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=11610"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11610\/revisions"}],"predecessor-version":[{"id":17995,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11610\/revisions\/17995"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}