{"id":1423,"date":"2022-03-03T19:41:30","date_gmt":"2022-03-03T19:41:30","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1423"},"modified":"2022-03-03T19:41:30","modified_gmt":"2022-03-03T19:41:30","slug":"bubble-sort-in-ascending-and-descending-order","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/bubble-sort-in-ascending-and-descending-order\/","title":{"rendered":"Bubble sort in Ascending and descending order"},"content":{"rendered":"\n<p>in this tutorial we are gonna see how to do sorting in ascending &amp; descending order using\u00a0Bubble sort\u00a0algorithm.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bubble sort program for sorting in ascending Order<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass BubbleSortExample {\n  public static void main(String &#91;]args) {\n    int num, i, j, temp;\n    Scanner input = new Scanner(System.in);\n \n    System.out.println(\"Enter the number of integers to sort:\");\n    num = input.nextInt();\n \n    int array&#91;] = new int&#91;num];\n \n    System.out.println(\"Enter \" + num + \" integers: \");\n \n    for (i = 0; i &lt; num; i++) \n      array&#91;i] = input.nextInt();\n \n    for (i = 0; i &lt; ( num - 1 ); i++) {\n      for (j = 0; j &lt; num - i - 1; j++) {\n        if (array&#91;j] &gt; array&#91;j+1]) \n        {\n           temp = array&#91;j];\n           array&#91;j] = array&#91;j+1];\n           array&#91;j+1] = temp;\n        }\n      }\n    }\n \n    System.out.println(\"Sorted list of integers:\");\n \n    for (i = 0; i &lt; num; i++) \n      System.out.println(array&#91;i]);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number of integers to sort:\n6\nEnter 6 integers: \n12\n6\n78\n9\n45\n08\nSorted list of integers:\n6\n8\n9\n12\n45\n78<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Bubble sort program for sorting in descending Order<\/h2>\n\n\n\n<p>In order to sort in descending order we just need to change the logic array[j] &gt; array[j+1] to array[j] &lt; array[j+1] in the above program. Complete code as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\nclass BubbleSortExample {\n  public static void main(String &#91;]args) {\n    int num, i, j, temp;\n    Scanner input = new Scanner(System.in);\n \n    System.out.println(\"Enter the number of integers to sort:\");\n    num = input.nextInt();\n \n    int array&#91;] = new int&#91;num];\n \n    System.out.println(\"Enter \" + num + \" integers: \");\n \n    for (i = 0; i &lt; num; i++) \n      array&#91;i] = input.nextInt();\n \n    for (i = 0; i &lt; ( num - 1 ); i++) {\n      for (j = 0; j &lt; num - i - 1; j++) {\n        if (array&#91;j] &lt; array&#91;j+1]) \n        {\n          temp = array&#91;j];\n          array&#91;j] = array&#91;j+1];\n          array&#91;j+1] = temp;\n        }\n      }\n    }\n \n    System.out.println(\"Sorted list of integers:\");\n \n    for (i = 0; i &lt; num; i++) \n      System.out.println(array&#91;i]);\n  } \n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number of integers to sort:\n6\nEnter 6 integers: \n89\n12\n45\n9\n56\n102\nSorted list of integers:\n102\n89\n56\n45\n12\n9<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>in this tutorial we are gonna see how to do sorting in ascending &amp; descending order using\u00a0Bubble sort\u00a0algorithm. Bubble sort program for sorting in ascending Order Output: Bubble sort program for sorting in descending Order In order to sort in descending order we just need to change the logic array[j] &gt; array[j+1] to array[j] &lt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[586],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1423"}],"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=1423"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}