
Selection Sort Algorithm using Java
Selection Sort
- Repeatedly searches for the largest value in a section of the data
- Moves that value into its correct position in a sorted section of the list
- Uses the Find Largest algorithm
Pseudo Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
1. Get values for n and the n list items
2. Set the marker for the unsorted section at the end of the list
3. While the unsorted section of the list is not empty, do steps 4 through 6
4. Select the largest number in the unsorted section of the list
Exchange this number with the last number in the unsorted
section of the list
6. Move the marker for the unsorted section left one position
7. Stop
|
- Count comparisons of largest so far against other values
- Find Largest, given m values, does m-1 comparisons
- Selection sort calls Find Largest n times,
- Each time with a smaller list of values
- Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2
Efficiency
- Time efficiency
- Comparisons: n(n-1)/2
- Exchanges: n (swapping largest into place)
- Overall: (n2), best and worst cases
- Space efficiency
- Space for the input sequence, plus a constant number of local variables
In this lesson we will learn how to write a source code in Java programming language for doing simple Selection sort using array in ascending order.
Java Example :
import java.util.Arrays; /** * Created by ProgrammingKnowledge. */ public class SelectionSort { public static int[] doSelectionSort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { int index = i; for (int j = i + 1; j < arr.length; j++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } return arr; } public static void main(String a[]){ int[] list = {4,3,2,5,9,6,3,21,42,4,3,6}; System.out.println("Before Selection sort\n"); System.out.println(Arrays.toString(list)); list = doSelectionSort(list); System.out.println("\nAfter Selection sort"); System.out.println(Arrays.toString(list)); } } /** * Output Before Selection sort [4, 3, 2, 5, 9, 6, 3, 21, 42, 4, 3, 6] After Selection sort [2, 3, 3, 3, 4, 4, 5, 6, 6, 9, 21, 42] */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply