
{"id":7136,"date":"2012-04-20T16:35:45","date_gmt":"2012-04-20T16:35:45","guid":{"rendered":"http:\/\/www.beautifulwork.org\/?p=7136"},"modified":"2012-04-20T16:35:45","modified_gmt":"2012-04-20T16:35:45","slug":"bubble-sort-in-java","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2012\/04\/20\/bubble-sort-in-java\/","title":{"rendered":"bubble sort in java"},"content":{"rendered":"<h4><u>commandline session<\/u><\/h4>\n<pre>\n\/*\n        Java Bubble Sort Example\n        This Java bubble sort example shows how to sort an array of int using bubble\n        sort algorithm. Bubble sort is the simplest sorting algorithm.\n*\/\n\npublic class BubbleSort {\n\n        public static void main(String[] args) {\n\n                \/\/create an int array we want to sort using bubble sort algorithm\n                int intArray[] = new int[]{5,90,35,45,150,3};\n\n                \/\/print array before sorting using bubble sort algorithm\n                System.out.println(\"Array Before Bubble Sort\");\n                for(int i=0; i &lt; intArray.length; i++){\n                        System.out.print(intArray[i] + &quot; &quot;);\n                }\n\n                \/\/sort an array using bubble sort algorithm\n                bubbleSort(intArray);\n\n                System.out.println(&quot;&quot;);\n\n                \/\/print array after sorting using bubble sort algorithm\n                System.out.println(&quot;Array After Bubble Sort&quot;);\n                for(int i=0; i  array [1] swap it.\n                 * 3. Compare array[1] &amp; array[2]\n                 * 4. If array[1] &gt; array[2] swap it.\n                 * ...\n                 * 5. Compare array[n-1] &amp; array[n]\n                 * 6. if [n-1] &gt; array[n] then swap it.\n                 *\n                 * After this step we will have largest element at the last index.\n                 *\n                 * Repeat the same steps for array[1] to array[n-1]\n                 *\n                 *\/\n\n                int n = intArray.length;\n                int temp = 0;\n\n                for(int i=0; i &lt; n; i++){\n                        for(int j=1; j  intArray[j]){\n                                        \/\/swap the elements!\n                                        temp = intArray[j-1];\n                                        intArray[j-1] = intArray[j];\n                                        intArray[j] = temp;\n                                }\n\n                        }\n                }\n\n        }\n}\n\n\/*\nOutput of the Bubble Sort Example would be\n\nArray Before Bubble Sort\n5 90 35 45 150 3\nArray After Bubble Sort\n3 5 35 45 90 150\n\n*\/\n<\/pre>\n<p><\/br><\/p>\n<pre>\n$ 4.2.20 7 507---&gt; javac BubbleSort.java\n$ 4.2.20 8 508---&gt; java BubbleSort\nArray Before Bubble Sort\n5 90 35 45 150 3\nArray After Bubble Sort\n3 5 35 45 90 150\n $ 4.2.20 9 509---&gt;\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>commandline session \/* Java Bubble Sort Example This Java bubble sort example shows how to sort an array of int using bubble sort algorithm. Bubble sort is the simplest sorting algorithm. *\/ public class BubbleSort { public static void main(String[] args) { \/\/create an int array we want to sort using bubble sort algorithm int &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2012\/04\/20\/bubble-sort-in-java\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;bubble sort in java&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/7136"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=7136"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/7136\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=7136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=7136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=7136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}