commit e1a2185def1e03d137cd496f0abf53ae80dcd1e2 Author: Charles Reid <charlesreid1@gmail.com> Date: Sun May 28 00:24:54 2017 -0700 adding SelectionSort class for algorithm complexity notes. diff --git a/algorithm_complexity/Makefile b/algorithm_complexity/Makefile new file mode 100644 index 0000000..8139d7b --- /dev/null +++ b/algorithm_complexity/Makefile @@ -0,0 +1,3 @@ +default: + javac SelectionSort.java + java SelectionSort diff --git a/algorithm_complexity/SelectionSort.java b/algorithm_complexity/SelectionSort.java new file mode 100644 index 0000000..556b9c8 --- /dev/null +++ b/algorithm_complexity/SelectionSort.java @@ -0,0 +1,35 @@ +import java.util.Arrays; + +public class SelectionSort { + + public static void selection_sort(char[] s) { + int min; // indexes of minimum + int n = s.length; + for(int i=0; i<n; i++) { + + // Print the String at each step: + System.out.println(new String(s)); + + min = i; + for(int j=i+1; j<n; j++) { + if(s[j] < s[min]) { + min = j; + } + } + swap(s, i, min); + } + } + + public static void swap(char[] s, int i, int min) { + char temp = s[i]; + s[i] = s[min]; + s[min] = temp; + } + + public static void main(String[] args) { + char[] s = "SELECTIONSORT".toCharArray(); + System.out.println(Arrays.toString(s)); + selection_sort(s); + System.out.println(Arrays.toString(s)); + } +}