{"id":1390,"date":"2022-03-03T19:17:34","date_gmt":"2022-03-03T19:17:34","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1390"},"modified":"2022-03-03T19:17:34","modified_gmt":"2022-03-03T19:17:34","slug":"program-to-perform-bubble-sort-on-strings","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-perform-bubble-sort-on-strings\/","title":{"rendered":"Program to perform Bubble Sort on Strings"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Bubble Sort on Strings example<\/h4>\n\n\n\n<p>In the following example we have stored the strings in a String array and we are using nested for loops to compare adjacent strings in the array, if they are not in order we are\u00a0swapping\u00a0them using a temporary string variable\u00a0<code>temp<\/code>.<\/p>\n\n\n\n<p>Here we are using\u00a0compareTo() method\u00a0to compare the adjacent Strings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class JavaExample {\n   public static void main(String &#91;]args) {\n\tString str&#91;] = { \"Ajeet\", \"Steve\", \"Rick\", \"Becky\", \"Mohan\"};\n\tString temp;\n\tSystem.out.println(\"Strings in sorted order:\");\n\tfor (int j = 0; j &lt; str.length; j++) {\n   \t   for (int i = j + 1; i &lt; str.length; i++) {\n\t\t\/\/ comparing adjacent strings\n\t\tif (str&#91;i].compareTo(str&#91;j]) &lt; 0) {\n\t\t\ttemp = str&#91;j];\n\t\t\tstr&#91;j] = str&#91;i];\n\t\t\tstr&#91;i] = temp;\n\t\t}\n\t   }\n\t   System.out.println(str&#91;j]);\n\t}\n   }\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/beginnersbook.com\/wp-content\/uploads\/2019\/04\/bubble_sort_on_strings.jpg\" alt=\"Java bubble sort on strings example\" class=\"wp-image-10128\"\/><\/figure><\/div>\n\n\n\n<p><strong>Output:<\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bubble Sort on Strings example In the following example we have stored the strings in a String array and we are using nested for loops to compare adjacent strings in the array, if they are not in order we are\u00a0swapping\u00a0them using a temporary string variable\u00a0temp. Here we are using\u00a0compareTo() method\u00a0to compare the adjacent Strings. Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1390"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1390"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1390\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}