{"id":1396,"date":"2022-03-03T19:21:11","date_gmt":"2022-03-03T19:21:11","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1396"},"modified":"2022-03-03T19:21:11","modified_gmt":"2022-03-03T19:21:11","slug":"program-to-sort-an-array-in-ascending-order","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-sort-an-array-in-ascending-order\/","title":{"rendered":"Program to Sort an Array in Ascending Order"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Program to Sort an Array in Ascending Order<\/h2>\n\n\n\n<p>In this program, user is asked to enter the number of elements that he wish to enter. Based on the input we have declared an int array and then we are accepting all the\u00a0numbers input by user\u00a0and storing them in the array.<\/p>\n\n\n\n<p>Once we have all the numbers stored in the array, we are sorting them using&nbsp;<a href=\"https:\/\/beginnersbook.com\/2015\/03\/for-loop-in-java-with-example\/\">nested for loop<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\npublic class JavaExample \n{\n    public static void main(String&#91;] args) \n    {\n    \tint count, temp;\n    \t\n    \t\/\/User inputs the array size\n        Scanner scan = new Scanner(System.in);\n        System.out.print(\"Enter number of elements you want in the array: \");\n        count = scan.nextInt();\n    \n        int num&#91;] = new int&#91;count];\n        System.out.println(\"Enter array elements:\");\n        for (int i = 0; i &lt; count; i++) \n        {\n            num&#91;i] = scan.nextInt();\n        }\n        scan.close();\n        for (int i = 0; i &lt; count; i++) \n        {\n            for (int j = i + 1; j &lt; count; j++) { \n                if (num&#91;i] &gt; num&#91;j]) \n                {\n                    temp = num&#91;i];\n                    num&#91;i] = num&#91;j];\n                    num&#91;j] = temp;\n                }\n            }\n        }\n        System.out.print(\"Array Elements in Ascending Order: \");\n        for (int i = 0; i &lt; count - 1; i++) \n        {\n            System.out.print(num&#91;i] + \", \");\n        }\n        System.out.print(num&#91;count - 1]);\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\/2018\/10\/java_program_array_sorting_ascending.jpg\" alt=\"Java Program to Sort an Array in Ascending Order\" class=\"wp-image-8867\"\/><\/figure><\/div>\n\n\n\n<p>Output:<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program to Sort an Array in Ascending Order In this program, user is asked to enter the number of elements that he wish to enter. Based on the input we have declared an int array and then we are accepting all the\u00a0numbers input by user\u00a0and storing them in the array. Once we have all the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[404],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1396"}],"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=1396"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1396\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}