{"id":238,"date":"2015-06-19T13:45:38","date_gmt":"2015-06-19T03:45:38","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=238"},"modified":"2015-06-30T14:38:23","modified_gmt":"2015-06-30T04:38:23","slug":"selection-sort","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","title":{"rendered":"Selection Sort"},"content":{"rendered":"
\n

”<\/span> The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive. “<\/span><\/p>\n

Selection Sort, Wikipedia<\/a><\/cite><\/p><\/blockquote>\n

When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It’s not a particularly efficient algorithm and really only suitable for small lists.<\/p>\n

\r\npackage com.learnjava.math.sort;\r\n\r\nimport java.util.Random;\r\n\r\n\/**\r\n * Write a program to sort an array of numbers using a Selection Sort\r\n * Also known as an Exchange Sort\r\n *\r\n * @author https:\/\/learn-java-by-example.com\r\n *\r\n *\/\r\npublic class SelectionSort {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\t\/\/ Create an array of random numbers to sort\r\n\r\n\t\tint[] data = generateData();\r\n\r\n\t\t\/\/ Display the unsorted data\r\n\r\n\t\tSystem.out.println("Unsorted data:");\r\n\t\tprintArray(data);\r\n\r\n\t\t\/\/ Sort the array\r\n\r\n\t\tsortArray(data);\r\n\r\n\t\t\/\/ Display the sorted data\r\n\r\n\t\tSystem.out.println("Sorted data:");\r\n\t\tprintArray(data);\r\n\t}\r\n\r\n\tprivate static void sortArray(int[] data) {\r\n\r\n\t\t\/\/ Loop through the array,\r\n\t\t\/\/ comparing each value with each of the following values\r\n\r\n\t\tfor (int i=0; i<data.length-1; i++) {\r\n\t\t\tfor (int j=i+1; j<data.length; j++) {\r\n\r\n\t\t\t\t\/\/ Check if the value needs to be swapped or not\r\n\r\n\t\t\t\tif (data[i] < data[j]) {\r\n\r\n\t\t\t\t\t\/\/ Swap values\r\n\r\n\t\t\t\t\tint temp = data[i];\r\n\t\t\t\t\tdata[i] = data[j];\r\n\t\t\t\t\tdata[j] = temp;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Print out the array after we have\r\n\t\t\t\/\/ finished each element to see the progress\r\n\r\n\t\t\tSystem.out.println("Loop " + i);\r\n\t\t\tprintArray(data);\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static int[] generateData() {\r\n\t\tRandom random = new Random();\r\n\r\n\t\tint[] data = new int[10];\r\n\t\tfor (int i=0; i<data.length; i++) {\r\n\t\t\tdata[i] = random.nextInt(100);\r\n\t\t}\r\n\t\treturn data;\r\n\t}\r\n\r\n\tprivate static void printArray(int[] data) {\r\n\t\tfor (int i=0; i<data.length; i++) {\r\n\t\t\tSystem.out.print(data[i]);\r\n\t\t\tSystem.out.print(", ");\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It’s not a particularly efficient algorithm and really only suitable for small lists.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[43,6,66],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-3Q","jetpack-related-posts":[{"id":247,"url":"https:\/\/learn-java-by-example.com\/java\/bubble-sort\/","url_meta":{"origin":238,"position":0},"title":"Bubble Sort","date":"June 30, 2015","format":false,"excerpt":"The Bubble Sort algorithm gets it's name from the fact that on each pass, one element of the array 'bubbles' it's way through to it's correct (sorted) position in the array. Rather than me try and explain the details the following video gives a great visual demonstration of how the\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/img.youtube.com\/vi\/MtcrEhrt_K0\/0.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":199,"url":"https:\/\/learn-java-by-example.com\/java\/pythagoras-theorem\/","url_meta":{"origin":238,"position":1},"title":"Pythagoras Theorem","date":"December 13, 2010","format":false,"excerpt":"One of our member students was asked to implement the Pythagoras Theorem using Java. This is a good opportunity to introduce the Math class which contains a collection of static methods for various mathematical functions. We need the square root method (sort) to calculate the hypotenuse, and can also use\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":238,"position":2},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":251,"url":"https:\/\/learn-java-by-example.com\/java\/add-checkbox-items-jlist\/","url_meta":{"origin":238,"position":3},"title":"How do add a checkbox to items in a JList?","date":"June 30, 2015","format":false,"excerpt":"We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox's seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean's as\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":238,"position":4},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":154,"url":"https:\/\/learn-java-by-example.com\/java\/prime-numbers\/","url_meta":{"origin":238,"position":5},"title":"Prime numbers","date":"January 21, 2011","format":false,"excerpt":"The assignment here is to calculate all prime numbers less than 100. The solution provided uses the following to determine if a given number is prime. 2 is prime Any number divisible by 2 is not prime If the number is divisible by any odd number then it is not\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/238"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":4,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/238\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}