{"id":1365,"date":"2017-05-06T10:39:45","date_gmt":"2017-05-06T10:39:45","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1365"},"modified":"2024-11-16T14:57:17","modified_gmt":"2024-11-16T14:57:17","slug":"what-is-insertion-sort-write-a-java-program-to-demonstrate-it","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-insertion-sort-write-a-java-program-to-demonstrate-it\/","title":{"rendered":"What is Insertion Sort? Write a java program to demonstrate it?"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>The insertion sort technique is similar to sorting playing cards in our hands. We pick a card and place it in the sort order. When we complete this process with all the cards the array will be sorted.<\/p>\n<p>A sample array sorting is shown in the below picture:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1263\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InsertionSort.png\" alt=\"Insertion Sort\" width=\"1056\" height=\"816\" title=\"\"><\/p>\n<h3><strong>Java Program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.InputMismatchException;\r\nimport java.util.Scanner;\r\n\r\npublic class InsertionSort {\r\n    public static void main(String[] args) {\r\n        int n , handIndex;\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            for(int i =0 ; i&lt;a.length; i++)\r\n                insertionSort(a,i+1) ;\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        {\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 aA : a) {\r\n            System.out.print(\"[ \" + aA + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n    private static void insertionSort(int[] a, int card){\r\n        for(int j=1; j&lt; card; j++){\r\n            int key = a[j];\r\n            int k = j-1;\r\n            while(k &gt;=0 &amp;&amp; key &lt; a[k])\r\n            {\r\n                a[k+1] = a[k];\r\n                --k;\r\n            }\r\n            a[k+1] = key;\r\n        }\r\n\r\n        for(int hand=0; hand&lt; card; hand++){\r\n            System.out.print(\"[h \" + a[hand] + \" h]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>The run output of the program<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1264\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/InsertionSort-Java-Program.png\" alt=\"InsertionSort Java Program\" width=\"1261\" height=\"610\" title=\"\"><\/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 \/>\n[h 23 h]<br \/>\n[h 23 h][h 56 h]<br \/>\n[h 9 h][h 23 h][h 56 h]<br \/>\n[h 9 h][h 23 h][h 56 h][h 103 h]<br \/>\n[h 9 h][h 23 h][h 56 h][h 77 h][h 103 h]<br \/>\nSorted Array:<br \/>\n[ 9 ][ 23 ][ 56 ][ 77 ][ 103 ]<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1365\" data-postid=\"1365\" class=\"themify_builder_content themify_builder_content-1365 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction The insertion sort technique is similar to sorting playing cards in our hands. We pick a card and place it in the sort order. When we complete this process with all the cards the array will be sorted. A sample array sorting is shown in the below picture: &nbsp; Java Program import java.util.InputMismatchException; import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1365","post","type-post","status-publish","format-standard","hentry","category-automation","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\/1365","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=1365"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":19628,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1365\/revisions\/19628"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}