{"id":1399,"date":"2017-02-07T16:38:40","date_gmt":"2017-02-07T16:38:40","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1399"},"modified":"2024-11-16T14:45:23","modified_gmt":"2024-11-16T14:45:23","slug":"write-a-java-program-for-selection-sort","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-for-selection-sort\/","title":{"rendered":"Write a Java Program for Selection Sort?"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>In this tutorial, we will learn to write a Java Program for Selection Sort. Selection sort is one of the simple sorting techniques.<\/p>\n<p>Sorting is a process of arranging things in a specified order, for example in ascending or descending order. There are many algorithms for sorting.<\/p>\n<p>Consider array a, using this technique you will find the smallest element in the list and interchange it with the first element in the list i.e a[0]. In the next pass, you will find the next smallest element in the rest of the list ( a[1]\u2026. end of the list ) and interchange with the element a[1] so on&#8230;<\/p>\n<p>After all the array passes you will be left with the sorted array.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1255\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/SelectionSortExample.png\" alt=\"Selection sort\" width=\"800\" height=\"618\" title=\"\"><\/p>\n<h2>Eclipse IDE Setup<\/h2>\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\n<ul>\n<li>Create Java Project in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\n<ul>\n<li>Create Java Package in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\n<ul>\n<li>Create Java Class in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\n<ul>\n<li>Run Java Project in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\n<h2><strong>Java program<\/strong><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.InputMismatchException;\r\nimport java.util.Scanner;\r\n\r\npublic class SelectionSort {\r\n    public static void main(String[] args) {\r\n        int n , minIndex;\r\n        Scanner input = null;\r\n        int[] a = null;\r\n        try {\r\n            input=new Scanner(System.in);\r\n            System.out.println(\"Enter size of the array:\");\r\n            n= input.nextInt();\r\n\r\n            if(n &gt; 0) {\r\n                a=new int[n];\r\n                System.out.println(\"Enter array elements:\");\r\n                for (int i = 0; i &lt; n; i++) {\r\n                    a[i] = input.nextInt();\r\n                }\r\n            }else{\r\n                System.out.println(\"Enter positive number.\");\r\n                System.exit(0);\r\n            }\r\n\r\n            System.out.println(\"Input Array:\");\r\n            printArray(a);\r\n\r\n            for(int i=0;i&lt;a.length;i++){\r\n                minIndex=i;\r\n                for(int j=i+1;j&lt;a.length;j++){\r\n                    if(a[j]&lt;a[minIndex])\r\n                        minIndex=j;\r\n                }\r\n                swap(a,i,minIndex);\r\n                System.out.println(\"Smallest element marked **\") ;\r\n                printArray(a,i);\r\n            }\r\n            System.out.println(\"Sorted Array:\");\r\n            printArray(a);\r\n        }catch (InputMismatchException ime) {\r\n            System.out.println(\"Not a valid input\");\r\n        }\r\n        catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if(input!=null)\r\n            {input.close();}\r\n        }\r\n    }\r\n\r\n    private static void printArray(int[] a){\r\n        for (int anA : a) {\r\n            System.out.print(\"[ \" + anA + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n    private static void printArray(int[] a, int index){\r\n        for (int i=0;i&lt; a.length;i++) {\r\n            if(i == index)\r\n                System.out.print(\"[* \" + a[i] + \"*]\");\r\n            else\r\n                System.out.print(\"[ \" + a[i] + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n    private static void swap( int[] a, int i , int j)\r\n    {\r\n        int temp = a[i];\r\n        a[i] = a[j];\r\n        a[j] = temp;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Run output<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1259\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selection-Sort-Program.png\" alt=\"Selection Sort Program\" width=\"1588\" height=\"681\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Enter size of the array:<br \/>\n5<br \/>\nEnter array elements:<br \/>\n23<br \/>\n56<br \/>\n9<br \/>\n103<br \/>\n77<br \/>\nInput Array:<br \/>\n[ 23 ][ 56 ][ 9 ][ 103 ][ 77 ]<br \/>\nSmallest element marked **<br \/>\n[* 9*][ 56 ][ 23 ][ 103 ][ 77 ]<br \/>\nSmallest element marked **<br \/>\n[ 9 ][* 23*][ 56 ][ 103 ][ 77 ]<br \/>\nSmallest element marked **<br \/>\n[ 9 ][ 23 ][* 56*][ 103 ][ 77 ]<br \/>\nSmallest element marked **<br \/>\n[ 9 ][ 23 ][ 56 ][* 77*][ 103 ]<br \/>\nSmallest element marked **<br \/>\n[ 9 ][ 23 ][ 56 ][ 77 ][* 103*]<br \/>\nSorted Array:<br \/>\n[ 9 ][ 23 ][ 56 ][ 77 ][ 103 ]<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1399\" data-postid=\"1399\" class=\"themify_builder_content themify_builder_content-1399 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this tutorial, we will learn to write a Java Program for Selection Sort. Selection sort is one of the simple sorting techniques. Sorting is a process of arranging things in a specified order, for example in ascending or descending order. There are many algorithms for sorting. Consider array a, using this technique you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1399","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1399"}],"version-history":[{"count":11,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1399\/revisions"}],"predecessor-version":[{"id":21767,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1399\/revisions\/21767"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}