{"id":71,"date":"2016-10-12T18:47:00","date_gmt":"2016-10-12T18:47:00","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=71"},"modified":"2021-09-06T20:37:59","modified_gmt":"2021-09-06T15:07:59","slug":"selection-sort-in-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/selection-sort-in-java\/","title":{"rendered":"Selection sort in java"},"content":{"rendered":"<blockquote><p>If you want to practice data structure and algorithm programs, you can go through\u00a0<a href=\"http:\/\/www.java2blog.com\/data-structure-and-algorithm-interview-questions-in-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">data structure and algorithm interview programs<\/a>.<\/p><\/blockquote>\n<div dir=\"ltr\" style=\"text-align: left;\">Selection sort is an in place comparison sorting algorithm. It is very simple to implement but it does not go well with large number of inputs.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Selection_sort_algorithm\">Selection sort algorithm<\/a><\/li><li><a href=\"#Selection_sort_algorithm-2\">Selection sort algorithm<\/a><\/li><li><a href=\"#Time_complexity\">Time complexity<\/a><\/li><\/ul><\/div>\n\n<h2 style=\"text-align: left;\"><span id=\"Selection_sort_algorithm\">Selection sort algorithm<\/span><\/h2>\n<ul style=\"text-align: left;\">\n<li>Find the minimum element in the list.<\/li>\n<li>Swap minimum element with current element.<\/li>\n<li>Repeat the whole process until array is fully sorted.<\/li>\n<\/ul>\n<div>Below visualization will make it more clear<\/div>\n<div style=\"clear: both; text-align: center;\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2021\/09\/SelectionSort.png\" width=\"502\" height=\"640\" border=\"0\" \/><\/div>\n<h2 style=\"text-align: left;\"><span id=\"Selection_sort_algorithm-2\">Selection sort algorithm<\/span><\/h2>\n<div>\n<pre name=\"code\" class=\"\">package org.arpit.java2blog;\n\nimport java.util.Arrays;\n\npublic class SelectionSortMain {\n\n public static int[] selectionSort(int[] arr){\n\n for (int i = 0; i < arr.length - 1; i++)\n {\n int index = i;\n for (int j = i + 1; j < arr.length; j++)\n if (arr[j] < arr[index])\n index = j;\n\n int smallerNumber = arr[index];\n arr[index] = arr[i];\n arr[i] = smallerNumber;\n }\n return arr;\n }\n\n public static void main(String a[]){\n\n int[] arr = {40,10,-30,45,39,32};\n System.out.println(\"Before Sorting : \");\n System.out.println(Arrays.toString(arr));\n arr = selectionSort(arr);\n System.out.println(\"===================\");\n System.out.println(\"After Sorting : \");\n System.out.println(Arrays.toString(arr));\n }\n}<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<pre name=\"code\" style=\"text-align: left;\">Before Sorting :\n[40, 10, -30, 45, 39, 32]\n===================\nAfter Sorting :\n[-30, 10, 32, 39, 40, 45]<\/pre>\n<h2 style=\"text-align: left;\"><span id=\"Time_complexity\">Time complexity<\/span><\/h2>\n<p>Best case : <code>O(N^2)<\/code><br \/>\nAverage case : <code>O(N^2)<\/code><br \/>\nWorst case : <code>O(N^2)<\/code><\/div>\n<\/div>\n<div style=\"text-align: left;\">To understand more about complexity,please go through\u00a0<a href=\"http:\/\/www.java2blog.com\/introduction-to-complexity-of-algorithm\/\">complexity of algorithm<\/a>.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSelection sort algorithmSelection sort algorithmTime complexity If you want to practice data structure and algorithm programs, you can go through\u00a0data structure and algorithm interview programs. Selection sort is an in place comparison sorting algorithm. It is very simple to implement but it does not go well with large number of inputs. Selection sort [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[29,13],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}